Auto merge of #46022 - matthewjasper:cannot-assign-twice-error, r=arielb1

Mir Borrowck: Parity with Ast for E0384 (Cannot assign twice to immutable)

- Closes #45199
- Don't allow assigning to dropped immutable variables
- Show the "first assignment" note on the first assignment that can actually come before the second assignment.
- Make "first assignment" notes point to function parameters if needed.
- Don't show a "first assignment" note if the first and second assignment have the same span (in a loop). This matches ast borrowck for now, but maybe this we should add "in previous loop iteration" as with some other borrowck errors. (Commit 2)
- Use revisions to check mir borrowck for the existing tests for this error. (Commit 3)

~~Still working on a less ad-hoc way to get 'first assignment' notes to show on the correct assignment. Also need to check mutating function arguments.~~ Now using a new dataflow pass.
This commit is contained in:
bors 2017-11-27 17:13:20 +00:00
commit 560a5da9f1
15 changed files with 575 additions and 140 deletions

View file

@ -8,12 +8,18 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// revisions: ast mir
//[mir]compile-flags: -Zborrowck=mir
fn test() {
let v: isize;
v = 1; //~ NOTE first assignment
v = 1; //[ast]~ NOTE first assignment
//[mir]~^ NOTE first assignment
println!("v={}", v);
v = 2; //~ ERROR cannot assign twice to immutable variable
//~| NOTE cannot assign twice to immutable
v = 2; //[ast]~ ERROR cannot assign twice to immutable variable
//[mir]~^ ERROR cannot assign twice to immutable variable `v`
//[ast]~| NOTE cannot assign twice to immutable
//[mir]~| NOTE cannot assign twice to immutable
println!("v={}", v);
}

View file

@ -16,6 +16,12 @@ fn ok() {
}
}
fn also_ok() {
loop {
let _x = String::new();
}
}
fn fail() {
loop {
let x: i32;
@ -26,5 +32,6 @@ fn fail() {
fn main() {
ok();
also_ok();
fail();
}

View file

@ -0,0 +1,41 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// revisions: ast mir
//[mir]compile-flags: -Zborrowck=mir
fn test_drop_replace() {
let b: Box<isize>;
b = Box::new(1); //[ast]~ NOTE first assignment
//[mir]~^ NOTE first assignment
b = Box::new(2); //[ast]~ ERROR cannot assign twice to immutable variable
//[mir]~^ ERROR cannot assign twice to immutable variable `b`
//[ast]~| NOTE cannot assign twice to immutable
//[mir]~| NOTE cannot assign twice to immutable
}
fn test_call() {
let b = Box::new(1); //[ast]~ NOTE first assignment
//[mir]~^ NOTE first assignment
b = Box::new(2); //[ast]~ ERROR cannot assign twice to immutable variable
//[mir]~^ ERROR cannot assign twice to immutable variable `b`
//[ast]~| NOTE cannot assign twice to immutable
//[mir]~| NOTE cannot assign twice to immutable
}
fn test_args(b: Box<i32>) { //[ast]~ NOTE first assignment
//[mir]~^ NOTE first assignment
b = Box::new(2); //[ast]~ ERROR cannot assign twice to immutable variable
//[mir]~^ ERROR cannot assign twice to immutable variable `b`
//[ast]~| NOTE cannot assign twice to immutable
//[mir]~| NOTE cannot assign twice to immutable
}
fn main() {}

View file

@ -8,11 +8,16 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// revisions: ast mir
//[mir]compile-flags: -Zborrowck=mir
fn test() {
let v: isize;
loop {
v = 1; //~ ERROR cannot assign twice to immutable variable
//~^ NOTE cannot assign twice to immutable variable
v = 1; //[ast]~ ERROR cannot assign twice to immutable variable
//[mir]~^ ERROR cannot assign twice to immutable variable `v`
//[ast]~| NOTE cannot assign twice to immutable variable
//[mir]~| NOTE cannot assign twice to immutable variable
v.clone(); // just to prevent liveness warnings
}
}

View file

@ -8,11 +8,17 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// revisions: ast mir
//[mir]compile-flags: -Zborrowck=mir
fn test() {
let v: isize;
v = 2; //~ NOTE first assignment
v += 1; //~ ERROR cannot assign twice to immutable variable
//~| NOTE cannot assign twice to immutable
v = 2; //[ast]~ NOTE first assignment
//[mir]~^ NOTE first assignment
v += 1; //[ast]~ ERROR cannot assign twice to immutable variable
//[mir]~^ ERROR cannot assign twice to immutable variable `v`
//[ast]~| NOTE cannot assign twice to immutable
//[mir]~| NOTE cannot assign twice to immutable
v.clone();
}

View file

@ -0,0 +1,26 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// revisions: ast mir
//[mir]compile-flags: -Zborrowck=mir
fn test() {
let b = Box::new(1); //[ast]~ NOTE first assignment
//[mir]~^ NOTE first assignment
drop(b);
b = Box::new(2); //[ast]~ ERROR cannot assign twice to immutable variable
//[mir]~^ ERROR cannot assign twice to immutable variable `b`
//[ast]~| NOTE cannot assign twice to immutable
//[mir]~| NOTE cannot assign twice to immutable
drop(b);
}
fn main() {
}

View file

@ -8,11 +8,17 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// revisions: ast mir
//[mir]compile-flags: -Zborrowck=mir
fn test() {
let v: isize = 1; //~ NOTE first assignment
let v: isize = 1; //[ast]~ NOTE first assignment
//[mir]~^ NOTE first assignment
v.clone();
v = 2; //~ ERROR cannot assign twice to immutable variable
//~| NOTE cannot assign twice to immutable
v = 2; //[ast]~ ERROR cannot assign twice to immutable variable
//[mir]~^ ERROR cannot assign twice to immutable variable `v`
//[ast]~| NOTE cannot assign twice to immutable
//[mir]~| NOTE cannot assign twice to immutable
v.clone();
}

View file

@ -0,0 +1,54 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// FIXME: Change to UI Test
// Check notes are placed on an assignment that can actually preceed the current assigmnent
// Don't emmit a first assignment for assignment in a loop.
// compile-flags: -Zborrowck=compare
fn test() {
let x;
if true {
x = 1;
} else {
x = 2;
x = 3; //~ ERROR (Ast) [E0384]
//~^ ERROR (Mir) [E0384]
}
}
fn test_in_loop() {
loop {
let x;
if true {
x = 1;
} else {
x = 2;
x = 3; //~ ERROR (Ast) [E0384]
//~^ ERROR (Mir) [E0384]
}
}
}
fn test_using_loop() {
let x;
loop {
if true {
x = 1; //~ ERROR (Ast) [E0384]
//~^ ERROR (Mir) [E0384]
} else {
x = 2; //~ ERROR (Ast) [E0384]
//~^ ERROR (Mir) [E0384]
}
}
}
fn main() {}

View file

@ -0,0 +1,64 @@
error[E0384]: cannot assign twice to immutable variable `x` (Ast)
--> $DIR/liveness-assign-imm-local-notes.rs:23:9
|
22 | x = 2;
| ----- first assignment to `x`
23 | x = 3; //~ ERROR (Ast) [E0384]
| ^^^^^ cannot assign twice to immutable variable
error[E0384]: cannot assign twice to immutable variable `x` (Ast)
--> $DIR/liveness-assign-imm-local-notes.rs:35:13
|
34 | x = 2;
| ----- first assignment to `x`
35 | x = 3; //~ ERROR (Ast) [E0384]
| ^^^^^ cannot assign twice to immutable variable
error[E0384]: cannot assign twice to immutable variable `x` (Ast)
--> $DIR/liveness-assign-imm-local-notes.rs:45:13
|
45 | x = 1; //~ ERROR (Ast) [E0384]
| ^^^^^ cannot assign twice to immutable variable
error[E0384]: cannot assign twice to immutable variable `x` (Ast)
--> $DIR/liveness-assign-imm-local-notes.rs:48:13
|
45 | x = 1; //~ ERROR (Ast) [E0384]
| ----- first assignment to `x`
...
48 | x = 2; //~ ERROR (Ast) [E0384]
| ^^^^^ cannot assign twice to immutable variable
error[E0384]: cannot assign twice to immutable variable `x` (Mir)
--> $DIR/liveness-assign-imm-local-notes.rs:23:9
|
22 | x = 2;
| ----- first assignment to `x`
23 | x = 3; //~ ERROR (Ast) [E0384]
| ^^^^^ cannot assign twice to immutable variable
error[E0384]: cannot assign twice to immutable variable `x` (Mir)
--> $DIR/liveness-assign-imm-local-notes.rs:35:13
|
34 | x = 2;
| ----- first assignment to `x`
35 | x = 3; //~ ERROR (Ast) [E0384]
| ^^^^^ cannot assign twice to immutable variable
error[E0384]: cannot assign twice to immutable variable `x` (Mir)
--> $DIR/liveness-assign-imm-local-notes.rs:45:13
|
45 | x = 1; //~ ERROR (Ast) [E0384]
| ^^^^^ cannot assign twice to immutable variable
error[E0384]: cannot assign twice to immutable variable `x` (Mir)
--> $DIR/liveness-assign-imm-local-notes.rs:48:13
|
45 | x = 1; //~ ERROR (Ast) [E0384]
| ----- first assignment to `x`
...
48 | x = 2; //~ ERROR (Ast) [E0384]
| ^^^^^ cannot assign twice to immutable variable
error: aborting due to 8 previous errors