Skip to content

Commit c5d9ae7

Browse files
Do not optimize out SwitchInt before borrowck, or if Zmir-preserve-ub
1 parent 3a31fce commit c5d9ae7

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ pub const MIRI_DEFAULT_ARGS: &[&str] = &[
169169
"-Zalways-encode-mir",
170170
"-Zextra-const-ub-checks",
171171
"-Zmir-emit-retag",
172-
"-Zmir-keep-place-mention",
172+
"-Zmir-preserve-ub",
173173
"-Zmir-opt-level=0",
174174
"-Zmir-enable-passes=-CheckAlignment,-CheckNull",
175175
// Deduplicating diagnostics means we miss events when tracking what happens during an
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Ensure that we don't optimize out `SwitchInt` reads even if that terminator
2+
// branches to the same basic block on every target, since the operand may have
3+
// side-effects that affect analysis of the MIR.
4+
//
5+
// See <https://github.com/rust-lang/miri/issues/4237>.
6+
7+
use std::mem::MaybeUninit;
8+
9+
fn main() {
10+
let uninit: MaybeUninit<i32> = MaybeUninit::uninit();
11+
let bad_ref: &i32 = unsafe { uninit.assume_init_ref() };
12+
let &(0 | _) = bad_ref;
13+
//~^ ERROR: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
2+
--> tests/fail/read_from_trivial_switch.rs:LL:CC
3+
|
4+
LL | let &(0 | _) = bad_ref;
5+
| ^^^^^^^^ using uninitialized data, but this operation requires initialized memory
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 tests/fail/read_from_trivial_switch.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)