Skip to content

Commit 16b8a41

Browse files
committed
auto merge of #10026 : luqmana/rust/mut-pat, r=pcwalton
Fixes #9792.
2 parents 950add4 + 523a28d commit 16b8a41

24 files changed

+224
-168
lines changed

src/librustc/middle/borrowck/gather_loans/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ impl<'self> GatherLoanCtxt<'self> {
730730
loan_mutability,
731731
scope_r);
732732
}
733-
ast::BindInfer => {
733+
ast::BindByValue(_) => {
734734
// No borrows here, but there may be moves
735735
if self.bccx.is_move(pat.id) {
736736
gather_moves::gather_move_from_pat(

src/librustc/middle/check_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ fn check_legality_of_move_bindings(cx: &MatchCheckCtxt,
884884
BindByRef(_) => {
885885
by_ref_span = Some(span);
886886
}
887-
BindInfer => {
887+
BindByValue(_) => {
888888
if cx.moves_map.contains(&id) {
889889
any_by_move = true;
890890
}

src/librustc/middle/kind.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -432,14 +432,12 @@ fn is_nullary_variant(cx: &Context, ex: @Expr) -> bool {
432432

433433
fn check_imm_free_var(cx: &Context, def: Def, sp: Span) {
434434
match def {
435-
DefLocal(_, is_mutbl) => {
436-
if is_mutbl {
437-
cx.tcx.sess.span_err(
438-
sp,
439-
"mutable variables cannot be implicitly captured");
440-
}
435+
DefLocal(_, BindByValue(MutMutable)) => {
436+
cx.tcx.sess.span_err(
437+
sp,
438+
"mutable variables cannot be implicitly captured");
441439
}
442-
DefArg(*) => { /* ok */ }
440+
DefLocal(*) | DefArg(*) => { /* ok */ }
443441
DefUpvar(_, def1, _, _) => { check_imm_free_var(cx, *def1, sp); }
444442
DefBinding(*) | DefSelf(*) => { /*ok*/ }
445443
_ => {

src/librustc/middle/lint.rs

+19-41
Original file line numberDiff line numberDiff line change
@@ -813,27 +813,24 @@ fn check_unused_unsafe(cx: &Context, e: &ast::Expr) {
813813
}
814814

815815
fn check_unused_mut_pat(cx: &Context, p: @ast::Pat) {
816-
let mut used = false;
817-
let mut bindings = 0;
818-
do pat_util::pat_bindings(cx.tcx.def_map, p) |_, id, _, _| {
819-
used = used || cx.tcx.used_mut_nodes.contains(&id);
820-
bindings += 1;
821-
}
822-
if !used {
823-
let msg = if bindings == 1 {
824-
"variable does not need to be mutable"
825-
} else {
826-
"variables do not need to be mutable"
827-
};
828-
cx.span_lint(unused_mut, p.span, msg);
829-
}
830-
}
831-
832-
fn check_unused_mut_fn_decl(cx: &Context, fd: &ast::fn_decl) {
833-
for arg in fd.inputs.iter() {
834-
if arg.is_mutbl {
835-
check_unused_mut_pat(cx, arg.pat);
816+
match p.node {
817+
ast::PatIdent(ast::BindByValue(ast::MutMutable), _, _) => {
818+
let mut used = false;
819+
let mut bindings = 0;
820+
do pat_util::pat_bindings(cx.tcx.def_map, p) |_, id, _, _| {
821+
used = used || cx.tcx.used_mut_nodes.contains(&id);
822+
bindings += 1;
823+
}
824+
if !used {
825+
let msg = if bindings == 1 {
826+
"variable does not need to be mutable"
827+
} else {
828+
"variables do not need to be mutable"
829+
};
830+
cx.span_lint(unused_mut, p.span, msg);
831+
}
836832
}
833+
_ => ()
837834
}
838835
}
839836

@@ -1075,6 +1072,8 @@ impl Visitor<()> for Context {
10751072

10761073
fn visit_pat(&mut self, p: @ast::Pat, _: ()) {
10771074
check_pat_non_uppercase_statics(self, p);
1075+
check_unused_mut_pat(self, p);
1076+
10781077
visit::walk_pat(self, p, ());
10791078
}
10801079

@@ -1095,30 +1094,9 @@ impl Visitor<()> for Context {
10951094
visit::walk_stmt(self, s, ());
10961095
}
10971096

1098-
fn visit_ty_method(&mut self, tm: &ast::TypeMethod, _: ()) {
1099-
check_unused_mut_fn_decl(self, &tm.decl);
1100-
visit::walk_ty_method(self, tm, ());
1101-
}
1102-
1103-
fn visit_trait_method(&mut self, tm: &ast::trait_method, _: ()) {
1104-
match *tm {
1105-
ast::required(ref m) => check_unused_mut_fn_decl(self, &m.decl),
1106-
ast::provided(ref m) => check_unused_mut_fn_decl(self, &m.decl)
1107-
}
1108-
visit::walk_trait_method(self, tm, ());
1109-
}
1110-
1111-
fn visit_local(&mut self, l: @ast::Local, _: ()) {
1112-
if l.is_mutbl {
1113-
check_unused_mut_pat(self, l.pat);
1114-
}
1115-
visit::walk_local(self, l, ());
1116-
}
1117-
11181097
fn visit_fn(&mut self, fk: &visit::fn_kind, decl: &ast::fn_decl,
11191098
body: &ast::Block, span: Span, id: ast::NodeId, _: ()) {
11201099
let recurse = |this: &mut Context| {
1121-
check_unused_mut_fn_decl(this, decl);
11221100
visit::walk_fn(this, fk, decl, body, span, id, ());
11231101
};
11241102

src/librustc/middle/liveness.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -429,18 +429,22 @@ fn visit_fn(v: &mut LivenessVisitor,
429429

430430
fn visit_local(v: &mut LivenessVisitor, local: @Local, this: @mut IrMaps) {
431431
let def_map = this.tcx.def_map;
432-
do pat_util::pat_bindings(def_map, local.pat) |_bm, p_id, sp, path| {
432+
do pat_util::pat_bindings(def_map, local.pat) |bm, p_id, sp, path| {
433433
debug!("adding local variable {}", p_id);
434434
let name = ast_util::path_to_ident(path);
435435
this.add_live_node_for_node(p_id, VarDefNode(sp));
436436
let kind = match local.init {
437437
Some(_) => FromLetWithInitializer,
438438
None => FromLetNoInitializer
439439
};
440+
let mutbl = match bm {
441+
BindByValue(MutMutable) => true,
442+
_ => false
443+
};
440444
this.add_variable(Local(LocalInfo {
441445
id: p_id,
442446
ident: name,
443-
is_mutbl: local.is_mutbl,
447+
is_mutbl: mutbl,
444448
kind: kind
445449
}));
446450
}
@@ -454,11 +458,15 @@ fn visit_arm(v: &mut LivenessVisitor, arm: &Arm, this: @mut IrMaps) {
454458
debug!("adding local variable {} from match with bm {:?}",
455459
p_id, bm);
456460
let name = ast_util::path_to_ident(path);
461+
let mutbl = match bm {
462+
BindByValue(MutMutable) => true,
463+
_ => false
464+
};
457465
this.add_live_node_for_node(p_id, VarDefNode(sp));
458466
this.add_variable(Local(LocalInfo {
459467
id: p_id,
460468
ident: name,
461-
is_mutbl: false,
469+
is_mutbl: mutbl,
462470
kind: FromMatch(bm)
463471
}));
464472
}

src/librustc/middle/mem_categorization.rs

+17-19
Original file line numberDiff line numberDiff line change
@@ -473,12 +473,15 @@ impl mem_categorization_ctxt {
473473
}
474474
}
475475

476-
ast::DefArg(vid, mutbl) => {
476+
ast::DefArg(vid, binding_mode) => {
477477
// Idea: make this could be rewritten to model by-ref
478478
// stuff as `&const` and `&mut`?
479479

480480
// m: mutability of the argument
481-
let m = if mutbl {McDeclared} else {McImmutable};
481+
let m = match binding_mode {
482+
ast::BindByValue(ast::MutMutable) => McDeclared,
483+
_ => McImmutable
484+
};
482485
@cmt_ {
483486
id: id,
484487
span: span,
@@ -548,25 +551,20 @@ impl mem_categorization_ctxt {
548551
}
549552
}
550553

551-
ast::DefLocal(vid, mutbl) => {
552-
let m = if mutbl {McDeclared} else {McImmutable};
553-
@cmt_ {
554-
id:id,
555-
span:span,
556-
cat:cat_local(vid),
557-
mutbl:m,
558-
ty:expr_ty
559-
}
560-
}
561-
562-
ast::DefBinding(vid, _) => {
554+
ast::DefLocal(vid, binding_mode) |
555+
ast::DefBinding(vid, binding_mode) => {
563556
// by-value/by-ref bindings are local variables
557+
let m = match binding_mode {
558+
ast::BindByValue(ast::MutMutable) => McDeclared,
559+
_ => McImmutable
560+
};
561+
564562
@cmt_ {
565-
id:id,
566-
span:span,
567-
cat:cat_local(vid),
568-
mutbl:McImmutable,
569-
ty:expr_ty
563+
id: id,
564+
span: span,
565+
cat: cat_local(vid),
566+
mutbl: m,
567+
ty: expr_ty
570568
}
571569
}
572570
}

src/librustc/middle/moves.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ impl VisitContext {
618618
do pat_bindings(self.tcx.def_map, pat) |bm, id, _span, path| {
619619
let binding_moves = match bm {
620620
BindByRef(_) => false,
621-
BindInfer => {
621+
BindByValue(_) => {
622622
let pat_ty = ty::node_id_to_type(self.tcx, id);
623623
debug!("pattern {:?} {} type is {}",
624624
id,

src/librustc/middle/resolve.rs

+5-14
Original file line numberDiff line numberDiff line change
@@ -3817,11 +3817,8 @@ impl Resolver {
38173817
Some(declaration) => {
38183818
for argument in declaration.inputs.iter() {
38193819
let binding_mode = ArgumentIrrefutableMode;
3820-
let mutability =
3821-
if argument.is_mutbl {Mutable} else {Immutable};
38223820
this.resolve_pattern(argument.pat,
38233821
binding_mode,
3824-
mutability,
38253822
None);
38263823

38273824
this.resolve_type(&argument.ty);
@@ -4036,8 +4033,6 @@ impl Resolver {
40364033
}
40374034

40384035
fn resolve_local(&mut self, local: @Local) {
4039-
let mutability = if local.is_mutbl {Mutable} else {Immutable};
4040-
40414036
// Resolve the type.
40424037
self.resolve_type(&local.ty);
40434038

@@ -4052,7 +4047,7 @@ impl Resolver {
40524047
}
40534048

40544049
// Resolve the pattern.
4055-
self.resolve_pattern(local.pat, LocalIrrefutableMode, mutability, None);
4050+
self.resolve_pattern(local.pat, LocalIrrefutableMode, None);
40564051
}
40574052

40584053
// build a map from pattern identifiers to binding-info's.
@@ -4116,8 +4111,7 @@ impl Resolver {
41164111

41174112
let bindings_list = @mut HashMap::new();
41184113
for pattern in arm.pats.iter() {
4119-
self.resolve_pattern(*pattern, RefutableMode, Immutable,
4120-
Some(bindings_list));
4114+
self.resolve_pattern(*pattern, RefutableMode, Some(bindings_list));
41214115
}
41224116

41234117
// This has to happen *after* we determine which
@@ -4261,7 +4255,6 @@ impl Resolver {
42614255
fn resolve_pattern(&mut self,
42624256
pattern: @Pat,
42634257
mode: PatternBindingMode,
4264-
mutability: Mutability,
42654258
// Maps idents to the node ID for the (outermost)
42664259
// pattern that binds them
42674260
bindings_list: Option<@mut HashMap<Name,NodeId>>) {
@@ -4324,8 +4317,6 @@ impl Resolver {
43244317
debug!("(resolving pattern) binding `{}`",
43254318
interner_get(renamed));
43264319

4327-
let is_mutable = mutability == Mutable;
4328-
43294320
let def = match mode {
43304321
RefutableMode => {
43314322
// For pattern arms, we must use
@@ -4335,11 +4326,11 @@ impl Resolver {
43354326
}
43364327
LocalIrrefutableMode => {
43374328
// But for locals, we use `def_local`.
4338-
DefLocal(pattern.id, is_mutable)
4329+
DefLocal(pattern.id, binding_mode)
43394330
}
43404331
ArgumentIrrefutableMode => {
43414332
// And for function arguments, `def_arg`.
4342-
DefArg(pattern.id, is_mutable)
4333+
DefArg(pattern.id, binding_mode)
43434334
}
43444335
};
43454336

@@ -5361,7 +5352,7 @@ impl Resolver {
53615352
pat_binding_mode: BindingMode,
53625353
descr: &str) {
53635354
match pat_binding_mode {
5364-
BindInfer => {}
5355+
BindByValue(_) => {}
53655356
BindByRef(*) => {
53665357
self.resolve_error(
53675358
pat.span,

src/librustc/middle/trans/_match.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1842,7 +1842,7 @@ fn create_bindings_map(bcx: @mut Block, pat: @ast::Pat) -> BindingsMap {
18421842
let llmatch;
18431843
let trmode;
18441844
match bm {
1845-
ast::BindInfer => {
1845+
ast::BindByValue(_) => {
18461846
// in this case, the final type of the variable will be T,
18471847
// but during matching we need to store a *T as explained
18481848
// above
@@ -2130,7 +2130,7 @@ fn bind_irrefutable_pat(bcx: @mut Block,
21302130
bcx, pat.id, path, binding_mode,
21312131
|bcx, variable_ty, llvariable_val| {
21322132
match pat_binding_mode {
2133-
ast::BindInfer => {
2133+
ast::BindByValue(_) => {
21342134
// By value binding: move the value that `val`
21352135
// points at into the binding's stack slot.
21362136
let datum = Datum {val: val,
@@ -2241,7 +2241,7 @@ fn bind_irrefutable_pat(bcx: @mut Block,
22412241

22422242
fn simple_identifier<'a>(pat: &'a ast::Pat) -> Option<&'a ast::Path> {
22432243
match pat.node {
2244-
ast::PatIdent(ast::BindInfer, ref path, None) => {
2244+
ast::PatIdent(ast::BindByValue(_), ref path, None) => {
22452245
Some(path)
22462246
}
22472247
_ => {

src/librustc/middle/trans/base.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2065,7 +2065,6 @@ pub fn trans_enum_variant_or_tuple_like_struct<A:IdAndTy>(
20652065
// Translate variant arguments to function arguments.
20662066
let fn_args = do args.map |varg| {
20672067
ast::arg {
2068-
is_mutbl: false,
20692068
ty: (*varg.ty()).clone(),
20702069
pat: ast_util::ident_to_pat(
20712070
ccx.tcx.sess.next_node_id(),

src/librustc/middle/typeck/check/_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ pub fn check_pat(pcx: &pat_ctxt, pat: @ast::Pat, expected: ty::t) {
476476
demand::eqtype(fcx, pat.span, region_ty, typ);
477477
}
478478
// otherwise the type of x is the expected type T
479-
ast::BindInfer => {
479+
ast::BindByValue(_) => {
480480
demand::eqtype(fcx, pat.span, expected, typ);
481481
}
482482
}

src/libsyntax/ast.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ pub enum Def {
232232
DefMod(DefId),
233233
DefForeignMod(DefId),
234234
DefStatic(DefId, bool /* is_mutbl */),
235-
DefArg(NodeId, bool /* is_mutbl */),
236-
DefLocal(NodeId, bool /* is_mutbl */),
235+
DefArg(NodeId, BindingMode),
236+
DefLocal(NodeId, BindingMode),
237237
DefVariant(DefId /* enum */, DefId /* variant */, bool /* is_structure */),
238238
DefTy(DefId),
239239
DefTrait(DefId),
@@ -324,7 +324,7 @@ pub struct FieldPat {
324324
#[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
325325
pub enum BindingMode {
326326
BindByRef(Mutability),
327-
BindInfer
327+
BindByValue(Mutability),
328328
}
329329

330330
#[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
@@ -445,7 +445,6 @@ pub enum Stmt_ {
445445
// a refinement on pat.
446446
#[deriving(Eq, Encodable, Decodable,IterBytes)]
447447
pub struct Local {
448-
is_mutbl: bool,
449448
ty: Ty,
450449
pat: @Pat,
451450
init: Option<@Expr>,
@@ -880,7 +879,6 @@ pub struct inline_asm {
880879

881880
#[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
882881
pub struct arg {
883-
is_mutbl: bool,
884882
ty: Ty,
885883
pat: @Pat,
886884
id: NodeId,

src/libsyntax/ast_util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ pub fn ident_to_path(s: Span, identifier: Ident) -> Path {
234234

235235
pub fn ident_to_pat(id: NodeId, s: Span, i: Ident) -> @Pat {
236236
@ast::Pat { id: id,
237-
node: PatIdent(BindInfer, ident_to_path(s, i), None),
237+
node: PatIdent(BindByValue(MutImmutable), ident_to_path(s, i), None),
238238
span: s }
239239
}
240240

0 commit comments

Comments
 (0)