Skip to content

Commit 90abfe9

Browse files
committed
expand inner or pattern
1 parent 3a8b014 commit 90abfe9

File tree

7 files changed

+88
-1
lines changed

7 files changed

+88
-1
lines changed

compiler/rustc_mir_build/src/thir/pattern/usefulness.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,17 @@ impl<'p, 'tcx> Matrix<'p, 'tcx> {
453453
/// expands it.
454454
fn push(&mut self, row: PatStack<'p, 'tcx>) {
455455
if !row.is_empty() && row.head().is_or_pat() {
456-
self.patterns.extend(row.expand_or_pat());
456+
let pats = row.expand_or_pat();
457+
let mut no_inner_or = true;
458+
for pat in pats {
459+
if !pat.is_empty() && pat.head().is_or_pat() {
460+
self.patterns.extend(pat.expand_or_pat());
461+
no_inner_or = false;
462+
}
463+
}
464+
if no_inner_or {
465+
self.patterns.extend(row.expand_or_pat());
466+
}
457467
} else {
458468
self.patterns.push(row);
459469
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#[allow(unused_variables)]
2+
#[allow(unused_parens)]
3+
fn main() {
4+
let x = "foo";
5+
match x {
6+
x @ ((("h" | "ho" | "yo" | ("dude" | "w")) | () | "nop") | ("hey" | "gg")) |
7+
//~^ ERROR mismatched types
8+
x @ ("black" | "pink") |
9+
x @ ("red" | "blue") => {
10+
}
11+
_ => (),
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/inner-or-pat-2.rs:6:54
3+
|
4+
LL | match x {
5+
| - this expression has type `&str`
6+
LL | x @ ((("h" | "ho" | "yo" | ("dude" | "w")) | () | "nop") | ("hey" | "gg")) |
7+
| ^^ expected `str`, found `()`
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0308`.
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// run-pass
2+
3+
#[allow(unreachable_patterns)]
4+
#[allow(unused_variables)]
5+
#[allow(unused_parens)]
6+
fn main() {
7+
let x = "foo";
8+
9+
match x {
10+
x @ ("foo" | "bar") |
11+
(x @ "red" | (x @ "blue" | x @ "red")) => {
12+
}
13+
_ => (),
14+
}
15+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#[allow(unused_variables)]
2+
#[allow(unused_parens)]
3+
fn main() {
4+
let x = "foo";
5+
6+
match x {
7+
x @ ("foo" | "bar") |
8+
(x @ "red" | (x @ "blue" | "red")) => {
9+
//~^ variable `x` is not bound in all patterns
10+
}
11+
_ => (),
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0408]: variable `x` is not bound in all patterns
2+
--> $DIR/inner-or-pat-4.rs:8:37
3+
|
4+
LL | (x @ "red" | (x @ "blue" | "red")) => {
5+
| - ^^^^^ pattern doesn't bind `x`
6+
| |
7+
| variable not in all patterns
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0408`.
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// run-pass
2+
3+
#[allow(unused_variables)]
4+
#[allow(unused_parens)]
5+
fn main() {
6+
let x = "foo";
7+
match x {
8+
x @ ((("h" | "ho" | "yo" | ("dude" | "w")) | "no" | "nop") | ("hey" | "gg")) |
9+
x @ ("black" | "pink") |
10+
x @ ("red" | "blue") => {
11+
}
12+
_ => (),
13+
}
14+
}

0 commit comments

Comments
 (0)