Add span label to E0384 for MIR borrowck

This commit is contained in:
Keith Yeung 2017-09-24 03:56:57 -07:00
parent f71b37bc28
commit 6d4989b821
2 changed files with 42 additions and 11 deletions

View File

@ -580,7 +580,19 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
if flow_state.inits.curr_state.contains(&mpi) {
// may already be assigned before reaching this statement;
// report error.
self.report_illegal_reassignment(context, (lvalue, span));
// FIXME: Not ideal, it only finds the assignment that lexically comes first
let assigned_lvalue = &move_data.move_paths[mpi].lvalue;
let assignment_stmt = self.mir.basic_blocks().iter().filter_map(|bb| {
bb.statements.iter().find(|stmt| {
if let StatementKind::Assign(ref lv, _) = stmt.kind {
*lv == *assigned_lvalue
} else {
false
}
})
}).next().unwrap();
self.report_illegal_reassignment(
context, (lvalue, span), assignment_stmt.source_info.span);
}
}
}
@ -982,11 +994,17 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
err.emit();
}
fn report_illegal_reassignment(&mut self, _context: Context, (lvalue, span): (&Lvalue, Span)) {
let mut err = self.tcx.cannot_reassign_immutable(
span, &self.describe_lvalue(lvalue), Origin::Mir);
// FIXME: add span labels for borrow and assignment points
err.emit();
fn report_illegal_reassignment(&mut self,
_context: Context,
(lvalue, span): (&Lvalue, Span),
assigned_span: Span) {
self.tcx.cannot_reassign_immutable(span,
&self.describe_lvalue(lvalue),
Origin::Mir)
.span_label(span, "re-assignment of immutable variable")
.span_label(assigned_span, format!("first assignment to `{}`",
self.describe_lvalue(lvalue)))
.emit();
}
fn report_assignment_to_static(&mut self, _context: Context, (lvalue, span): (&Lvalue, Span)) {

View File

@ -8,6 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// revisions: ast mir
//[mir]compile-flags: -Zemit-end-regions -Zborrowck-mir
// Test that immutable pattern bindings cannot be reassigned.
#![feature(slice_patterns)]
@ -23,31 +26,41 @@ struct S {
pub fn main() {
match 1 {
x => {
x += 1; //~ ERROR re-assignment of immutable variable `x`
x += 1; //[ast]~ ERROR re-assignment of immutable variable `x`
//[mir]~^ ERROR (Mir) [E0384]
//[mir]~| ERROR (Ast) [E0384]
}
}
match E::Foo(1) {
E::Foo(x) => {
x += 1; //~ ERROR re-assignment of immutable variable `x`
x += 1; //[ast]~ ERROR re-assignment of immutable variable `x`
//[mir]~^ ERROR (Mir) [E0384]
//[mir]~| ERROR (Ast) [E0384]
}
}
match (S { bar: 1 }) {
S { bar: x } => {
x += 1; //~ ERROR re-assignment of immutable variable `x`
x += 1; //[ast]~ ERROR re-assignment of immutable variable `x`
//[mir]~^ ERROR (Mir) [E0384]
//[mir]~| ERROR (Ast) [E0384]
}
}
match (1,) {
(x,) => {
x += 1; //~ ERROR re-assignment of immutable variable `x`
x += 1; //[ast]~ ERROR re-assignment of immutable variable `x`
//[mir]~^ ERROR (Mir) [E0384]
//[mir]~| ERROR (Ast) [E0384]
}
}
match [1,2,3] {
[x,_,_] => {
x += 1; //~ ERROR re-assignment of immutable variable `x`
x += 1; //[ast]~ ERROR re-assignment of immutable variable `x`
//[mir]~^ ERROR (Mir) [E0384]
//[mir]~| ERROR (Ast) [E0384]
}
}
}