Skip to content

Commit

Permalink
rustc: update docs & propagate @[]/@str removal more.
Browse files Browse the repository at this point in the history
Various functions can now be made specific to ~[], or just non-managed
vectors.
  • Loading branch information
huonw committed Feb 1, 2014
1 parent c8947c1 commit 2125074
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 128 deletions.
2 changes: 1 addition & 1 deletion doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -3115,7 +3115,7 @@ Such a definite-sized vector type is a first-class type, since its size is known
A vector without such a size is said to be of _indefinite_ size,
and is therefore not a _first-class_ type.
An indefinite-size vector can only be instantiated through a pointer type,
such as `&[T]`, `@[T]` or `~[T]`.
such as `&[T]` or `~[T]`.
The kind of a vector type depends on the kind of its element type,
as with other simple structural types.

Expand Down
10 changes: 0 additions & 10 deletions src/librustc/middle/borrowck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,6 @@ pub struct BorrowStats {
//
// Note that there is no entry with derefs:3---the type of that expression
// is T, which is not a box.
//
// Note that implicit dereferences also occur with indexing of `@[]`,
// `@str`, etc. The same rules apply. So, for example, given a
// variable `x` of type `@[@[...]]`, if I have an instance of the
// expression `x[0]` which is then auto-slice'd, there would be two
// potential entries in the root map, both with the id of the `x[0]`
// expression. The entry with `derefs==0` refers to the deref of `x`
// used as part of evaluating `x[0]`. The entry with `derefs==1`
// refers to the deref of the `x[0]` that occurs as part of the
// auto-slice.
#[deriving(Eq, IterBytes)]
pub struct root_map_key {
id: ast::NodeId,
Expand Down
7 changes: 2 additions & 5 deletions src/librustc/middle/trans/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,6 @@ fn match_datum(bcx: &Block,

fn extract_vec_elems<'a>(
bcx: &'a Block<'a>,
pat_span: Span,
pat_id: ast::NodeId,
elem_count: uint,
slice: Option<uint>,
Expand All @@ -1041,7 +1040,7 @@ fn extract_vec_elems<'a>(
-> ExtractedBlock<'a> {
let _icx = push_ctxt("match::extract_vec_elems");
let vec_datum = match_datum(bcx, val, pat_id);
let (bcx, base, len) = vec_datum.get_vec_base_and_len(bcx, pat_span, pat_id, 0);
let (base, len) = vec_datum.get_vec_base_and_len(bcx);
let vt = tvec::vec_types(bcx, node_id_type(bcx, pat_id));

let mut elems = vec::from_fn(elem_count, |i| {
Expand Down Expand Up @@ -1512,13 +1511,11 @@ fn compile_submatch_continue<'r,
vals.slice(col + 1u, vals.len()));
let ccx = bcx.fcx.ccx;
let mut pat_id = 0;
let mut pat_span = DUMMY_SP;
for br in m.iter() {
// Find a real id (we're adding placeholder wildcard patterns, but
// each column is guaranteed to have at least one real pattern)
if pat_id == 0 {
pat_id = br.pats[col].id;
pat_span = br.pats[col].span;
}
}

Expand Down Expand Up @@ -1767,7 +1764,7 @@ fn compile_submatch_continue<'r,
vec_len_ge(i) => (n + 1u, Some(i)),
vec_len_eq => (n, None)
};
let args = extract_vec_elems(opt_cx, pat_span, pat_id, n,
let args = extract_vec_elems(opt_cx, pat_id, n,
slice, val, test_val);
size = args.vals.len();
unpacked = args.vals.clone();
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ pub fn malloc_raw_dyn<'a>(
None);
rslt(r.bcx, PointerCast(r.bcx, r.val, llty_value.ptr_to()))
} else {
// we treat ~fn, @fn and @[] as @ here, which isn't ideal
// we treat ~fn as @ here, which isn't ideal
let langcall = match heap {
heap_managed => {
require_alloc_fn(bcx, t, MallocFnLangItem)
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/controlflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ pub fn trans_fail_expr<'a>(
unpack_datum!(bcx, expr::trans_to_lvalue(bcx, arg_expr, "fail"));

if ty::type_is_str(arg_datum.ty) {
let (lldata, _) = arg_datum.get_vec_base_and_len_no_root(bcx);
let (lldata, _) = arg_datum.get_vec_base_and_len(bcx);
return trans_fail_value(bcx, sp_opt, lldata);
} else if bcx.unreachable.get() || ty::type_is_bot(arg_datum.ty) {
return bcx;
Expand Down
45 changes: 2 additions & 43 deletions src/librustc/middle/trans/datum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,49 +528,8 @@ impl Datum<Lvalue> {
}
}

pub fn get_vec_base_and_byte_len<'a>(
&self,
mut bcx: &'a Block<'a>,
span: Span,
expr_id: ast::NodeId,
derefs: uint)
-> (&'a Block<'a>, ValueRef, ValueRef) {
//! Converts a vector into the slice pair. Performs rooting
//! and write guards checks.
// only imp't for @[] and @str, but harmless
bcx = write_guard::root_and_write_guard(self, bcx, span, expr_id, derefs);
let (base, len) = self.get_vec_base_and_byte_len_no_root(bcx);
(bcx, base, len)
}

pub fn get_vec_base_and_byte_len_no_root(&self, bcx: &Block)
-> (ValueRef, ValueRef) {
//! Converts a vector into the slice pair. Des not root
//! nor perform write guard checks.
tvec::get_base_and_byte_len(bcx, self.val, self.ty)
}

pub fn get_vec_base_and_len<'a>(&self,
mut bcx: &'a Block<'a>,
span: Span,
expr_id: ast::NodeId,
derefs: uint)
-> (&'a Block<'a>, ValueRef, ValueRef) {
//! Converts a vector into the slice pair. Performs rooting
//! and write guards checks.
// only imp't for @[] and @str, but harmless
bcx = write_guard::root_and_write_guard(self, bcx, span, expr_id, derefs);
let (base, len) = self.get_vec_base_and_len_no_root(bcx);
(bcx, base, len)
}

pub fn get_vec_base_and_len_no_root<'a>(&self, bcx: &'a Block<'a>)
-> (ValueRef, ValueRef) {
//! Converts a vector into the slice pair. Des not root
//! nor perform write guard checks.
pub fn get_vec_base_and_len<'a>(&self, bcx: &'a Block<'a>) -> (ValueRef, ValueRef) {
//! Converts a vector into the slice pair.
tvec::get_base_and_len(bcx, self.val, self.ty)
}
Expand Down
19 changes: 6 additions & 13 deletions src/librustc/middle/trans/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,10 @@ fn apply_adjustments<'a>(bcx: &'a Block<'a>,
unpack_datum!(bcx, auto_ref(bcx, datum, expr))
}
Some(AutoBorrowVec(..)) => {
unpack_datum!(bcx, auto_slice(bcx, adj.autoderefs,
expr, datum))
unpack_datum!(bcx, auto_slice(bcx, expr, datum))
}
Some(AutoBorrowVecRef(..)) => {
unpack_datum!(bcx, auto_slice_and_ref(bcx, adj.autoderefs,
expr, datum))
unpack_datum!(bcx, auto_slice_and_ref(bcx, expr, datum))
}
Some(AutoBorrowFn(..)) => {
let adjusted_ty = ty::adjust_ty(bcx.tcx(), expr.span,
Expand Down Expand Up @@ -271,7 +269,6 @@ fn apply_adjustments<'a>(bcx: &'a Block<'a>,

fn auto_slice<'a>(
bcx: &'a Block<'a>,
autoderefs: uint,
expr: &ast::Expr,
datum: Datum<Expr>)
-> DatumBlock<'a, Expr> {
Expand All @@ -290,8 +287,7 @@ fn apply_adjustments<'a>(bcx: &'a Block<'a>,
let datum = unpack_datum!(
bcx, datum.to_lvalue_datum(bcx, "auto_slice", expr.id));

let (bcx, base, len) =
datum.get_vec_base_and_len(bcx, expr.span, expr.id, autoderefs+1);
let (base, len) = datum.get_vec_base_and_len(bcx);

// this type may have a different region/mutability than the
// real one, but it will have the same runtime representation
Expand Down Expand Up @@ -323,11 +319,10 @@ fn apply_adjustments<'a>(bcx: &'a Block<'a>,

fn auto_slice_and_ref<'a>(
bcx: &'a Block<'a>,
autoderefs: uint,
expr: &ast::Expr,
datum: Datum<Expr>)
-> DatumBlock<'a, Expr> {
let DatumBlock { bcx, datum } = auto_slice(bcx, autoderefs, expr, datum);
let DatumBlock { bcx, datum } = auto_slice(bcx, expr, datum);
auto_ref(bcx, datum, expr)
}

Expand Down Expand Up @@ -522,8 +517,7 @@ fn trans_datum_unadjusted<'a>(bcx: &'a Block<'a>,
ast::ExprVstore(contents, ast::ExprVstoreUniq) => {
fcx.push_ast_cleanup_scope(contents.id);
let datum = unpack_datum!(
bcx, tvec::trans_uniq_or_managed_vstore(bcx, heap_exchange,
expr, contents));
bcx, tvec::trans_uniq_vstore(bcx, expr, contents));
bcx = fcx.pop_and_trans_ast_cleanup_scope(bcx, contents.id);
DatumBlock(bcx, datum)
}
Expand Down Expand Up @@ -626,8 +620,7 @@ fn trans_index<'a>(bcx: &'a Block<'a>,
let vt = tvec::vec_types(bcx, base_datum.ty);
base::maybe_name_value(bcx.ccx(), vt.llunit_size, "unit_sz");

let (bcx, base, len) =
base_datum.get_vec_base_and_len(bcx, index_expr.span, index_expr.id, 0);
let (base, len) = base_datum.get_vec_base_and_len(bcx);

debug!("trans_index: base {}", bcx.val_to_str(base));
debug!("trans_index: len {}", bcx.val_to_str(len));
Expand Down
83 changes: 37 additions & 46 deletions src/librustc/middle/trans/tvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,10 @@ pub fn alloc_uniq_raw<'a>(
alloc_raw(bcx, unit_ty, fill, alloc, heap_exchange)
}

pub fn alloc_vec<'a>(
pub fn alloc_uniq_vec<'a>(
bcx: &'a Block<'a>,
unit_ty: ty::t,
elts: uint,
heap: heap)
elts: uint)
-> Result<'a> {
let _icx = push_ctxt("tvec::alloc_uniq");
let ccx = bcx.ccx();
Expand All @@ -125,7 +124,7 @@ pub fn alloc_vec<'a>(
let alloc = if elts < 4u { Mul(bcx, C_int(ccx, 4), unit_sz) }
else { fill };
let Result {bcx: bcx, val: vptr} =
alloc_raw(bcx, unit_ty, fill, alloc, heap);
alloc_raw(bcx, unit_ty, fill, alloc, heap_exchange);
return rslt(bcx, vptr);
}

Expand Down Expand Up @@ -302,70 +301,62 @@ pub fn trans_lit_str<'a>(
}


pub fn trans_uniq_or_managed_vstore<'a>(bcx: &'a Block<'a>,
heap: heap,
vstore_expr: &ast::Expr,
content_expr: &ast::Expr)
-> DatumBlock<'a, Expr> {
pub fn trans_uniq_vstore<'a>(bcx: &'a Block<'a>,
vstore_expr: &ast::Expr,
content_expr: &ast::Expr)
-> DatumBlock<'a, Expr> {
/*!
* @[...] or ~[...] (also @"..." or ~"...") allocate boxes in the
* appropriate heap and write the array elements into them.
* ~[...] and ~"..." allocate boxes in the exchange heap and write
* the array elements into them.
*/

debug!("trans_uniq_or_managed_vstore(vstore_expr={}, heap={:?})",
bcx.expr_to_str(vstore_expr), heap);
debug!("trans_uniq_vstore(vstore_expr={})", bcx.expr_to_str(vstore_expr));
let fcx = bcx.fcx;

// Handle ~"".
match heap {
heap_exchange => {
match content_expr.node {
ast::ExprLit(lit) => {
match lit.node {
ast::LitStr(ref s, _) => {
let llptrval = C_cstr(bcx.ccx(), (*s).clone());
let llptrval = PointerCast(bcx,
llptrval,
Type::i8p());
let llsizeval = C_uint(bcx.ccx(), s.get().len());
let typ = ty::mk_str(bcx.tcx(), ty::vstore_uniq);
let lldestval = rvalue_scratch_datum(bcx,
typ,
"");
let alloc_fn = langcall(bcx,
Some(lit.span),
"",
StrDupUniqFnLangItem);
let bcx = callee::trans_lang_call(
bcx,
alloc_fn,
[ llptrval, llsizeval ],
Some(expr::SaveIn(lldestval.val))).bcx;
return DatumBlock(bcx, lldestval).to_expr_datumblock();
}
_ => {}
}
match content_expr.node {
ast::ExprLit(lit) => {
match lit.node {
ast::LitStr(ref s, _) => {
let llptrval = C_cstr(bcx.ccx(), (*s).clone());
let llptrval = PointerCast(bcx,
llptrval,
Type::i8p());
let llsizeval = C_uint(bcx.ccx(), s.get().len());
let typ = ty::mk_str(bcx.tcx(), ty::vstore_uniq);
let lldestval = rvalue_scratch_datum(bcx,
typ,
"");
let alloc_fn = langcall(bcx,
Some(lit.span),
"",
StrDupUniqFnLangItem);
let bcx = callee::trans_lang_call(
bcx,
alloc_fn,
[ llptrval, llsizeval ],
Some(expr::SaveIn(lldestval.val))).bcx;
return DatumBlock(bcx, lldestval).to_expr_datumblock();
}
_ => {}
}
}
heap_exchange_closure => fail!("vectors use exchange_alloc"),
heap_managed => {}
_ => {}
}

let vt = vec_types_from_expr(bcx, vstore_expr);
let count = elements_required(bcx, content_expr);

let Result {bcx, val} = alloc_vec(bcx, vt.unit_ty, count, heap);
let Result {bcx, val} = alloc_uniq_vec(bcx, vt.unit_ty, count);

// Create a temporary scope lest execution should fail while
// constructing the vector.
let temp_scope = fcx.push_custom_cleanup_scope();
fcx.schedule_free_value(cleanup::CustomScope(temp_scope), val, heap);
fcx.schedule_free_value(cleanup::CustomScope(temp_scope), val, heap_exchange);

let dataptr = get_dataptr(bcx, val);

debug!("alloc_vec() returned val={}, dataptr={}",
debug!("alloc_uniq_vec() returned val={}, dataptr={}",
bcx.val_to_str(val), bcx.val_to_str(dataptr));

let bcx = write_content(bcx, &vt, vstore_expr,
Expand Down
9 changes: 4 additions & 5 deletions src/librustc/middle/trans/write_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ fn root<'a, K:KindOps>(datum: &Datum<K>,
_span: Span,
root_key: root_map_key,
root_info: RootInfo) -> &'a Block<'a> {
//! In some cases, borrowck will decide that an @T/@[]/@str
//! value must be rooted for the program to be safe. In that
//! case, we will call this function, which will stash a copy
//! away until we exit the scope `scope_id`.
//! In some cases, borrowck will decide that an @T value must be
//! rooted for the program to be safe. In that case, we will call
//! this function, which will stash a copy away until we exit the
//! scope `scope_id`.
debug!("write_guard::root(root_key={:?}, root_info={:?}, datum={:?})",
root_key, root_info, datum.to_str(bcx.ccx()));
Expand All @@ -62,4 +62,3 @@ fn root<'a, K:KindOps>(datum: &Datum<K>,
cleanup::AstScope(root_info.scope), (),
|(), bcx, llval| datum.shallow_copy_and_take(bcx, llval)).bcx
}

4 changes: 2 additions & 2 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,10 @@ pub enum AutoRef {
/// Convert from T to &T
AutoPtr(Region, ast::Mutability),

/// Convert from @[]/~[]/&[] to &[] (or str)
/// Convert from ~[]/&[] to &[] (or str)
AutoBorrowVec(Region, ast::Mutability),

/// Convert from @[]/~[]/&[] to &&[] (or str)
/// Convert from ~[]/&[] to &&[] (or str)
AutoBorrowVecRef(Region, ast::Mutability),

/// Convert from @fn()/~fn()/|| to ||
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1291,7 +1291,7 @@ impl Parser {
}

// other things are parsed as @/~ + a type. Note that constructs like
// @[] and @str will be resolved during typeck to slices and so forth,
// ~[] and ~str will be resolved during typeck to slices and so forth,
// rather than boxed ptrs. But the special casing of str/vec is not
// reflected in the AST type.
if sigil == OwnedSigil {
Expand Down

5 comments on commit 2125074

@bors
Copy link
Contributor

@bors bors commented on 2125074 Feb 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from pcwalton
at huonw@2125074

@bors
Copy link
Contributor

@bors bors commented on 2125074 Feb 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging huonw/rust/no-at-vec = 2125074 into auto

@bors
Copy link
Contributor

@bors bors commented on 2125074 Feb 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huonw/rust/no-at-vec = 2125074 merged ok, testing candidate = 2bcd951

@bors
Copy link
Contributor

@bors bors commented on 2125074 Feb 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 2125074 Feb 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 2bcd951

Please sign in to comment.