Skip to content

Commit 6853b34

Browse files
committed
Don't show first assignment in loops
Matches current ast-borrowck behavior.
1 parent d409dbf commit 6853b34

File tree

3 files changed

+126
-6
lines changed

3 files changed

+126
-6
lines changed

src/librustc_mir/borrow_check.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1586,13 +1586,15 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
15861586
_context: Context,
15871587
(lvalue, span): (&Lvalue<'tcx>, Span),
15881588
assigned_span: Span) {
1589-
self.tcx.cannot_reassign_immutable(span,
1589+
let mut err = self.tcx.cannot_reassign_immutable(span,
15901590
&self.describe_lvalue(lvalue),
1591-
Origin::Mir)
1592-
.span_label(span, "cannot assign twice to immutable variable")
1593-
.span_label(assigned_span, format!("first assignment to `{}`",
1594-
self.describe_lvalue(lvalue)))
1595-
.emit();
1591+
Origin::Mir);
1592+
err.span_label(span, "cannot assign twice to immutable variable");
1593+
if span != assigned_span {
1594+
err.span_label(assigned_span, format!("first assignment to `{}`",
1595+
self.describe_lvalue(lvalue)));
1596+
}
1597+
err.emit();
15961598
}
15971599

15981600
fn report_assignment_to_static(&mut self,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// FIXME: Change to UI Test
12+
// Check notes are placed on an assignment that can actually preceed the current assigmnent
13+
// Don't emmit a first assignment for assignment in a loop.
14+
15+
// compile-flags: -Zborrowck=compare
16+
17+
fn test() {
18+
let x;
19+
if true {
20+
x = 1;
21+
} else {
22+
x = 2;
23+
x = 3; //~ ERROR (Ast) [E0384]
24+
//~^ ERROR (Mir) [E0384]
25+
}
26+
}
27+
28+
fn test_in_loop() {
29+
loop {
30+
let x;
31+
if true {
32+
x = 1;
33+
} else {
34+
x = 2;
35+
x = 3; //~ ERROR (Ast) [E0384]
36+
//~^ ERROR (Mir) [E0384]
37+
}
38+
}
39+
}
40+
41+
fn test_using_loop() {
42+
let x;
43+
loop {
44+
if true {
45+
x = 1; //~ ERROR (Ast) [E0384]
46+
//~^ ERROR (Mir) [E0384]
47+
} else {
48+
x = 2; //~ ERROR (Ast) [E0384]
49+
//~^ ERROR (Mir) [E0384]
50+
}
51+
}
52+
}
53+
54+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
error[E0384]: cannot assign twice to immutable variable `x` (Ast)
2+
--> $DIR/liveness-assign-imm-local-notes.rs:23:9
3+
|
4+
22 | x = 2;
5+
| ----- first assignment to `x`
6+
23 | x = 3; //~ ERROR (Ast) [E0384]
7+
| ^^^^^ cannot assign twice to immutable variable
8+
9+
error[E0384]: cannot assign twice to immutable variable `x` (Ast)
10+
--> $DIR/liveness-assign-imm-local-notes.rs:35:13
11+
|
12+
34 | x = 2;
13+
| ----- first assignment to `x`
14+
35 | x = 3; //~ ERROR (Ast) [E0384]
15+
| ^^^^^ cannot assign twice to immutable variable
16+
17+
error[E0384]: cannot assign twice to immutable variable `x` (Ast)
18+
--> $DIR/liveness-assign-imm-local-notes.rs:45:13
19+
|
20+
45 | x = 1; //~ ERROR (Ast) [E0384]
21+
| ^^^^^ cannot assign twice to immutable variable
22+
23+
error[E0384]: cannot assign twice to immutable variable `x` (Ast)
24+
--> $DIR/liveness-assign-imm-local-notes.rs:48:13
25+
|
26+
45 | x = 1; //~ ERROR (Ast) [E0384]
27+
| ----- first assignment to `x`
28+
...
29+
48 | x = 2; //~ ERROR (Ast) [E0384]
30+
| ^^^^^ cannot assign twice to immutable variable
31+
32+
error[E0384]: cannot assign twice to immutable variable `x` (Mir)
33+
--> $DIR/liveness-assign-imm-local-notes.rs:23:9
34+
|
35+
22 | x = 2;
36+
| ----- first assignment to `x`
37+
23 | x = 3; //~ ERROR (Ast) [E0384]
38+
| ^^^^^ cannot assign twice to immutable variable
39+
40+
error[E0384]: cannot assign twice to immutable variable `x` (Mir)
41+
--> $DIR/liveness-assign-imm-local-notes.rs:35:13
42+
|
43+
34 | x = 2;
44+
| ----- first assignment to `x`
45+
35 | x = 3; //~ ERROR (Ast) [E0384]
46+
| ^^^^^ cannot assign twice to immutable variable
47+
48+
error[E0384]: cannot assign twice to immutable variable `x` (Mir)
49+
--> $DIR/liveness-assign-imm-local-notes.rs:45:13
50+
|
51+
45 | x = 1; //~ ERROR (Ast) [E0384]
52+
| ^^^^^ cannot assign twice to immutable variable
53+
54+
error[E0384]: cannot assign twice to immutable variable `x` (Mir)
55+
--> $DIR/liveness-assign-imm-local-notes.rs:48:13
56+
|
57+
45 | x = 1; //~ ERROR (Ast) [E0384]
58+
| ----- first assignment to `x`
59+
...
60+
48 | x = 2; //~ ERROR (Ast) [E0384]
61+
| ^^^^^ cannot assign twice to immutable variable
62+
63+
error: aborting due to 8 previous errors
64+

0 commit comments

Comments
 (0)