From 324f136401b4edc1500fc2dfca75ca0de8d511f6 Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Mon, 14 Sep 2026 18:17:10 -0400 Subject: [PATCH] Fix a bug in MatchBranchSimplification --- .../rustc_mir_transform/src/match_branches.rs | 19 ++- ...sing_locals.MatchBranchSimplification.diff | 36 ++++++ .../match_branch_simplification_aliasing.rs | 116 ++++++++++++++++++ ...nion_fields.MatchBranchSimplification.diff | 34 +++++ 4 files changed, 204 insertions(+), 1 deletion(-) create mode 100644 tests/mir-opt/match_branch_simplification_aliasing.aliasing_locals.MatchBranchSimplification.diff create mode 100644 tests/mir-opt/match_branch_simplification_aliasing.rs create mode 100644 tests/mir-opt/match_branch_simplification_aliasing.union_fields.MatchBranchSimplification.diff diff --git a/compiler/rustc_mir_transform/src/match_branches.rs b/compiler/rustc_mir_transform/src/match_branches.rs index 894f209f9b4..48a0caa4979 100644 --- a/compiler/rustc_mir_transform/src/match_branches.rs +++ b/compiler/rustc_mir_transform/src/match_branches.rs @@ -1,5 +1,6 @@ use rustc_abi::Integer; use rustc_const_eval::const_eval::mk_eval_cx_for_const_val; +use rustc_index::bit_set::DenseBitSet; use rustc_middle::mir::*; use rustc_middle::ty::layout::{IntegerExt, TyAndLayout}; use rustc_middle::ty::util::Discr; @@ -44,6 +45,7 @@ struct SimplifyMatch<'tcx, 'a> { discr: &'a Operand<'tcx>, discr_local: Option, discr_ty: Ty<'tcx>, + borrowed_locals: Option>, } impl<'tcx, 'a> SimplifyMatch<'tcx, 'a> { @@ -228,7 +230,7 @@ impl<'tcx, 'a> SimplifyMatch<'tcx, 'a> { /// ``` /// This will simplify into a copy statement. fn unify_by_copy( - &self, + &mut self, dest: Place<'tcx>, rvals: &[(u128, &Rvalue<'tcx>)], ) -> Option> { @@ -258,6 +260,20 @@ impl<'tcx, 'a> SimplifyMatch<'tcx, 'a> { return None; }; + if copy_src_place.is_indirect() { + // If the src place is indirect, only permit generating the copy when the dest place is + // never borrowed. + let borrowed_locals = self + .borrowed_locals + .get_or_insert_with(|| rustc_mir_dataflow::impls::borrowed_locals(self.body)); + if borrowed_locals.contains(dest.local) { + return None; + } + } else if copy_src_place.local == dest.local { + // Also forbid the case where the source and dest are fields of the same local + return None; + } + for &(case, rvalue) in rvals.iter() { match rvalue { // Check if `_3 = const Foo::B` can be transformed to `_3 = copy *_1`. @@ -385,6 +401,7 @@ fn simplify_match<'tcx>( discr, discr_local: None, discr_ty: discr.ty(body.local_decls(), tcx), + borrowed_locals: None, }; let reachable_cases: Vec<_> = targets.iter().filter(|&(_, bb)| !body.basic_blocks[bb].is_empty_unreachable()).collect(); diff --git a/tests/mir-opt/match_branch_simplification_aliasing.aliasing_locals.MatchBranchSimplification.diff b/tests/mir-opt/match_branch_simplification_aliasing.aliasing_locals.MatchBranchSimplification.diff new file mode 100644 index 00000000000..ccaa78736af --- /dev/null +++ b/tests/mir-opt/match_branch_simplification_aliasing.aliasing_locals.MatchBranchSimplification.diff @@ -0,0 +1,36 @@ +- // MIR for `aliasing_locals` before MatchBranchSimplification ++ // MIR for `aliasing_locals` after MatchBranchSimplification + + fn aliasing_locals(_1: Foo) -> Foo { + let mut _0: Foo; + let mut _2: Foo; + let mut _3: *const Foo; + let mut _4: u8; + + bb0: { + _2 = copy _1; + _3 = &raw const _2; + _4 = discriminant((*_3)); + switchInt(copy _4) -> [0: bb2, 1: bb3, otherwise: bb1]; + } + + bb1: { + unreachable; + } + + bb2: { + _2 = Foo::A; + goto -> bb4; + } + + bb3: { + _2 = Foo::B; + goto -> bb4; + } + + bb4: { + _0 = copy _2; + return; + } + } + diff --git a/tests/mir-opt/match_branch_simplification_aliasing.rs b/tests/mir-opt/match_branch_simplification_aliasing.rs new file mode 100644 index 00000000000..1c81d2efa74 --- /dev/null +++ b/tests/mir-opt/match_branch_simplification_aliasing.rs @@ -0,0 +1,116 @@ +//@ test-mir-pass: MatchBranchSimplification + +#![feature(custom_mir, core_intrinsics)] +#![allow(internal_features)] + +use std::intrinsics::mir::*; + +#[repr(u8)] +#[derive(Clone, Copy, PartialEq, Eq)] +enum Foo { + A, + B, + // This variant is not used, but makes the enum BackendRepr::Memory. Without it, the enum is a + // scalar and overlapping copies of it are permitted. + C(u32), +} + +// EMIT_MIR match_branch_simplification_aliasing.aliasing_locals.MatchBranchSimplification.diff +#[inline(never)] +#[custom_mir(dialect = "runtime")] +fn aliasing_locals(init: Foo) -> Foo { + // CHECK-LABEL: fn aliasing_locals(_1 + // CHECK: _2 = copy _1; + // CHECK: _3 = &raw const _2; + // CHECK: _4 = discriminant((*_3)); + // CHECK-NOT: copy (*_3); + // CHECK: switchInt + // CHECK: _2 = Foo::A; + // CHECK: _2 = Foo::B; + // CHECK: _0 = copy _2; + mir! { + let x: Foo; + let p: *const Foo; + let d: u8; + { + x = init; + p = core::ptr::addr_of!(x); + d = Discriminant(*p); + match d { + 0 => bb_a, + 1 => bb_b, + _ => bb_unreachable, + } + } + bb_unreachable = { + Unreachable() + } + bb_a = { + x = Foo::A; + Goto(bb_join) + } + bb_b = { + x = Foo::B; + Goto(bb_join) + } + bb_join = { + RET = x; + Return() + } + } +} + +union U { + a: Foo, + b: Foo, +} + +// EMIT_MIR match_branch_simplification_aliasing.union_fields.MatchBranchSimplification.diff +#[inline(never)] +#[custom_mir(dialect = "runtime")] +fn union_fields(init: Foo) -> Foo { + // CHECK-LABEL: fn union_fields(_1 + // CHECK: (_2.1: Foo) = copy _1; + // CHECK: _3 = discriminant((_2.1: Foo)); + // CHECK-NOT: copy(_2.1: Foo); + // CHECK: switchInt + // CHECK: (_2.0: Foo) = Foo::A; + // CHECK: (_2.0: Foo) = Foo::B; + // CHECK: _0 = copy (_2.0: Foo); + mir! { + let u: U; + let d: u8; + { + u.b = init; + d = Discriminant(u.b); + match d { + 0 => bb_a, + 1 => bb_b, + _ => bb_unreachable, + } + } + bb_unreachable = { + Unreachable() + } + bb_a = { + u.a = Foo::A; + Goto(bb_join) + } + bb_b = { + u.a = Foo::B; + Goto(bb_join) + } + bb_join = { + RET = u.a; + Return() + } + } +} + +fn main() { + let r = aliasing_locals(std::hint::black_box(Foo::B)); + assert!(r == Foo::B); + + let r = union_fields(std::hint::black_box(Foo::B)); + assert!(r == Foo::B); +} diff --git a/tests/mir-opt/match_branch_simplification_aliasing.union_fields.MatchBranchSimplification.diff b/tests/mir-opt/match_branch_simplification_aliasing.union_fields.MatchBranchSimplification.diff new file mode 100644 index 00000000000..15092edd549 --- /dev/null +++ b/tests/mir-opt/match_branch_simplification_aliasing.union_fields.MatchBranchSimplification.diff @@ -0,0 +1,34 @@ +- // MIR for `union_fields` before MatchBranchSimplification ++ // MIR for `union_fields` after MatchBranchSimplification + + fn union_fields(_1: Foo) -> Foo { + let mut _0: Foo; + let mut _2: U; + let mut _3: u8; + + bb0: { + (_2.1: Foo) = copy _1; + _3 = discriminant((_2.1: Foo)); + switchInt(copy _3) -> [0: bb2, 1: bb3, otherwise: bb1]; + } + + bb1: { + unreachable; + } + + bb2: { + (_2.0: Foo) = Foo::A; + goto -> bb4; + } + + bb3: { + (_2.0: Foo) = Foo::B; + goto -> bb4; + } + + bb4: { + _0 = copy (_2.0: Foo); + return; + } + } +