just check whether a variable is initialized

Don't iterate over all things that are initialized.
This commit is contained in:
Niko Matsakis 2018-08-10 18:05:01 -04:00
parent a8a982bb61
commit 78e987ab8f
2 changed files with 25 additions and 30 deletions

View File

@ -1264,7 +1264,12 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
if let &Place::Local(local) = place_span.0 { if let &Place::Local(local) = place_span.0 {
if let Mutability::Not = self.mir.local_decls[local].mutability { if let Mutability::Not = self.mir.local_decls[local].mutability {
// check for reassignments to immutable local variables // 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; return;
} }
} }
@ -1575,27 +1580,20 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
fn check_if_reassignment_to_immutable_state( fn check_if_reassignment_to_immutable_state(
&mut self, &mut self,
context: Context, context: Context,
(place, span): (&Place<'tcx>, Span), local: Local,
place_span: (&Place<'tcx>, Span),
flow_state: &Flows<'cx, 'gcx, 'tcx>, flow_state: &Flows<'cx, 'gcx, 'tcx>,
) { ) {
debug!("check_if_reassignment_to_immutable_state({:?})", place); debug!("check_if_reassignment_to_immutable_state({:?})", local);
// 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() { // Check if any of the initializiations of `local` have happened yet:
let init = self.move_data.inits[i]; let mpi = self.move_data.rev_lookup.find_local(local);
let init_place = &self.move_data.move_paths[init.path].place; let init_indices = &self.move_data.init_path_map[mpi];
if places_conflict::places_conflict(self.tcx, self.mir, &init_place, place, Deep) { let first_init_index = init_indices.iter().find(|ii| flow_state.ever_inits.contains(ii));
self.report_illegal_reassignment(context, (place, span), init.span, err_place); if let Some(&init_index) = first_init_index {
break; // 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);
} }
} }

View File

@ -6,26 +6,23 @@ LL | let f = Foo { v: Vec::new() };
LL | f.v.push("cat".to_string()); //~ ERROR cannot borrow LL | f.v.push("cat".to_string()); //~ ERROR cannot borrow
| ^^^ cannot borrow as mutable | ^^^ 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 --> $DIR/issue-35937.rs:26:5
| |
LL | let s = S { x: 42 }; LL | let s = S { x: 42 };
| - | - help: consider changing this to be mutable: `mut s`
| |
| first assignment to `s`
| consider changing this to `mut s`
LL | s.x += 1; //~ ERROR cannot assign 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 --> $DIR/issue-35937.rs:30:5
| |
LL | fn bar(s: S) { 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 LL | s.x += 1; //~ ERROR cannot assign
| ^^^^^^^^ cannot assign to immutable argument | ^^^^^^^^ cannot assign
error: aborting due to 3 previous errors error: aborting due to 3 previous errors
Some errors occurred: E0384, E0596. Some errors occurred: E0594, E0596.
For more information about an error, try `rustc --explain E0384`. For more information about an error, try `rustc --explain E0594`.