Skip to content

Commit a0c5857

Browse files
committed
Add test for #59352
1 parent 18ec4a9 commit a0c5857

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

src/test/codegen/issue-59352.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This test is a mirror of mir-opt/issues/issue-59352.rs. The LLVM inliner doesn't inline
2+
// `char::method::is_digit()` and `char::method::to_digit()`, probably because of their size.
3+
//
4+
// Currently, the MIR optimizer isn't capable of removing the unreachable panic in this test case.
5+
// Once the optimizer can do that, mir-opt/issues/issue-59352.rs will need to be updated and this
6+
// test case should be removed as it will become redundant.
7+
8+
// mir-opt-level=2 enables inlining and enables LLVM to optimize away the unreachable panic call.
9+
// compile-flags: -O -Z mir-opt-level=2
10+
11+
#![crate_type = "rlib"]
12+
13+
// CHECK-LABEL: @num_to_digit
14+
#[no_mangle]
15+
pub fn num_to_digit(num: char) -> u32 {
16+
// CHECK-NOT: panic
17+
if num.is_digit(8) { num.to_digit(8).unwrap() } else { 0 }
18+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This test is a mirror of codegen/issue-59352.rs.
2+
// The LLVM inliner doesn't inline `char::method::is_digit()` and so it doesn't recognize this case
3+
// as effectively `if x.is_some() { x.unwrap() } else { 0 }`.
4+
//
5+
// Currently, the MIR optimizer isn't capable of removing the unreachable panic in this test case.
6+
// Once the optimizer can do that, this test case will need to be updated and codegen/issue-59352.rs
7+
// removed.
8+
9+
// EMIT_MIR issue_59352.num_to_digit.PreCodegen.after.mir
10+
// compile-flags: -Z mir-opt-level=2 -Z span_free_formats
11+
12+
pub fn num_to_digit(num: char) -> u32 {
13+
// CHECK-NOT: panic
14+
if num.is_digit(8) { num.to_digit(8).unwrap() } else { 0 }
15+
}
16+
17+
pub fn main() {
18+
num_to_digit('2');
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// MIR for `num_to_digit` after PreCodegen
2+
3+
fn num_to_digit(_1: char) -> u32 {
4+
debug num => _1; // in scope 0 at $DIR/issue-59352.rs:12:21: 12:24
5+
let mut _0: u32; // return place in scope 0 at $DIR/issue-59352.rs:12:35: 12:38
6+
let mut _2: bool; // in scope 0 at $DIR/issue-59352.rs:14:8: 14:23
7+
let mut _3: std::option::Option<u32>; // in scope 0 at $DIR/issue-59352.rs:14:26: 14:41
8+
let mut _4: char; // in scope 0 at $DIR/issue-59352.rs:14:26: 14:29
9+
let mut _5: u32; // in scope 0 at $DIR/issue-59352.rs:14:8: 14:23
10+
let mut _10: isize; // in scope 0 at $DIR/issue-59352.rs:14:8: 14:23
11+
scope 1 (inlined char::methods::<impl char>::is_digit) { // at $DIR/issue-59352.rs:14:8: 14:23
12+
debug self => _8; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
13+
debug radix => _5; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
14+
let mut _6: &std::option::Option<u32>; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
15+
let _7: std::option::Option<u32>; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
16+
let mut _8: char; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
17+
scope 2 (inlined Option::<u32>::is_some) { // at $DIR/issue-59352.rs:14:8: 14:23
18+
debug self => _6; // in scope 2 at $DIR/issue-59352.rs:14:8: 14:23
19+
}
20+
}
21+
scope 3 (inlined #[track_caller] Option::<u32>::unwrap) { // at $DIR/issue-59352.rs:14:26: 14:50
22+
debug self => _3; // in scope 3 at $DIR/issue-59352.rs:14:26: 14:50
23+
let mut _9: isize; // in scope 3 at $DIR/issue-59352.rs:14:26: 14:50
24+
scope 4 {
25+
debug val => _0; // in scope 4 at $DIR/issue-59352.rs:14:26: 14:50
26+
}
27+
}
28+
29+
bb0: {
30+
StorageLive(_2); // scope 0 at $DIR/issue-59352.rs:14:8: 14:23
31+
_8 = _1; // scope 0 at $DIR/issue-59352.rs:14:8: 14:11
32+
StorageLive(_5); // scope 0 at $DIR/issue-59352.rs:14:8: 14:23
33+
_5 = const 8_u32; // scope 0 at $DIR/issue-59352.rs:14:8: 14:23
34+
StorageLive(_6); // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
35+
StorageLive(_7); // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
36+
_7 = char::methods::<impl char>::to_digit(move _8, const 8_u32) -> bb5; // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
37+
// mir::Constant
38+
// + span: $DIR/issue-59352.rs:14:8: 14:23
39+
// + literal: Const { ty: fn(char, u32) -> std::option::Option<u32> {std::char::methods::<impl char>::to_digit}, val: Value(Scalar(<ZST>)) }
40+
}
41+
42+
bb1: {
43+
StorageLive(_3); // scope 0 at $DIR/issue-59352.rs:14:26: 14:41
44+
StorageLive(_4); // scope 0 at $DIR/issue-59352.rs:14:26: 14:29
45+
_4 = _1; // scope 0 at $DIR/issue-59352.rs:14:26: 14:29
46+
_3 = char::methods::<impl char>::to_digit(move _4, const 8_u32) -> bb3; // scope 0 at $DIR/issue-59352.rs:14:26: 14:41
47+
// mir::Constant
48+
// + span: $DIR/issue-59352.rs:14:30: 14:38
49+
// + literal: Const { ty: fn(char, u32) -> std::option::Option<u32> {std::char::methods::<impl char>::to_digit}, val: Value(Scalar(<ZST>)) }
50+
}
51+
52+
bb2: {
53+
_0 = const 0_u32; // scope 0 at $DIR/issue-59352.rs:14:60: 14:61
54+
goto -> bb4; // scope 0 at $DIR/issue-59352.rs:14:5: 14:63
55+
}
56+
57+
bb3: {
58+
StorageDead(_4); // scope 0 at $DIR/issue-59352.rs:14:40: 14:41
59+
StorageLive(_9); // scope 0 at $DIR/issue-59352.rs:14:26: 14:50
60+
_9 = discriminant(_3); // scope 3 at $DIR/issue-59352.rs:14:26: 14:50
61+
switchInt(move _9) -> [0_isize: bb6, 1_isize: bb8, otherwise: bb7]; // scope 3 at $DIR/issue-59352.rs:14:26: 14:50
62+
}
63+
64+
bb4: {
65+
StorageDead(_2); // scope 0 at $DIR/issue-59352.rs:14:62: 14:63
66+
return; // scope 0 at $DIR/issue-59352.rs:15:2: 15:2
67+
}
68+
69+
bb5: {
70+
_6 = &_7; // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
71+
_10 = discriminant((*_6)); // scope 2 at $DIR/issue-59352.rs:14:8: 14:23
72+
_2 = Eq(_10, const 1_isize); // scope 2 at $DIR/issue-59352.rs:14:8: 14:23
73+
StorageDead(_6); // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
74+
StorageDead(_7); // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
75+
StorageDead(_5); // scope 0 at $DIR/issue-59352.rs:14:8: 14:23
76+
switchInt(move _2) -> [false: bb2, otherwise: bb1]; // scope 0 at $DIR/issue-59352.rs:14:5: 14:63
77+
}
78+
79+
bb6: {
80+
core::panicking::panic(const "called `Option::unwrap()` on a `None` value"); // scope 3 at $DIR/issue-59352.rs:14:26: 14:50
81+
// mir::Constant
82+
// + span: $DIR/issue-59352.rs:14:26: 14:50
83+
// + literal: Const { ty: fn(&'static str) -> ! {core::panicking::panic}, val: Value(Scalar(<ZST>)) }
84+
// ty::Const
85+
// + ty: &str
86+
// + val: Value(Slice { data: Allocation { bytes: [99, 97, 108, 108, 101, 100, 32, 96, 79, 112, 116, 105, 111, 110, 58, 58, 117, 110, 119, 114, 97, 112, 40, 41, 96, 32, 111, 110, 32, 97, 32, 96, 78, 111, 110, 101, 96, 32, 118, 97, 108, 117, 101], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [8796093022207], len: Size { raw: 43 } }, size: Size { raw: 43 }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 43 })
87+
// mir::Constant
88+
// + span: $DIR/issue-59352.rs:14:26: 14:50
89+
// + literal: Const { ty: &str, val: Value(Slice { data: Allocation { bytes: [99, 97, 108, 108, 101, 100, 32, 96, 79, 112, 116, 105, 111, 110, 58, 58, 117, 110, 119, 114, 97, 112, 40, 41, 96, 32, 111, 110, 32, 97, 32, 96, 78, 111, 110, 101, 96, 32, 118, 97, 108, 117, 101], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [8796093022207], len: Size { raw: 43 } }, size: Size { raw: 43 }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 43 }) }
90+
}
91+
92+
bb7: {
93+
unreachable; // scope 3 at $DIR/issue-59352.rs:14:26: 14:50
94+
}
95+
96+
bb8: {
97+
_0 = move ((_3 as Some).0: u32); // scope 3 at $DIR/issue-59352.rs:14:26: 14:50
98+
StorageDead(_9); // scope 0 at $DIR/issue-59352.rs:14:26: 14:50
99+
StorageDead(_3); // scope 0 at $DIR/issue-59352.rs:14:49: 14:50
100+
goto -> bb4; // scope 0 at $DIR/issue-59352.rs:14:5: 14:63
101+
}
102+
}

0 commit comments

Comments
 (0)