Skip to content

Commit 27a8c35

Browse files
committed
Add a MIR test ensuring we use an aggregate comparison when matching on a large fixed-length array
1 parent a39525f commit 27a8c35

3 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// MIR for `array_match` after built
2+
3+
fn array_match(_1: [u8; 4]) -> bool {
4+
debug x => _1;
5+
let mut _0: bool;
6+
let mut _2: &[u8; 4];
7+
let mut _3: bool;
8+
scope 1 {
9+
}
10+
11+
bb0: {
12+
PlaceMention(_1);
13+
_2 = &_1;
14+
_3 = <[u8; 4] as PartialEq>::eq(copy _2, const &*b"\x01\x02\x03\x04") -> [return: bb4, unwind: bb8];
15+
}
16+
17+
bb1: {
18+
_0 = const false;
19+
goto -> bb7;
20+
}
21+
22+
bb2: {
23+
falseEdge -> [real: bb6, imaginary: bb1];
24+
}
25+
26+
bb3: {
27+
goto -> bb1;
28+
}
29+
30+
bb4: {
31+
switchInt(move _3) -> [0: bb1, otherwise: bb2];
32+
}
33+
34+
bb5: {
35+
FakeRead(ForMatchedPlace(None), _1);
36+
unreachable;
37+
}
38+
39+
bb6: {
40+
_0 = const true;
41+
goto -> bb7;
42+
}
43+
44+
bb7: {
45+
return;
46+
}
47+
48+
bb8 (cleanup): {
49+
resume;
50+
}
51+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// MIR for `array_match` after built
2+
3+
fn array_match(_1: [u8; 4]) -> bool {
4+
debug x => _1;
5+
let mut _0: bool;
6+
let mut _2: &[u8; 4];
7+
let mut _3: bool;
8+
scope 1 {
9+
}
10+
11+
bb0: {
12+
PlaceMention(_1);
13+
_2 = &_1;
14+
_3 = <[u8; 4] as PartialEq>::eq(copy _2, const &*b"\x01\x02\x03\x04") -> [return: bb4, unwind: bb8];
15+
}
16+
17+
bb1: {
18+
_0 = const false;
19+
goto -> bb7;
20+
}
21+
22+
bb2: {
23+
falseEdge -> [real: bb6, imaginary: bb1];
24+
}
25+
26+
bb3: {
27+
goto -> bb1;
28+
}
29+
30+
bb4: {
31+
switchInt(move _3) -> [0: bb1, otherwise: bb2];
32+
}
33+
34+
bb5: {
35+
FakeRead(ForMatchedPlace(None), _1);
36+
unreachable;
37+
}
38+
39+
bb6: {
40+
_0 = const true;
41+
goto -> bb7;
42+
}
43+
44+
bb7: {
45+
return;
46+
}
47+
48+
bb8 (cleanup): {
49+
resume;
50+
}
51+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
2+
//@ compile-flags: -Zmir-opt-level=0
3+
4+
// Verify that matching against a constant array pattern produces a single
5+
// `PartialEq::eq` call rather than element-by-element comparisons.
6+
7+
#![crate_type = "lib"]
8+
9+
// EMIT_MIR aggregate_array_eq.array_match.built.after.mir
10+
pub fn array_match(x: [u8; 4]) -> bool {
11+
// CHECK-LABEL: fn array_match(
12+
// CHECK: <[u8; 4] as PartialEq>::eq
13+
// CHECK-NOT: switchInt(copy _1[
14+
matches!(x, [1, 2, 3, 4])
15+
}

0 commit comments

Comments
 (0)