Skip to content

Commit

Permalink
Rollup merge of #69891 - Centril:fix-69875, r=varkor
Browse files Browse the repository at this point in the history
Exhaustiveness checking, `Matrix::push`: recursively expand or-patterns

> There's an implicit invariant that there should be no or-patterns directly in the first column of the matrix, but this invariant is broken exactly when an or-pattern has a child that is itself an or-pattern.

Here we preserve this broken invariant by recursively expanding `PatKind::Or`s in `Matrix::push`.
Fixes #69875.

r? @varkor
cc @Nadrieril
cc #54883
  • Loading branch information
Centril committed Mar 11, 2020
2 parents 62e3dae + 4d16c21 commit a05bab5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/librustc_mir_build/hair/pattern/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,11 @@ impl<'p, 'tcx> Matrix<'p, 'tcx> {
/// Pushes a new row to the matrix. If the row starts with an or-pattern, this expands it.
crate fn push(&mut self, row: PatStack<'p, 'tcx>) {
if let Some(rows) = row.expand_or_pat() {
self.0.extend(rows);
for row in rows {
// We recursively expand the or-patterns of the new rows.
// This is necessary as we might have `0 | (1 | 2)` or e.g., `x @ 0 | x @ (1 | 2)`.
self.push(row)
}
} else {
self.0.push(row);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![feature(or_patterns)]

fn main() {
let 0 | (1 | 2) = 0; //~ ERROR refutable pattern in local binding
match 0 {
//~^ ERROR non-exhaustive patterns
0 | (1 | 2) => {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
error[E0005]: refutable pattern in local binding: `std::i32::MIN..=-1i32` and `3i32..=std::i32::MAX` not covered
--> $DIR/issue-69875-should-have-been-expanded-earlier-non-exhaustive.rs:4:9
|
LL | let 0 | (1 | 2) = 0;
| ^^^^^^^^^^^ patterns `std::i32::MIN..=-1i32` and `3i32..=std::i32::MAX` not covered
|
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
help: you might want to use `if let` to ignore the variant that isn't matched
|
LL | if let 0 | (1 | 2) = 0 { /* */ }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0004]: non-exhaustive patterns: `std::i32::MIN..=-1i32` and `3i32..=std::i32::MAX` not covered
--> $DIR/issue-69875-should-have-been-expanded-earlier-non-exhaustive.rs:5:11
|
LL | match 0 {
| ^ patterns `std::i32::MIN..=-1i32` and `3i32..=std::i32::MAX` not covered
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0004, E0005.
For more information about an error, try `rustc --explain E0004`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// check-pass

#![feature(or_patterns)]

fn main() {
let 0 | (1 | _) = 0;
if let 0 | (1 | 2) = 0 {}
if let x @ 0 | x @ (1 | 2) = 0 {}
}

0 comments on commit a05bab5

Please sign in to comment.