Skip to content

Commit 7857314

Browse files
committed
auto merge of #9989 : luqmana/rust/mut-everywhere, r=alexcrichton
2 parents 22a5ebd + b2b2095 commit 7857314

File tree

24 files changed

+188
-56
lines changed

24 files changed

+188
-56
lines changed

doc/rust.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3395,16 +3395,23 @@ a [temporary](#lvalues-rvalues-and-temporaries), or a local variable.
33953395
A _local variable_ (or *stack-local* allocation) holds a value directly,
33963396
allocated within the stack's memory. The value is a part of the stack frame.
33973397

3398-
Local variables are immutable unless declared with `let mut`. The
3399-
`mut` keyword applies to all local variables declared within that
3400-
declaration (so `let mut (x, y) = ...` declares two mutable variables, `x` and
3401-
`y`).
3398+
Local variables are immutable unless declared otherwise like: `let mut x = ...`.
34023399

34033400
Function parameters are immutable unless declared with `mut`. The
34043401
`mut` keyword applies only to the following parameter (so `|mut x, y|`
34053402
and `fn f(mut x: ~int, y: ~int)` declare one mutable variable `x` and
34063403
one immutable variable `y`).
34073404

3405+
Methods that take either `self` or `~self` can optionally place them in a
3406+
mutable slot by prefixing them with `mut` (similar to regular arguments):
3407+
3408+
~~~
3409+
trait Changer {
3410+
fn change(mut self) -> Self;
3411+
fn modify(mut ~self) -> ~Self;
3412+
}
3413+
~~~
3414+
34083415
Local variables are not initialized when allocated; the entire frame worth of
34093416
local variables are allocated at once, on frame-entry, in an uninitialized
34103417
state. Subsequent statements within a function may or may not initialize the

src/librustc/metadata/decoder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -975,9 +975,9 @@ fn get_explicit_self(item: ebml::Doc) -> ast::explicit_self_ {
975975
let explicit_self_kind = string[0];
976976
match explicit_self_kind as char {
977977
's' => { return ast::sty_static; }
978-
'v' => { return ast::sty_value; }
978+
'v' => { return ast::sty_value(get_mutability(string[1])); }
979979
'@' => { return ast::sty_box(get_mutability(string[1])); }
980-
'~' => { return ast::sty_uniq; }
980+
'~' => { return ast::sty_uniq(get_mutability(string[1])); }
981981
'&' => {
982982
// FIXME(#4846) expl. region
983983
return ast::sty_region(None, get_mutability(string[1]));

src/librustc/metadata/encoder.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,9 @@ fn encode_explicit_self(ebml_w: &mut writer::Encoder, explicit_self: ast::explic
662662
sty_static => {
663663
ebml_w.writer.write(&[ 's' as u8 ]);
664664
}
665-
sty_value => {
665+
sty_value(m) => {
666666
ebml_w.writer.write(&[ 'v' as u8 ]);
667+
encode_mutability(ebml_w, m);
667668
}
668669
sty_region(_, m) => {
669670
// FIXME(#4846) encode custom lifetime
@@ -674,8 +675,9 @@ fn encode_explicit_self(ebml_w: &mut writer::Encoder, explicit_self: ast::explic
674675
ebml_w.writer.write(&[ '@' as u8 ]);
675676
encode_mutability(ebml_w, m);
676677
}
677-
sty_uniq => {
678+
sty_uniq(m) => {
678679
ebml_w.writer.write(&[ '~' as u8 ]);
680+
encode_mutability(ebml_w, m);
679681
}
680682
}
681683

src/librustc/middle/astencode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ impl tr for ast::Def {
410410
ast::DefMethod(did0.tr(xcx), did1.map(|did1| did1.tr(xcx)))
411411
}
412412
ast::DefSelfTy(nid) => { ast::DefSelfTy(xcx.tr_id(nid)) }
413-
ast::DefSelf(nid) => { ast::DefSelf(xcx.tr_id(nid)) }
413+
ast::DefSelf(nid, m) => { ast::DefSelf(xcx.tr_id(nid), m) }
414414
ast::DefMod(did) => { ast::DefMod(did.tr(xcx)) }
415415
ast::DefForeignMod(did) => { ast::DefForeignMod(did.tr(xcx)) }
416416
ast::DefStatic(did, m) => { ast::DefStatic(did.tr(xcx), m) }

src/librustc/middle/liveness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ fn visit_fn(v: &mut LivenessVisitor,
392392
match *fk {
393393
visit::fk_method(_, _, method) => {
394394
match method.explicit_self.node {
395-
sty_value | sty_region(*) | sty_box(_) | sty_uniq => {
395+
sty_value(_) | sty_region(*) | sty_box(_) | sty_uniq(_) => {
396396
fn_maps.add_variable(Arg(method.self_id,
397397
special_idents::self_));
398398
}

src/librustc/middle/mem_categorization.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,12 +488,12 @@ impl mem_categorization_ctxt {
488488
}
489489
}
490490

491-
ast::DefSelf(self_id) => {
491+
ast::DefSelf(self_id, mutbl) => {
492492
@cmt_ {
493493
id:id,
494494
span:span,
495495
cat:cat_self(self_id),
496-
mutbl: McImmutable,
496+
mutbl: if mutbl { McDeclared } else { McImmutable },
497497
ty:expr_ty
498498
}
499499
}

src/librustc/middle/moves.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ pub fn moved_variable_node_id_from_def(def: Def) -> Option<NodeId> {
227227
DefBinding(nid, _) |
228228
DefArg(nid, _) |
229229
DefLocal(nid, _) |
230-
DefSelf(nid) => Some(nid),
230+
DefSelf(nid, _) => Some(nid),
231231

232232
_ => None
233233
}

src/librustc/middle/resolve.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ enum Mutability {
150150

151151
enum SelfBinding {
152152
NoSelfBinding,
153-
HasSelfBinding(NodeId)
153+
HasSelfBinding(NodeId, explicit_self)
154154
}
155155

156156
impl Visitor<()> for Resolver {
@@ -3799,8 +3799,12 @@ impl Resolver {
37993799
NoSelfBinding => {
38003800
// Nothing to do.
38013801
}
3802-
HasSelfBinding(self_node_id) => {
3803-
let def_like = DlDef(DefSelf(self_node_id));
3802+
HasSelfBinding(self_node_id, explicit_self) => {
3803+
let mutable = match explicit_self.node {
3804+
sty_uniq(m) | sty_value(m) if m == MutMutable => true,
3805+
_ => false
3806+
};
3807+
let def_like = DlDef(DefSelf(self_node_id, mutable));
38043808
*function_value_rib.self_binding = Some(def_like);
38053809
}
38063810
}
@@ -3937,7 +3941,7 @@ impl Resolver {
39373941
// we only have self ty if it is a non static method
39383942
let self_binding = match method.explicit_self.node {
39393943
sty_static => { NoSelfBinding }
3940-
_ => { HasSelfBinding(method.self_id) }
3944+
_ => { HasSelfBinding(method.self_id, method.explicit_self) }
39413945
};
39423946

39433947
self.resolve_function(rib_kind,

src/librustc/middle/trans/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ pub fn trans_local_var(bcx: @mut Block, def: ast::Def) -> Datum {
10991099
ast::DefLocal(nid, _) | ast::DefBinding(nid, _) => {
11001100
take_local(bcx, bcx.fcx.lllocals, nid)
11011101
}
1102-
ast::DefSelf(nid) => {
1102+
ast::DefSelf(nid, _) => {
11031103
let self_info: ValSelfData = match bcx.fcx.llself {
11041104
Some(ref self_info) => *self_info,
11051105
None => {

src/librustc/middle/trans/inline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ pub fn maybe_instantiate_inline(ccx: @mut CrateContext, fn_id: ast::DefId)
144144
debug!("calling inline trans_fn with self_ty {}",
145145
ty_to_str(ccx.tcx, self_ty));
146146
match mth.explicit_self.node {
147-
ast::sty_value => impl_self(self_ty, ty::ByRef),
147+
ast::sty_value(_) => impl_self(self_ty, ty::ByRef),
148148
_ => impl_self(self_ty, ty::ByCopy),
149149
}
150150
}

0 commit comments

Comments
 (0)