Skip to content

Commit 161309b

Browse files
authored
Rollup merge of #126154 - RalfJung:storage-live, r=compiler-errors
StorageLive: refresh storage (instead of UB) when local is already live Blocked on [this FCP](rust-lang/rust#99160 (comment)), which also contains the motivation. Fixes rust-lang/rust#99160 Fixes rust-lang/rust#98896 (by declaring it not-a-bug) Fixes rust-lang/rust#119366 Fixes rust-lang/unsafe-code-guidelines#129
2 parents 0f190d0 + 77904eb commit 161309b

4 files changed

+61
-0
lines changed

tests/fail/storage-live-dead-var.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(core_intrinsics, custom_mir)]
2+
use std::intrinsics::mir::*;
3+
4+
#[custom_mir(dialect = "runtime")]
5+
fn main() {
6+
mir! {
7+
let val: i32;
8+
{
9+
val = 42; //~ERROR: accessing a dead local variable
10+
StorageLive(val); // too late... (but needs to be here to make `val` not implicitly live)
11+
Return()
12+
}
13+
}
14+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: accessing a dead local variable
2+
--> $DIR/storage-live-dead-var.rs:LL:CC
3+
|
4+
LL | val = 42;
5+
| ^^^^^^^^ accessing a dead local variable
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: BACKTRACE:
10+
= note: inside `main` at $DIR/storage-live-dead-var.rs:LL:CC
11+
12+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
13+
14+
error: aborting due to 1 previous error
15+

tests/fail/storage-live-resets-var.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![feature(core_intrinsics, custom_mir)]
2+
use std::intrinsics::mir::*;
3+
4+
#[custom_mir(dialect = "runtime")]
5+
fn main() {
6+
mir! {
7+
let val: i32;
8+
let _val2: i32;
9+
{
10+
StorageLive(val);
11+
val = 42;
12+
StorageLive(val); // reset val to `uninit`
13+
_val2 = val; //~ERROR: uninitialized
14+
Return()
15+
}
16+
}
17+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: constructing invalid value: encountered uninitialized memory, but expected an integer
2+
--> $DIR/storage-live-resets-var.rs:LL:CC
3+
|
4+
LL | _val2 = val;
5+
| ^^^^^^^^^^^ constructing invalid value: encountered uninitialized memory, but expected an integer
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: BACKTRACE:
10+
= note: inside `main` at $DIR/storage-live-resets-var.rs:LL:CC
11+
12+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
13+
14+
error: aborting due to 1 previous error
15+

0 commit comments

Comments
 (0)