Skip to content

Commit 458c9b5

Browse files
authored
Unrolled build for #141494
Rollup merge of #141494 - dianqk:match-br-non-int, r=wesleywiser mir-opt: Do not transform non-int type in match_branches Fixes #141378. r? mir-opt
2 parents 70b3f46 + 457f8ba commit 458c9b5

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

compiler/rustc_mir_transform/src/match_branches.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,14 @@ fn can_cast(
284284
let v = match src_layout.ty.kind() {
285285
ty::Uint(_) => from_scalar.to_uint(src_layout.size),
286286
ty::Int(_) => from_scalar.to_int(src_layout.size) as u128,
287-
_ => unreachable!("invalid int"),
287+
// We can also transform the values of other integer representations (such as char),
288+
// although this may not be practical in real-world scenarios.
289+
_ => return false,
288290
};
289291
let size = match *cast_ty.kind() {
290292
ty::Int(t) => Integer::from_int_ty(&tcx, t).size(),
291293
ty::Uint(t) => Integer::from_uint_ty(&tcx, t).size(),
292-
_ => unreachable!("invalid int"),
294+
_ => return false,
293295
};
294296
let v = size.truncate(v);
295297
let cast_scalar = ScalarInt::try_from_uint(v, size).unwrap();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
- // MIR for `match_non_int_failed` before MatchBranchSimplification
2+
+ // MIR for `match_non_int_failed` after MatchBranchSimplification
3+
4+
fn match_non_int_failed(_1: char) -> u8 {
5+
let mut _0: u8;
6+
7+
bb0: {
8+
switchInt(copy _1) -> [97: bb1, 98: bb2, otherwise: bb3];
9+
}
10+
11+
bb1: {
12+
_0 = const 97_u8;
13+
goto -> bb4;
14+
}
15+
16+
bb2: {
17+
_0 = const 98_u8;
18+
goto -> bb4;
19+
}
20+
21+
bb3: {
22+
unreachable;
23+
}
24+
25+
bb4: {
26+
return;
27+
}
28+
}
29+

tests/mir-opt/matches_reduce_branches.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,37 @@ fn match_i128_u128(i: EnumAi128) -> u128 {
627627
}
628628
}
629629

630+
// EMIT_MIR matches_reduce_branches.match_non_int_failed.MatchBranchSimplification.diff
631+
#[custom_mir(dialect = "runtime")]
632+
fn match_non_int_failed(i: char) -> u8 {
633+
// CHECK-LABEL: fn match_non_int_failed(
634+
// CHECK: switchInt
635+
// CHECK: return
636+
mir! {
637+
{
638+
match i {
639+
'a' => bb1,
640+
'b' => bb2,
641+
_ => unreachable_bb,
642+
}
643+
}
644+
bb1 = {
645+
RET = 97;
646+
Goto(ret)
647+
}
648+
bb2 = {
649+
RET = 98;
650+
Goto(ret)
651+
}
652+
unreachable_bb = {
653+
Unreachable()
654+
}
655+
ret = {
656+
Return()
657+
}
658+
}
659+
}
660+
630661
fn main() {
631662
let _ = foo(None);
632663
let _ = foo(Some(()));
@@ -664,4 +695,5 @@ fn main() {
664695
let _ = match_i128_u128(EnumAi128::A);
665696

666697
let _ = my_is_some(None);
698+
let _ = match_non_int_failed('a');
667699
}

0 commit comments

Comments
 (0)