Skip to content

Commit a29b68f

Browse files
authoredNov 15, 2020
Rollup merge of #78856 - mark-i-m:fix-or-pat-ice, r=matthewjasper
Explicitly checking for or-pattern before test Fixes #72680 cc #54883 r? ````@varkor````
2 parents ae7020f + b825ae7 commit a29b68f

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
 

‎compiler/rustc_mir_build/src/build/matches/test.rs

+11
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,17 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
671671
(&TestKind::Range { .. }, _) => None,
672672

673673
(&TestKind::Eq { .. } | &TestKind::Len { .. }, _) => {
674+
// The call to `self.test(&match_pair)` below is not actually used to generate any
675+
// MIR. Instead, we just want to compare with `test` (the parameter of the method)
676+
// to see if it is the same.
677+
//
678+
// However, at this point we can still encounter or-patterns that were extracted
679+
// from previous calls to `sort_candidate`, so we need to manually address that
680+
// case to avoid panicking in `self.test()`.
681+
if let PatKind::Or { .. } = &*match_pair.pattern.kind {
682+
return None;
683+
}
684+
674685
// These are all binary tests.
675686
//
676687
// FIXME(#29623) we can be more clever here

‎src/test/ui/match/issue-72680.rs

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// run-pass
2+
3+
#![feature(or_patterns)]
4+
5+
fn main() {
6+
assert!(f("", 0));
7+
assert!(f("a", 1));
8+
assert!(f("b", 1));
9+
10+
assert!(!f("", 1));
11+
assert!(!f("a", 0));
12+
assert!(!f("b", 0));
13+
14+
assert!(!f("asdf", 32));
15+
16+
////
17+
18+
assert!(!g(true, true, true));
19+
assert!(!g(false, true, true));
20+
assert!(!g(true, false, true));
21+
assert!(!g(false, false, true));
22+
assert!(!g(true, true, false));
23+
24+
assert!(g(false, true, false));
25+
assert!(g(true, false, false));
26+
assert!(g(false, false, false));
27+
28+
////
29+
30+
assert!(!h(true, true, true));
31+
assert!(!h(false, true, true));
32+
assert!(!h(true, false, true));
33+
assert!(!h(false, false, true));
34+
assert!(!h(true, true, false));
35+
36+
assert!(h(false, true, false));
37+
assert!(h(true, false, false));
38+
assert!(h(false, false, false));
39+
}
40+
41+
fn f(s: &str, num: usize) -> bool {
42+
match (s, num) {
43+
("", 0) | ("a" | "b", 1) => true,
44+
45+
_ => false,
46+
}
47+
}
48+
49+
fn g(x: bool, y: bool, z: bool) -> bool {
50+
match (x, y, x, z) {
51+
(true | false, false, true, false) => true,
52+
(false, true | false, true | false, false) => true,
53+
(true | false, true | false, true | false, true) => false,
54+
(true, true | false, true | false, false) => false,
55+
}
56+
}
57+
58+
fn h(x: bool, y: bool, z: bool) -> bool {
59+
match (x, (y, (x, (z,)))) {
60+
(true | false, (false, (true, (false,)))) => true,
61+
(false, (true | false, (true | false, (false,)))) => true,
62+
(true | false, (true | false, (true | false, (true,)))) => false,
63+
(true, (true | false, (true | false, (false,)))) => false,
64+
}
65+
}

0 commit comments

Comments
 (0)
Please sign in to comment.