Skip to content

Commit 4e50c5b

Browse files
committed
add tests for assigning fields without initializing var
We did not seem to have any!
1 parent 78e987a commit 4e50c5b

8 files changed

+234
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2017 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+
// Currently, we permit you to assign to individual fields of a mut
12+
// var, but we do not permit you to use the complete var afterwards.
13+
// We hope to fix this at some point.
14+
//
15+
// FIXME(#21232)
16+
17+
#![feature(nll)]
18+
19+
fn assign_both_fields_and_use() {
20+
let mut x: (u32, u32);
21+
x.0 = 1;
22+
x.1 = 22;
23+
drop(x.0);
24+
drop(x.1);
25+
}
26+
27+
fn assign_both_fields_the_use_var() {
28+
let mut x: (u32, u32);
29+
x.0 = 1;
30+
x.1 = 22;
31+
drop(x); //~ ERROR
32+
}
33+
34+
fn main() { }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0381]: use of possibly uninitialized variable: `x`
2+
--> $DIR/assign_mutable_fields.rs:31:10
3+
|
4+
LL | drop(x); //~ ERROR
5+
| ^ use of possibly uninitialized `x`
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0381`.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2017 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+
// This test is currently disallowed, but we hope someday to support it.
12+
//
13+
// FIXME(#21232)
14+
15+
#![feature(nll)]
16+
17+
fn assign_both_fields_and_use() {
18+
let x: (u32, u32);
19+
x.0 = 1; //~ ERROR
20+
x.1 = 22; //~ ERROR
21+
drop(x.0);
22+
drop(x.1);
23+
}
24+
25+
fn assign_both_fields_the_use_var() {
26+
let x: (u32, u32);
27+
x.0 = 1; //~ ERROR
28+
x.1 = 22; //~ ERROR
29+
drop(x); //~ ERROR
30+
}
31+
32+
fn main() { }
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
error[E0594]: cannot assign to `x.0`, as `x` is not declared as mutable
2+
--> $DIR/reassignment_immutable_fields.rs:19:5
3+
|
4+
LL | let x: (u32, u32);
5+
| - help: consider changing this to be mutable: `mut x`
6+
LL | x.0 = 1; //~ ERROR
7+
| ^^^^^^^ cannot assign
8+
9+
error[E0594]: cannot assign to `x.1`, as `x` is not declared as mutable
10+
--> $DIR/reassignment_immutable_fields.rs:20:5
11+
|
12+
LL | let x: (u32, u32);
13+
| - help: consider changing this to be mutable: `mut x`
14+
LL | x.0 = 1; //~ ERROR
15+
LL | x.1 = 22; //~ ERROR
16+
| ^^^^^^^^ cannot assign
17+
18+
error[E0594]: cannot assign to `x.0`, as `x` is not declared as mutable
19+
--> $DIR/reassignment_immutable_fields.rs:27:5
20+
|
21+
LL | let x: (u32, u32);
22+
| - help: consider changing this to be mutable: `mut x`
23+
LL | x.0 = 1; //~ ERROR
24+
| ^^^^^^^ cannot assign
25+
26+
error[E0594]: cannot assign to `x.1`, as `x` is not declared as mutable
27+
--> $DIR/reassignment_immutable_fields.rs:28:5
28+
|
29+
LL | let x: (u32, u32);
30+
| - help: consider changing this to be mutable: `mut x`
31+
LL | x.0 = 1; //~ ERROR
32+
LL | x.1 = 22; //~ ERROR
33+
| ^^^^^^^^ cannot assign
34+
35+
error[E0381]: use of possibly uninitialized variable: `x`
36+
--> $DIR/reassignment_immutable_fields.rs:29:10
37+
|
38+
LL | drop(x); //~ ERROR
39+
| ^ use of possibly uninitialized `x`
40+
41+
error: aborting due to 5 previous errors
42+
43+
Some errors occurred: E0381, E0594.
44+
For more information about an error, try `rustc --explain E0381`.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2017 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+
// This should never be allowed -- `foo.a` and `foo.b` are
12+
// overlapping, so since `x` is not `mut` we should not permit
13+
// reassignment.
14+
15+
#![feature(nll)]
16+
17+
union Foo {
18+
a: u32,
19+
b: u32,
20+
}
21+
22+
unsafe fn overlapping_fields() {
23+
let x: Foo;
24+
x.a = 1; //~ ERROR
25+
x.b = 22; //~ ERROR
26+
}
27+
28+
fn main() { }
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0594]: cannot assign to `x.a`, as `x` is not declared as mutable
2+
--> $DIR/reassignment_immutable_fields_overlapping.rs:24:5
3+
|
4+
LL | let x: Foo;
5+
| - help: consider changing this to be mutable: `mut x`
6+
LL | x.a = 1; //~ ERROR
7+
| ^^^^^^^ cannot assign
8+
9+
error[E0594]: cannot assign to `x.b`, as `x` is not declared as mutable
10+
--> $DIR/reassignment_immutable_fields_overlapping.rs:25:5
11+
|
12+
LL | let x: Foo;
13+
| - help: consider changing this to be mutable: `mut x`
14+
LL | x.a = 1; //~ ERROR
15+
LL | x.b = 22; //~ ERROR
16+
| ^^^^^^^^ cannot assign
17+
18+
error: aborting due to 2 previous errors
19+
20+
For more information about this error, try `rustc --explain E0594`.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2017 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+
// This should never be allowed -- since `x` is not `mut`, so `x.0`
12+
// cannot be assigned twice.
13+
14+
#![feature(nll)]
15+
16+
fn var_then_field() {
17+
let x: (u32, u32);
18+
x = (22, 44);
19+
x.0 = 1; //~ ERROR
20+
}
21+
22+
fn same_field_twice() {
23+
let x: (u32, u32);
24+
x.0 = 1; //~ ERROR
25+
x.0 = 22; //~ ERROR
26+
x.1 = 44; //~ ERROR
27+
}
28+
29+
fn main() { }
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
error[E0594]: cannot assign to `x.0`, as `x` is not declared as mutable
2+
--> $DIR/reassignment_immutable_fields_twice.rs:19:5
3+
|
4+
LL | let x: (u32, u32);
5+
| - help: consider changing this to be mutable: `mut x`
6+
LL | x = (22, 44);
7+
LL | x.0 = 1; //~ ERROR
8+
| ^^^^^^^ cannot assign
9+
10+
error[E0594]: cannot assign to `x.0`, as `x` is not declared as mutable
11+
--> $DIR/reassignment_immutable_fields_twice.rs:24:5
12+
|
13+
LL | let x: (u32, u32);
14+
| - help: consider changing this to be mutable: `mut x`
15+
LL | x.0 = 1; //~ ERROR
16+
| ^^^^^^^ cannot assign
17+
18+
error[E0594]: cannot assign to `x.0`, as `x` is not declared as mutable
19+
--> $DIR/reassignment_immutable_fields_twice.rs:25:5
20+
|
21+
LL | let x: (u32, u32);
22+
| - help: consider changing this to be mutable: `mut x`
23+
LL | x.0 = 1; //~ ERROR
24+
LL | x.0 = 22; //~ ERROR
25+
| ^^^^^^^^ cannot assign
26+
27+
error[E0594]: cannot assign to `x.1`, as `x` is not declared as mutable
28+
--> $DIR/reassignment_immutable_fields_twice.rs:26:5
29+
|
30+
LL | let x: (u32, u32);
31+
| - help: consider changing this to be mutable: `mut x`
32+
...
33+
LL | x.1 = 44; //~ ERROR
34+
| ^^^^^^^^ cannot assign
35+
36+
error: aborting due to 4 previous errors
37+
38+
For more information about this error, try `rustc --explain E0594`.

0 commit comments

Comments
 (0)