Skip to content

Commit 0d3811e

Browse files
committed
improve liveness so it reports unused vars / dead assignments
doesn't warn about pattern bindings yet though
1 parent 30b4764 commit 0d3811e

28 files changed

+327
-119
lines changed

src/rustc/middle/liveness.rs

Lines changed: 207 additions & 83 deletions
Large diffs are not rendered by default.
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
fn test(cond: bool) {
1+
fn test() {
22
let v: int;
33
v = 1; //! NOTE prior assignment occurs here
4+
#debug["v=%d", v];
45
v = 2; //! ERROR re-assignment of immutable variable
6+
#debug["v=%d", v];
57
}
68

79
fn main() {
8-
test(true);
910
}

src/test/compile-fail/borrowck-pat-reassign-binding.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ fn main() {
1111
x = some(i+1); //! ERROR assigning to mutable local variable prohibited due to outstanding loan
1212
}
1313
}
14+
copy x; // just to prevent liveness warnings
1415
}

src/test/compile-fail/borrowck-pat-reassign-sometimes-binding.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ fn main() {
1414
x = some(1); //! ERROR assigning to mutable local variable prohibited due to outstanding loan
1515
}
1616
}
17+
copy x; // just to prevent liveness warnings
1718
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
fn test(cond: bool) {
1+
fn test() {
22
let v: int;
33
loop {
44
v = 1; //! ERROR re-assignment of immutable variable
55
//!^ NOTE prior assignment occurs here
6+
copy v; // just to prevent liveness warnings
67
}
78
}
89

910
fn main() {
10-
test(true);
1111
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
fn test(cond: bool) {
1+
fn test() {
22
let v: int;
33
v = 2; //! NOTE prior assignment occurs here
44
v += 1; //! ERROR re-assignment of immutable variable
5+
copy v;
56
}
67

78
fn main() {
8-
test(true);
99
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn test() {
2+
let v: int = 1; //! NOTE prior assignment occurs here
3+
copy v;
4+
v = 2; //! ERROR re-assignment of immutable variable
5+
copy v;
6+
}
7+
8+
fn main() {
9+
}

src/test/compile-fail/liveness-break-uninit-2.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
fn foo() -> int {
22
let x: int;
3-
let i: int;
43

54
while 1 != 2 {
6-
i = 0;
75
break;
86
x = 0; //! WARNING unreachable statement
97
}

src/test/compile-fail/liveness-break-uninit.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
fn foo() -> int {
22
let x: int;
3-
let i: int;
43

54
loop {
6-
i = 0;
75
break;
86
x = 0; //! WARNING unreachable statement
97
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
fn f1(&x: int) {
2+
x = 1; // no error
3+
}
4+
5+
fn f2() {
6+
let mut x = 3; //! WARNING value assigned to `x` is never read
7+
x = 4;
8+
copy x;
9+
}
10+
11+
fn f3() {
12+
let mut x = 3;
13+
copy x;
14+
x = 4; //! WARNING value assigned to `x` is never read
15+
}
16+
17+
fn main() { // leave this in here just to trigger compile-fail:
18+
let x: int;
19+
copy x; //! ERROR use of possibly uninitialized variable: `x`
20+
}

src/test/compile-fail/liveness-init-in-fru.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ type point = {x: int, y: int};
55
fn main() {
66
let mut origin: point;
77
origin = {x: 10 with origin}; //! ERROR use of possibly uninitialized variable: `origin`
8+
copy origin;
89
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
fn test(cond: bool) {
1+
fn test() {
22
let v: int;
33
v += 1; //! ERROR use of possibly uninitialized variable: `v`
4+
copy v;
45
}
56

67
fn main() {
7-
test(true);
88
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
fn test(cond: bool) {
1+
fn test() {
22
let mut v: int;
33
v = v + 1; //! ERROR use of possibly uninitialized variable: `v`
4+
copy v;
45
}
56

67
fn main() {
7-
test(true);
88
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
fn main(s: [str]) {
1+
fn main(_s: [str]) {
22
let a: [int] = [];
3-
vec::each(a) { |x| //! ERROR not all control paths return a value
3+
vec::each(a) { |_x| //! ERROR not all control paths return a value
44
}
55
}

src/test/compile-fail/liveness-move-from-mode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
fn take(-x: int) {}
1+
fn take(-_x: int) {}
22

33
fn main() {
44

src/test/compile-fail/liveness-move-in-loop.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ fn main() {
99
loop {
1010
x <- y; //! ERROR use of moved variable
1111
//!^ NOTE move of variable occurred here
12+
13+
copy x;
1214
}
1315
}
1416
}

src/test/compile-fail/liveness-move-in-while.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fn main() {
44
let mut x: int;
55
loop {
66
log(debug, y);
7-
while true { while true { while true { x <- y; } } }
7+
while true { while true { while true { x <- y; copy x; } } }
88
//!^ ERROR use of moved variable: `y`
99
//!^^ NOTE move of variable occurred here
1010
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
fn main() {
2-
let x = 3;
3-
let y;
4-
x <-> y; //! ERROR use of possibly uninitialized variable: `y`
2+
let mut x = 3;
3+
let y;
4+
x <-> y; //! ERROR use of possibly uninitialized variable: `y`
5+
copy x;
56
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
fn main() {
22
let bar;
3-
fn baz(x: int) { }
3+
fn baz(_x: int) { }
44
bind baz(bar); //! ERROR use of possibly uninitialized variable: `bar`
55
}
66

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
fn f1(x: int) {
2+
//!^ WARNING unused variable: `x`
3+
//!^^ WARNING unused variable x
4+
// (the 2nd one is from tstate)
5+
}
6+
7+
fn f1b(&x: int) {
8+
//!^ WARNING unused variable: `x`
9+
//!^^ WARNING unused variable x
10+
// (the 2nd one is from tstate)
11+
}
12+
13+
fn f2() {
14+
let x = 3;
15+
//!^ WARNING unused variable: `x`
16+
//!^^ WARNING unused variable x
17+
// (the 2nd one is from tstate)
18+
}
19+
20+
fn f3() {
21+
let mut x = 3;
22+
//!^ WARNING unused variable: `x`
23+
x += 4;
24+
//!^ WARNING value assigned to `x` is never read
25+
}
26+
27+
fn f3b() {
28+
let mut z = 3;
29+
//!^ WARNING unused variable: `z`
30+
loop {
31+
z += 4;
32+
}
33+
}
34+
35+
fn f4() {
36+
alt some(3) {
37+
some(i) {
38+
}
39+
none {}
40+
}
41+
}
42+
43+
// leave this in here just to trigger compile-fail:
44+
pure fn is_even(i: int) -> bool { (i%2) == 0 }
45+
fn even(i: int) : is_even(i) -> int { i }
46+
fn main() {
47+
let i: int = 4;
48+
log(debug, false && { check is_even(i); true });
49+
even(i); //! ERROR unsatisfied precondition
50+
}

src/test/compile-fail/liveness-use-after-move.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ fn main() {
22
let x = @5;
33
let y <- x; //! NOTE move of variable occurred here
44
log(debug, *x); //! ERROR use of moved variable: `x`
5+
copy y;
56
}

src/test/compile-fail/liveness-use-after-send.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ enum _chan<T> = int;
88

99
// Tests that "log(debug, message);" is flagged as using
1010
// message after the send deinitializes it
11-
fn test00_start(ch: _chan<int>, message: int, count: int) {
11+
fn test00_start(ch: _chan<int>, message: int, _count: int) {
1212
send(ch, message); //! NOTE move of variable occurred here
1313
log(debug, message); //! ERROR use of moved variable: `message`
1414
}

src/test/compile-fail/no-constraint-prop.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
fn main() unsafe {
44
fn foo(_a: uint, _b: uint) : uint::le(_a, _b) {}
5-
let a: uint = 1u;
6-
let b: uint = 4u;
7-
let c: uint = 5u;
5+
let mut a: uint = 1u;
6+
let mut b: uint = 4u;
7+
let mut c: uint = 5u;
88
// make sure that the constraint le(b, a) exists...
99
check (uint::le(b, a));
1010
// ...invalidate it...

src/test/compile-fail/pred-assign.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ fn f(a: int, b: int) : lt(a, b) { }
77
pure fn lt(a: int, b: int) -> bool { ret a < b; }
88

99
fn main() {
10-
let a: int = 10;
11-
let b: int = 23;
12-
let c: int = 77;
10+
let mut a: int = 10;
11+
let mut b: int = 23;
12+
let mut c: int = 77;
1313
check (lt(a, b));
1414
a = 24;
1515
f(a, b);

src/test/compile-fail/pred-swap.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ fn f(a: int, b: int) : lt(a, b) { }
77
pure fn lt(a: int, b: int) -> bool { ret a < b; }
88

99
fn main() {
10-
let a: int = 10;
11-
let b: int = 23;
12-
let c: int = 77;
10+
let mut a: int = 10;
11+
let mut b: int = 23;
12+
let mut c: int = 77;
1313
check (lt(a, b));
1414
b <-> a;
1515
f(a, b);

src/test/compile-fail/tstate-fru.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ fn main() {
1010
origin = {x: 0, y: 0};
1111
let right: point = {x: 10 with tested(origin)};
1212
//!^ ERROR precondition
13+
copy right;
1314
}

src/test/compile-fail/tstate-while-loop-unsat-constriants.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ pure fn even(y: int) -> bool { true }
66

77
fn main() {
88

9-
let y: int = 42;
10-
let x: int = 1;
9+
let mut y: int = 42;
10+
let mut x: int = 1;
1111
check (even(y));
1212
loop {
1313
print_even(y);

src/test/compile-fail/unsafe-alt.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
enum foo { left({mut x: int}), right(bool) }
44

55
fn main() {
6-
let x = left({mut x: 10});
7-
alt x { left(i) { x = right(false); log(debug, i); } _ { } }
6+
let mut x = left({mut x: 10});
7+
alt x { left(i) { x = right(false); copy x; log(debug, i); } _ { } }
88
}

0 commit comments

Comments
 (0)