Skip to content

Commit 2069015

Browse files
committed
dataflow_const_prop: do not eval a ptr address in SwitchInt
1 parent 1b3fb31 commit 2069015

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,13 @@ impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> {
534534
// This allows the set of visited edges to grow monotonically with the lattice.
535535
FlatSet::Bottom => TerminatorEdges::None,
536536
FlatSet::Elem(scalar) => {
537-
let choice = scalar.assert_scalar_int().to_bits_unchecked();
538-
TerminatorEdges::Single(targets.target_for_value(choice))
537+
if let Ok(scalar_int) = scalar.try_to_scalar_int() {
538+
TerminatorEdges::Single(
539+
targets.target_for_value(scalar_int.to_bits_unchecked()),
540+
)
541+
} else {
542+
TerminatorEdges::SwitchInt { discr, targets }
543+
}
539544
}
540545
FlatSet::Top => TerminatorEdges::SwitchInt { discr, targets },
541546
}

tests/crashes/131227.rs

-16
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ check-pass
2+
//@ compile-flags: -Zmir-enable-passes=+InstSimplify-before-inline,+DataflowConstProp
3+
// Test that constant propagation on a switch does not crash
4+
// when encountering a ptr-to-int transmute.
5+
6+
#![crate_type = "lib"]
7+
8+
static mut G: i32 = 0;
9+
10+
pub fn myfunc() -> i32 {
11+
let var = &raw mut G;
12+
let u: usize = unsafe { std::mem::transmute(var) };
13+
match u {
14+
0 => 0,
15+
_ => 1,
16+
}
17+
}

0 commit comments

Comments
 (0)