-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #88950 - Nadrieril:deconstruct-pat, r=oli-obk
Add an intermediate representation to exhaustiveness checking The exhaustiveness checking algorithm keeps deconstructing patterns into a `Constructor` and some `Fields`, but does so a bit all over the place. This PR introduces a new representation for patterns that already has that information, so we only compute it once at the start. I find this makes code easier to follow. In particular `DeconstructedPat::specialize` is a lot simpler than what happened before, and more closely matches the description of the algorithm. I'm also hoping this could help for the project of librarifying exhaustiveness for rust_analyzer since it decouples the algorithm from `rustc_middle::Pat`.
- Loading branch information
Showing
10 changed files
with
893 additions
and
994 deletions.
There are no files selected for viewing
208 changes: 89 additions & 119 deletions
208
compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Large diffs are not rendered by default.
Oops, something went wrong.
964 changes: 598 additions & 366 deletions
964
compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs
Large diffs are not rendered by default.
Oops, something went wrong.
599 changes: 107 additions & 492 deletions
599
compiler/rustc_mir_build/src/thir/pattern/usefulness.rs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// check-pass | ||
// | ||
// Check that we don't ignore private fields in usefulness checking | ||
#![deny(unreachable_patterns)] | ||
|
||
mod inner { | ||
#[derive(PartialEq, Eq)] | ||
pub struct PrivateField { | ||
pub x: bool, | ||
y: bool, | ||
} | ||
|
||
pub const FOO: PrivateField = PrivateField { x: true, y: true }; | ||
pub const BAR: PrivateField = PrivateField { x: true, y: false }; | ||
} | ||
use inner::*; | ||
|
||
fn main() { | ||
match FOO { | ||
FOO => {} | ||
BAR => {} | ||
_ => {} | ||
} | ||
|
||
match FOO { | ||
FOO => {} | ||
PrivateField { x: true, .. } => {} | ||
_ => {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/test/ui/pattern/usefulness/issue-82772-match-box-as-struct.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// This used to ICE in exhaustiveness checking. Explanation here: | ||
// https://github.com/rust-lang/rust/issues/82772#issuecomment-905946768 | ||
fn main() { | ||
let Box { 1: _, .. }: Box<()>; //~ ERROR field `1` of | ||
let Box { .. }: Box<()>; | ||
} |
9 changes: 9 additions & 0 deletions
9
src/test/ui/pattern/usefulness/issue-82772-match-box-as-struct.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
error[E0451]: field `1` of struct `Box` is private | ||
--> $DIR/issue-82772-match-box-as-struct.rs:4:15 | ||
| | ||
LL | let Box { 1: _, .. }: Box<()>; | ||
| ^^^^ private field | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0451`. |