]> git.lizzy.rs Git - rust.git/commitdiff
just check whether a variable is initialized
authorNiko Matsakis <niko@alum.mit.edu>
Fri, 10 Aug 2018 22:05:01 +0000 (18:05 -0400)
committerNiko Matsakis <niko@alum.mit.edu>
Sun, 19 Aug 2018 14:34:44 +0000 (07:34 -0700)
Don't iterate over all things that are initialized.

src/librustc_mir/borrow_check/mod.rs
src/test/ui/did_you_mean/issue-35937.nll.stderr

index 75e9b5f2df022689d80a7b090e86ebc1c1590a8e..ce0e76a636db2511cb2886ba85c361860039943c 100644 (file)
@@ -1264,7 +1264,12 @@ fn mutate_place(
         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
-        );
-
-        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;
-            }
+        debug!("check_if_reassignment_to_immutable_state({:?})", local);
+
+        // 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);
         }
     }
 
index 804e5f0531f852a5b9976b35139a5b025b27c1c1..34bdf48e2a65f7a319705040244ba0da3942fe0e 100644 (file)
@@ -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`.