Skip to content

Commit d887369

Browse files
committed
Auto merge of #44806 - KiChjang:mir-err-notes-2, r=pnkfelix
Add span label to E0384 for MIR borrowck Corresponds to `report_illegal_reassignment`. Part of #44596.
2 parents f22b9da + 6d4989b commit d887369

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

src/librustc_mir/borrow_check.rs

+24-6
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,19 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
580580
if flow_state.inits.curr_state.contains(&mpi) {
581581
// may already be assigned before reaching this statement;
582582
// report error.
583-
self.report_illegal_reassignment(context, (lvalue, span));
583+
// FIXME: Not ideal, it only finds the assignment that lexically comes first
584+
let assigned_lvalue = &move_data.move_paths[mpi].lvalue;
585+
let assignment_stmt = self.mir.basic_blocks().iter().filter_map(|bb| {
586+
bb.statements.iter().find(|stmt| {
587+
if let StatementKind::Assign(ref lv, _) = stmt.kind {
588+
*lv == *assigned_lvalue
589+
} else {
590+
false
591+
}
592+
})
593+
}).next().unwrap();
594+
self.report_illegal_reassignment(
595+
context, (lvalue, span), assignment_stmt.source_info.span);
584596
}
585597
}
586598
}
@@ -982,11 +994,17 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
982994
err.emit();
983995
}
984996

985-
fn report_illegal_reassignment(&mut self, _context: Context, (lvalue, span): (&Lvalue, Span)) {
986-
let mut err = self.tcx.cannot_reassign_immutable(
987-
span, &self.describe_lvalue(lvalue), Origin::Mir);
988-
// FIXME: add span labels for borrow and assignment points
989-
err.emit();
997+
fn report_illegal_reassignment(&mut self,
998+
_context: Context,
999+
(lvalue, span): (&Lvalue, Span),
1000+
assigned_span: Span) {
1001+
self.tcx.cannot_reassign_immutable(span,
1002+
&self.describe_lvalue(lvalue),
1003+
Origin::Mir)
1004+
.span_label(span, "re-assignment of immutable variable")
1005+
.span_label(assigned_span, format!("first assignment to `{}`",
1006+
self.describe_lvalue(lvalue)))
1007+
.emit();
9901008
}
9911009

9921010
fn report_assignment_to_static(&mut self, _context: Context, (lvalue, span): (&Lvalue, Span)) {

src/test/compile-fail/borrowck/borrowck-match-binding-is-assignment.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// revisions: ast mir
12+
//[mir]compile-flags: -Zemit-end-regions -Zborrowck-mir
13+
1114
// Test that immutable pattern bindings cannot be reassigned.
1215

1316
#![feature(slice_patterns)]
@@ -23,31 +26,41 @@ struct S {
2326
pub fn main() {
2427
match 1 {
2528
x => {
26-
x += 1; //~ ERROR re-assignment of immutable variable `x`
29+
x += 1; //[ast]~ ERROR re-assignment of immutable variable `x`
30+
//[mir]~^ ERROR (Mir) [E0384]
31+
//[mir]~| ERROR (Ast) [E0384]
2732
}
2833
}
2934

3035
match E::Foo(1) {
3136
E::Foo(x) => {
32-
x += 1; //~ ERROR re-assignment of immutable variable `x`
37+
x += 1; //[ast]~ ERROR re-assignment of immutable variable `x`
38+
//[mir]~^ ERROR (Mir) [E0384]
39+
//[mir]~| ERROR (Ast) [E0384]
3340
}
3441
}
3542

3643
match (S { bar: 1 }) {
3744
S { bar: x } => {
38-
x += 1; //~ ERROR re-assignment of immutable variable `x`
45+
x += 1; //[ast]~ ERROR re-assignment of immutable variable `x`
46+
//[mir]~^ ERROR (Mir) [E0384]
47+
//[mir]~| ERROR (Ast) [E0384]
3948
}
4049
}
4150

4251
match (1,) {
4352
(x,) => {
44-
x += 1; //~ ERROR re-assignment of immutable variable `x`
53+
x += 1; //[ast]~ ERROR re-assignment of immutable variable `x`
54+
//[mir]~^ ERROR (Mir) [E0384]
55+
//[mir]~| ERROR (Ast) [E0384]
4556
}
4657
}
4758

4859
match [1,2,3] {
4960
[x,_,_] => {
50-
x += 1; //~ ERROR re-assignment of immutable variable `x`
61+
x += 1; //[ast]~ ERROR re-assignment of immutable variable `x`
62+
//[mir]~^ ERROR (Mir) [E0384]
63+
//[mir]~| ERROR (Ast) [E0384]
5164
}
5265
}
5366
}

0 commit comments

Comments
 (0)