forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make use of possibly uninitialized data a hard error
This is one of the behaviors we no longer allow in NLL. Since it can lead to undefined behavior, I think it's definitely worth making it a hard error without waiting to turn off migration mode (rust-lang#58781). Closes rust-lang#60450. My ulterior motive here is making it impossible to leave variables partially initialized across a yield (see discussion at rust-lang#63035), so tests are included for that.
- Loading branch information
Showing
20 changed files
with
209 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/test/ui/async-await/partial-initialization-across-await.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Test that we don't allow awaiting from an async fn while a local is partially | ||
// initialized. | ||
|
||
// edition:2018 | ||
|
||
#![feature(async_await)] | ||
|
||
struct S { x: i32, y: i32 } | ||
struct T(i32, i32); | ||
|
||
async fn noop() {} | ||
|
||
async fn test_tuple() { | ||
let mut t: (i32, i32); | ||
t.0 = 42; | ||
//~^ ERROR assign to part of possibly uninitialized variable: `t` [E0381] | ||
noop().await; | ||
t.1 = 88; | ||
let _ = t; | ||
} | ||
|
||
async fn test_tuple_struct() { | ||
let mut t: T; | ||
t.0 = 42; | ||
//~^ ERROR assign to part of possibly uninitialized variable: `t` [E0381] | ||
noop().await; | ||
t.1 = 88; | ||
let _ = t; | ||
} | ||
|
||
async fn test_struct() { | ||
let mut t: S; | ||
t.x = 42; | ||
//~^ ERROR assign to part of possibly uninitialized variable: `t` [E0381] | ||
noop().await; | ||
t.y = 88; | ||
let _ = t; | ||
} | ||
|
||
fn main() { | ||
let _ = test_tuple(); | ||
let _ = test_tuple_struct(); | ||
let _ = test_struct(); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/test/ui/async-await/partial-initialization-across-await.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
error[E0381]: assign to part of possibly uninitialized variable: `t` | ||
--> $DIR/partial-initialization-across-await.rs:15:5 | ||
| | ||
LL | t.0 = 42; | ||
| ^^^^^^^^ use of possibly uninitialized `t` | ||
|
||
error[E0381]: assign to part of possibly uninitialized variable: `t` | ||
--> $DIR/partial-initialization-across-await.rs:24:5 | ||
| | ||
LL | t.0 = 42; | ||
| ^^^^^^^^ use of possibly uninitialized `t` | ||
|
||
error[E0381]: assign to part of possibly uninitialized variable: `t` | ||
--> $DIR/partial-initialization-across-await.rs:33:5 | ||
| | ||
LL | t.x = 42; | ||
| ^^^^^^^^ use of possibly uninitialized `t` | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0381`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Test that we don't allow partial initialization. | ||
// This may be relaxed in the future (see #54987). | ||
|
||
fn main() { | ||
let mut t: (u64, u64); | ||
t.0 = 1; | ||
//~^ ERROR assign to part of possibly uninitialized variable: `t` [E0381] | ||
t.1 = 1; | ||
|
||
let mut t: (u64, u64); | ||
t.1 = 1; | ||
//~^ ERROR assign to part of possibly uninitialized variable: `t` [E0381] | ||
t.0 = 1; | ||
|
||
let mut t: (u64, u64); | ||
t.0 = 1; | ||
//~^ ERROR assign to part of possibly uninitialized variable: `t` [E0381] | ||
|
||
let mut t: (u64,); | ||
t.0 = 1; | ||
//~^ ERROR assign to part of possibly uninitialized variable: `t` [E0381] | ||
} |
27 changes: 27 additions & 0 deletions
27
src/test/ui/borrowck/disallow-possibly-uninitialized.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
error[E0381]: assign to part of possibly uninitialized variable: `t` | ||
--> $DIR/disallow-possibly-uninitialized.rs:6:5 | ||
| | ||
LL | t.0 = 1; | ||
| ^^^^^^^ use of possibly uninitialized `t` | ||
|
||
error[E0381]: assign to part of possibly uninitialized variable: `t` | ||
--> $DIR/disallow-possibly-uninitialized.rs:11:5 | ||
| | ||
LL | t.1 = 1; | ||
| ^^^^^^^ use of possibly uninitialized `t` | ||
|
||
error[E0381]: assign to part of possibly uninitialized variable: `t` | ||
--> $DIR/disallow-possibly-uninitialized.rs:16:5 | ||
| | ||
LL | t.0 = 1; | ||
| ^^^^^^^ use of possibly uninitialized `t` | ||
|
||
error[E0381]: assign to part of possibly uninitialized variable: `t` | ||
--> $DIR/disallow-possibly-uninitialized.rs:20:5 | ||
| | ||
LL | t.0 = 1; | ||
| ^^^^^^^ use of possibly uninitialized `t` | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0381`. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/test/ui/generator/partial-initialization-across-yield.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Test that we don't allow yielding from a generator while a local is partially | ||
// initialized. | ||
|
||
#![feature(generators)] | ||
|
||
struct S { x: i32, y: i32 } | ||
struct T(i32, i32); | ||
|
||
fn test_tuple() { | ||
let _ = || { | ||
let mut t: (i32, i32); | ||
t.0 = 42; | ||
//~^ ERROR assign to part of possibly uninitialized variable: `t` [E0381] | ||
yield; | ||
t.1 = 88; | ||
let _ = t; | ||
}; | ||
} | ||
|
||
fn test_tuple_struct() { | ||
let _ = || { | ||
let mut t: T; | ||
t.0 = 42; | ||
//~^ ERROR assign to part of possibly uninitialized variable: `t` [E0381] | ||
yield; | ||
t.1 = 88; | ||
let _ = t; | ||
}; | ||
} | ||
|
||
fn test_struct() { | ||
let _ = || { | ||
let mut t: S; | ||
t.x = 42; | ||
//~^ ERROR assign to part of possibly uninitialized variable: `t` [E0381] | ||
yield; | ||
t.y = 88; | ||
let _ = t; | ||
}; | ||
} | ||
|
||
fn main() { | ||
test_tuple(); | ||
test_tuple_struct(); | ||
test_struct(); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/test/ui/generator/partial-initialization-across-yield.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
error[E0381]: assign to part of possibly uninitialized variable: `t` | ||
--> $DIR/partial-initialization-across-yield.rs:12:9 | ||
| | ||
LL | t.0 = 42; | ||
| ^^^^^^^^ use of possibly uninitialized `t` | ||
|
||
error[E0381]: assign to part of possibly uninitialized variable: `t` | ||
--> $DIR/partial-initialization-across-yield.rs:23:9 | ||
| | ||
LL | t.0 = 42; | ||
| ^^^^^^^^ use of possibly uninitialized `t` | ||
|
||
error[E0381]: assign to part of possibly uninitialized variable: `t` | ||
--> $DIR/partial-initialization-across-yield.rs:34:9 | ||
| | ||
LL | t.x = 42; | ||
| ^^^^^^^^ use of possibly uninitialized `t` | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0381`. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.