Skip to content

Commit 1c348e6

Browse files
committed
librustc: Implement @mut [T] as the new replacement for @[mut T]
1 parent 7bc29c6 commit 1c348e6

File tree

10 files changed

+42
-12
lines changed

10 files changed

+42
-12
lines changed

src/librustc/middle/const_eval.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ fn classify(e: @expr,
9292
ast::expr_vstore_fixed(_) |
9393
ast::expr_vstore_slice => classify(e, def_map, tcx),
9494
ast::expr_vstore_uniq |
95-
ast::expr_vstore_box => non_const
95+
ast::expr_vstore_box |
96+
ast::expr_vstore_mut_box => non_const
9697
}
9798
}
9899

src/librustc/middle/trans/expr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,8 @@ fn trans_rvalue_datum_unadjusted(bcx: block, expr: @ast::expr) -> DatumBlock {
392392
trace_span!(bcx, expr.span, shorten(bcx.expr_to_str(expr)));
393393

394394
match expr.node {
395-
ast::expr_vstore(contents, ast::expr_vstore_box) => {
395+
ast::expr_vstore(contents, ast::expr_vstore_box) |
396+
ast::expr_vstore(contents, ast::expr_vstore_mut_box) => {
396397
return tvec::trans_uniq_or_managed_vstore(bcx, heap_shared,
397398
expr, contents);
398399
}

src/librustc/middle/ty.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3187,6 +3187,7 @@ fn expr_kind(tcx: ctxt,
31873187
ast::expr_addr_of(*) |
31883188
ast::expr_binary(*) |
31893189
ast::expr_vstore(_, ast::expr_vstore_box) |
3190+
ast::expr_vstore(_, ast::expr_vstore_mut_box) |
31903191
ast::expr_vstore(_, ast::expr_vstore_uniq) => {
31913192
RvalueDatumExpr
31923193
}

src/librustc/middle/typeck/astconv.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,14 @@ fn ast_ty_to_ty<AC: ast_conv, RS: region_scope Copy Owned>(
176176
let tcx = self.tcx();
177177

178178
match a_seq_ty.ty.node {
179-
// to convert to an e{vec,str}, there can't be a
180-
// mutability argument
181-
_ if a_seq_ty.mutbl != ast::m_imm => (),
182179
ast::ty_vec(mt) => {
183-
return ty::mk_evec(tcx, ast_mt_to_mt(self, rscope, mt), vst);
180+
let mut mt = ast_mt_to_mt(self, rscope, mt);
181+
if a_seq_ty.mutbl == ast::m_mutbl {
182+
mt = { ty: mt.ty, mutbl: ast::m_mutbl };
183+
}
184+
return ty::mk_evec(tcx, mt, vst);
184185
}
185-
ast::ty_path(path, id) => {
186+
ast::ty_path(path, id) if a_seq_ty.mutbl == ast::m_imm => {
186187
match tcx.def_map.find(id) {
187188
Some(ast::def_prim_ty(ast::ty_str)) => {
188189
check_path_args(tcx, path, NO_TPS | NO_REGIONS);

src/librustc/middle/typeck/check.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,9 +1705,14 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
17051705
}
17061706
ast::expr_vec(args, mutbl) => {
17071707
let tt = ast_expr_vstore_to_vstore(fcx, ev, args.len(), vst);
1708+
let mutability;
1709+
match vst {
1710+
ast::expr_vstore_mut_box => mutability = ast::m_mutbl,
1711+
_ => mutability = mutbl
1712+
}
17081713
let t: ty::t = fcx.infcx().next_ty_var();
17091714
for args.each |e| { bot |= check_expr_with(fcx, *e, t); }
1710-
ty::mk_evec(tcx, {ty: t, mutbl: mutbl}, tt)
1715+
ty::mk_evec(tcx, {ty: t, mutbl: mutability}, tt)
17111716
}
17121717
ast::expr_repeat(element, count_expr, mutbl) => {
17131718
let count = ty::eval_repeat_count(tcx, count_expr, expr.span);
@@ -2721,7 +2726,7 @@ fn ast_expr_vstore_to_vstore(fcx: @fn_ctxt, e: @ast::expr, n: uint,
27212726
ty::vstore_fixed(u)
27222727
}
27232728
ast::expr_vstore_uniq => ty::vstore_uniq,
2724-
ast::expr_vstore_box => ty::vstore_box,
2729+
ast::expr_vstore_box | ast::expr_vstore_mut_box => ty::vstore_box,
27252730
ast::expr_vstore_slice => {
27262731
let r = fcx.infcx().next_region_var(e.span, e.id);
27272732
ty::vstore_slice(r)

src/libsyntax/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ enum expr_vstore {
645645
expr_vstore_fixed(Option<uint>), // [1,2,3,4]
646646
expr_vstore_uniq, // ~[1,2,3,4]
647647
expr_vstore_box, // @[1,2,3,4]
648+
expr_vstore_mut_box, // @mut [1,2,3,4]
648649
expr_vstore_slice // &[1,2,3,4]
649650
}
650651

src/libsyntax/parse/parser.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ use ast::{_mod, add, arg, arm, attribute,
3838
expr_lit, expr_log, expr_loop, expr_loop_body, expr_mac,
3939
expr_paren, expr_path, expr_rec, expr_repeat, expr_ret,
4040
expr_swap, expr_struct, expr_tup, expr_unary, expr_unary_move,
41-
expr_vec, expr_vstore, expr_while, extern_fn, field, fn_decl,
41+
expr_vec, expr_vstore, expr_vstore_mut_box, expr_while,
42+
extern_fn, field, fn_decl,
4243
foreign_item, foreign_item_const, foreign_item_fn, foreign_mod,
4344
ident, impure_fn, infer, inherited,
4445
item, item_, item_class, item_const, item_enum, item_fn,
@@ -1450,8 +1451,11 @@ impl Parser {
14501451
hi = e.span.hi;
14511452
// HACK: turn @[...] into a @-evec
14521453
ex = match e.node {
1453-
expr_vec(*) | expr_lit(@{node: lit_str(_), span: _})
1454-
if m == m_imm => expr_vstore(e, expr_vstore_box),
1454+
expr_vec(*) if m == m_mutbl =>
1455+
expr_vstore(e, expr_vstore_mut_box),
1456+
expr_vec(*) if m == m_imm => expr_vstore(e, expr_vstore_box),
1457+
expr_lit(@{node: lit_str(_), span: _}) if m == m_imm =>
1458+
expr_vstore(e, expr_vstore_box),
14551459
_ => expr_unary(box(m), e)
14561460
};
14571461
}

src/libsyntax/print/pprust.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,10 @@ fn print_expr_vstore(s: ps, t: ast::expr_vstore) {
10271027
ast::expr_vstore_fixed(None) => word(s.s, ~"_"),
10281028
ast::expr_vstore_uniq => word(s.s, ~"~"),
10291029
ast::expr_vstore_box => word(s.s, ~"@"),
1030+
ast::expr_vstore_mut_box => {
1031+
word(s.s, ~"@");
1032+
word(s.s, ~"mut");
1033+
}
10301034
ast::expr_vstore_slice => word(s.s, ~"&"),
10311035
}
10321036
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
let v = @mut [ 1, 2, 3 ];
3+
for v.each |_x| { //~ ERROR illegal borrow
4+
v[1] = 4;
5+
}
6+
}
7+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
let x: @mut [int] = @mut [ 1, 2, 3 ];
3+
4+
}
5+

0 commit comments

Comments
 (0)