From 78e987ab8f71c733ab1081018492f87ba47e9da5 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Fri, 10 Aug 2018 18:05:01 -0400 Subject: [PATCH] just check whether a variable is initialized Don't iterate over all things that are initialized. --- src/librustc_mir/borrow_check/mod.rs | 36 +++++++++---------- .../ui/did_you_mean/issue-35937.nll.stderr | 19 +++++----- 2 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/librustc_mir/borrow_check/mod.rs b/src/librustc_mir/borrow_check/mod.rs index 75e9b5f2df0..ce0e76a636d 100644 --- a/src/librustc_mir/borrow_check/mod.rs +++ b/src/librustc_mir/borrow_check/mod.rs @@ -1264,7 +1264,12 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { if let &Place::Local(local) = place_span.0 { if let Mutability::Not = self.mir.local_decls[local].mutability { // check for reassignments to immutable local variables - self.check_if_reassignment_to_immutable_state(context, place_span, flow_state); + self.check_if_reassignment_to_immutable_state( + context, + local, + place_span, + flow_state, + ); return; } } @@ -1575,27 +1580,20 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { fn check_if_reassignment_to_immutable_state( &mut self, context: Context, - (place, span): (&Place<'tcx>, Span), + local: Local, + place_span: (&Place<'tcx>, Span), flow_state: &Flows<'cx, 'gcx, 'tcx>, ) { - debug!("check_if_reassignment_to_immutable_state({:?})", place); - // determine if this path has a non-mut owner (and thus needs checking). - let err_place = match self.is_mutable(place, LocalMutationIsAllowed::No) { - Ok(..) => return, - Err(place) => place, - }; - debug!( - "check_if_reassignment_to_immutable_state({:?}) - is an imm local", - place - ); + debug!("check_if_reassignment_to_immutable_state({:?})", local); - for i in flow_state.ever_inits.iter_incoming() { - let init = self.move_data.inits[i]; - let init_place = &self.move_data.move_paths[init.path].place; - if places_conflict::places_conflict(self.tcx, self.mir, &init_place, place, Deep) { - self.report_illegal_reassignment(context, (place, span), init.span, err_place); - break; - } + // Check if any of the initializiations of `local` have happened yet: + let mpi = self.move_data.rev_lookup.find_local(local); + let init_indices = &self.move_data.init_path_map[mpi]; + let first_init_index = init_indices.iter().find(|ii| flow_state.ever_inits.contains(ii)); + if let Some(&init_index) = first_init_index { + // And, if so, report an error. + let init = &self.move_data.inits[init_index]; + self.report_illegal_reassignment(context, place_span, init.span, place_span.0); } } diff --git a/src/test/ui/did_you_mean/issue-35937.nll.stderr b/src/test/ui/did_you_mean/issue-35937.nll.stderr index 804e5f0531f..34bdf48e2a6 100644 --- a/src/test/ui/did_you_mean/issue-35937.nll.stderr +++ b/src/test/ui/did_you_mean/issue-35937.nll.stderr @@ -6,26 +6,23 @@ LL | let f = Foo { v: Vec::new() }; LL | f.v.push("cat".to_string()); //~ ERROR cannot borrow | ^^^ cannot borrow as mutable -error[E0384]: cannot assign twice to immutable variable `s` +error[E0594]: cannot assign to `s.x`, as `s` is not declared as mutable --> $DIR/issue-35937.rs:26:5 | LL | let s = S { x: 42 }; - | - - | | - | first assignment to `s` - | consider changing this to `mut s` + | - help: consider changing this to be mutable: `mut s` LL | s.x += 1; //~ ERROR cannot assign - | ^^^^^^^^ cannot assign twice to immutable variable + | ^^^^^^^^ cannot assign -error[E0384]: cannot assign to immutable argument `s` +error[E0594]: cannot assign to `s.x`, as `s` is not declared as mutable --> $DIR/issue-35937.rs:30:5 | LL | fn bar(s: S) { - | - consider changing this to `mut s` + | - help: consider changing this to be mutable: `mut s` LL | s.x += 1; //~ ERROR cannot assign - | ^^^^^^^^ cannot assign to immutable argument + | ^^^^^^^^ cannot assign error: aborting due to 3 previous errors -Some errors occurred: E0384, E0596. -For more information about an error, try `rustc --explain E0384`. +Some errors occurred: E0594, E0596. +For more information about an error, try `rustc --explain E0594`.