Skip to content

Commit

Permalink
Rollup merge of rust-lang#134073 - DianQK:fix-131227, r=oli-obk
Browse files Browse the repository at this point in the history
dataflow_const_prop: do not eval a ptr address in SwitchInt

Fixes rust-lang#131227.
  • Loading branch information
fmease authored Dec 9, 2024
2 parents e0bec9d + d0986f4 commit cb5f03c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
9 changes: 7 additions & 2 deletions compiler/rustc_mir_transform/src/dataflow_const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,13 @@ impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> {
// This allows the set of visited edges to grow monotonically with the lattice.
FlatSet::Bottom => TerminatorEdges::None,
FlatSet::Elem(scalar) => {
let choice = scalar.assert_scalar_int().to_bits_unchecked();
TerminatorEdges::Single(targets.target_for_value(choice))
if let Ok(scalar_int) = scalar.try_to_scalar_int() {
TerminatorEdges::Single(
targets.target_for_value(scalar_int.to_bits_unchecked()),
)
} else {
TerminatorEdges::SwitchInt { discr, targets }
}
}
FlatSet::Top => TerminatorEdges::SwitchInt { discr, targets },
}
Expand Down
16 changes: 0 additions & 16 deletions tests/crashes/131227.rs

This file was deleted.

19 changes: 19 additions & 0 deletions tests/ui/dataflow_const_prop/ptr-in-switch-int-issue-131227.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! Issue: <https://github.com/rust-lang/rust/issues/131227>
//! Test that constant propagation in SwitchInt does not crash
//! when encountering a ptr-to-int transmute.
//@ check-pass
//@ compile-flags: -Zmir-enable-passes=+InstSimplify-before-inline,+DataflowConstProp

#![crate_type = "lib"]

static mut G: i32 = 0;

pub fn myfunc() -> i32 {
let var = &raw mut G;
let u: usize = unsafe { std::mem::transmute(var) };
match u {
0 => 0,
_ => 1,
}
}

0 comments on commit cb5f03c

Please sign in to comment.