Skip to content

Commit 78e987a

Browse files
committed
just check whether a variable is initialized
Don't iterate over all things that are initialized.
1 parent a8a982b commit 78e987a

File tree

2 files changed

+26
-31
lines changed

2 files changed

+26
-31
lines changed

src/librustc_mir/borrow_check/mod.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,7 +1264,12 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
12641264
if let &Place::Local(local) = place_span.0 {
12651265
if let Mutability::Not = self.mir.local_decls[local].mutability {
12661266
// check for reassignments to immutable local variables
1267-
self.check_if_reassignment_to_immutable_state(context, place_span, flow_state);
1267+
self.check_if_reassignment_to_immutable_state(
1268+
context,
1269+
local,
1270+
place_span,
1271+
flow_state,
1272+
);
12681273
return;
12691274
}
12701275
}
@@ -1575,27 +1580,20 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
15751580
fn check_if_reassignment_to_immutable_state(
15761581
&mut self,
15771582
context: Context,
1578-
(place, span): (&Place<'tcx>, Span),
1583+
local: Local,
1584+
place_span: (&Place<'tcx>, Span),
15791585
flow_state: &Flows<'cx, 'gcx, 'tcx>,
15801586
) {
1581-
debug!("check_if_reassignment_to_immutable_state({:?})", place);
1582-
// determine if this path has a non-mut owner (and thus needs checking).
1583-
let err_place = match self.is_mutable(place, LocalMutationIsAllowed::No) {
1584-
Ok(..) => return,
1585-
Err(place) => place,
1586-
};
1587-
debug!(
1588-
"check_if_reassignment_to_immutable_state({:?}) - is an imm local",
1589-
place
1590-
);
1591-
1592-
for i in flow_state.ever_inits.iter_incoming() {
1593-
let init = self.move_data.inits[i];
1594-
let init_place = &self.move_data.move_paths[init.path].place;
1595-
if places_conflict::places_conflict(self.tcx, self.mir, &init_place, place, Deep) {
1596-
self.report_illegal_reassignment(context, (place, span), init.span, err_place);
1597-
break;
1598-
}
1587+
debug!("check_if_reassignment_to_immutable_state({:?})", local);
1588+
1589+
// Check if any of the initializiations of `local` have happened yet:
1590+
let mpi = self.move_data.rev_lookup.find_local(local);
1591+
let init_indices = &self.move_data.init_path_map[mpi];
1592+
let first_init_index = init_indices.iter().find(|ii| flow_state.ever_inits.contains(ii));
1593+
if let Some(&init_index) = first_init_index {
1594+
// And, if so, report an error.
1595+
let init = &self.move_data.inits[init_index];
1596+
self.report_illegal_reassignment(context, place_span, init.span, place_span.0);
15991597
}
16001598
}
16011599

src/test/ui/did_you_mean/issue-35937.nll.stderr

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,23 @@ LL | let f = Foo { v: Vec::new() };
66
LL | f.v.push("cat".to_string()); //~ ERROR cannot borrow
77
| ^^^ cannot borrow as mutable
88

9-
error[E0384]: cannot assign twice to immutable variable `s`
9+
error[E0594]: cannot assign to `s.x`, as `s` is not declared as mutable
1010
--> $DIR/issue-35937.rs:26:5
1111
|
1212
LL | let s = S { x: 42 };
13-
| -
14-
| |
15-
| first assignment to `s`
16-
| consider changing this to `mut s`
13+
| - help: consider changing this to be mutable: `mut s`
1714
LL | s.x += 1; //~ ERROR cannot assign
18-
| ^^^^^^^^ cannot assign twice to immutable variable
15+
| ^^^^^^^^ cannot assign
1916

20-
error[E0384]: cannot assign to immutable argument `s`
17+
error[E0594]: cannot assign to `s.x`, as `s` is not declared as mutable
2118
--> $DIR/issue-35937.rs:30:5
2219
|
2320
LL | fn bar(s: S) {
24-
| - consider changing this to `mut s`
21+
| - help: consider changing this to be mutable: `mut s`
2522
LL | s.x += 1; //~ ERROR cannot assign
26-
| ^^^^^^^^ cannot assign to immutable argument
23+
| ^^^^^^^^ cannot assign
2724

2825
error: aborting due to 3 previous errors
2926

30-
Some errors occurred: E0384, E0596.
31-
For more information about an error, try `rustc --explain E0384`.
27+
Some errors occurred: E0594, E0596.
28+
For more information about an error, try `rustc --explain E0594`.

0 commit comments

Comments
 (0)