Skip to content

Commit

Permalink
Auto merge of #117611 - Nadrieril:linear-pass-take-4, r=cjgillot
Browse files Browse the repository at this point in the history
Rewrite exhaustiveness in one pass

This is at least my 4th attempt at this in as many years x) Previous attempts were all too complicated or too slow. But we're finally here!

The previous version of the exhaustiveness algorithm computed reachability for each arm then exhaustiveness of the whole match. Since each of these steps does roughly the same things, this rewrites the algorithm to do them all in one go. I also think this makes things much simpler.

I also rewrote the documentation of the algorithm in depth. Hopefully it's up-to-date and easier to follow now. Plz comment if anything's unclear.

r? `@oli-obk` I think you're one of the rare other people to understand the exhaustiveness algorithm?

cc `@varkor` I know you're not active anymore, but if you feel like having a look you might enjoy this :D

Fixes #79307
  • Loading branch information
bors committed Nov 26, 2023
2 parents f5dc265 + 273cbb7 commit ee80c8d
Show file tree
Hide file tree
Showing 18 changed files with 1,294 additions and 1,002 deletions.
19 changes: 12 additions & 7 deletions compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,10 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
} else {
// Check the pattern for some things unrelated to exhaustiveness.
let refutable = if cx.refutable { Refutable } else { Irrefutable };
pat.walk_always(|pat| check_borrow_conflicts_in_at_patterns(self, pat));
pat.walk_always(|pat| check_for_bindings_named_same_as_variants(self, pat, refutable));
pat.walk_always(|pat| {
check_borrow_conflicts_in_at_patterns(self, pat);
check_for_bindings_named_same_as_variants(self, pat, refutable);
});
Ok(cx.pattern_arena.alloc(DeconstructedPat::from_pat(cx, pat)))
}
}
Expand All @@ -289,6 +291,7 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
&self,
refutability: RefutableFlag,
match_span: Option<Span>,
scrut_span: Span,
) -> MatchCheckCtxt<'p, 'tcx> {
let refutable = match refutability {
Irrefutable => false,
Expand All @@ -299,7 +302,9 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
param_env: self.param_env,
module: self.tcx.parent_module(self.lint_level).to_def_id(),
pattern_arena: self.pattern_arena,
match_lint_level: self.lint_level,
match_span,
scrut_span,
refutable,
}
}
Expand Down Expand Up @@ -330,7 +335,8 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
source: hir::MatchSource,
expr_span: Span,
) {
let cx = self.new_cx(Refutable, Some(expr_span));
let scrut = &self.thir[scrut];
let cx = self.new_cx(Refutable, Some(expr_span), scrut.span);

let mut tarms = Vec::with_capacity(arms.len());
for &arm in arms {
Expand All @@ -346,9 +352,8 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
}
}

let scrut = &self.thir[scrut];
let scrut_ty = scrut.ty;
let report = compute_match_usefulness(&cx, &tarms, self.lint_level, scrut_ty, scrut.span);
let report = compute_match_usefulness(&cx, &tarms, scrut_ty);

match source {
// Don't report arm reachability of desugared `match $iter.into_iter() { iter => .. }`
Expand Down Expand Up @@ -453,10 +458,10 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
pat: &Pat<'tcx>,
refutability: RefutableFlag,
) -> Result<(MatchCheckCtxt<'p, 'tcx>, UsefulnessReport<'p, 'tcx>), ErrorGuaranteed> {
let cx = self.new_cx(refutability, None);
let cx = self.new_cx(refutability, None, pat.span);
let pat = self.lower_pattern(&cx, pat)?;
let arms = [MatchArm { pat, hir_id: self.lint_level, has_guard: false }];
let report = compute_match_usefulness(&cx, &arms, self.lint_level, pat.ty(), pat.span());
let report = compute_match_usefulness(&cx, &arms, pat.ty());
Ok((cx, report))
}

Expand Down
367 changes: 177 additions & 190 deletions compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs

Large diffs are not rendered by default.

1,391 changes: 824 additions & 567 deletions compiler/rustc_mir_build/src/thir/pattern/usefulness.rs

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions tests/ui/or-patterns/exhaustiveness-pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ fn main() {
((0, 0) | (1, 0),) => {}
_ => {}
}
match ((0, 0),) {
// Note how the second one would be redundant without the guard.
((x, y) | (y, x),) if x == 0 => {}
_ => {}
}
match 0 {
// We don't warn the second one as redundant in general because of cases like the one above.
// We could technically do it if there are no bindings.
0 | 0 if 0 == 0 => {}
_ => {}
}

// This one caused ICE https://github.com/rust-lang/rust/issues/117378
match (0u8, 0) {
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/or-patterns/exhaustiveness-unreachable-pattern.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![deny(unreachable_patterns)]

// We wrap patterns in a tuple because top-level or-patterns were special-cased.
#[rustfmt::skip]
fn main() {
match (0u8,) {
(1 | 2,) => {}
Expand Down Expand Up @@ -73,6 +74,11 @@ fn main() {
| 0] => {} //~ ERROR unreachable
_ => {}
}
match (true, 0) {
(true, 0 | 0) => {} //~ ERROR unreachable
(_, 0 | 0) => {} //~ ERROR unreachable
_ => {}
}
match &[][..] {
[0] => {}
[0, _] => {}
Expand Down Expand Up @@ -149,4 +155,8 @@ fn main() {
| true, //~ ERROR unreachable
false | true) => {}
}
match (true, true) {
(x, y)
| (y, x) => {} //~ ERROR unreachable
}
}
72 changes: 45 additions & 27 deletions tests/ui/or-patterns/exhaustiveness-unreachable-pattern.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:7:9
--> $DIR/exhaustiveness-unreachable-pattern.rs:8:9
|
LL | (1,) => {}
| ^^^^
Expand All @@ -11,128 +11,140 @@ LL | #![deny(unreachable_patterns)]
| ^^^^^^^^^^^^^^^^^^^^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:12:9
--> $DIR/exhaustiveness-unreachable-pattern.rs:13:9
|
LL | (2,) => {}
| ^^^^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:18:9
--> $DIR/exhaustiveness-unreachable-pattern.rs:19:9
|
LL | (1 | 2,) => {}
| ^^^^^^^^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:23:9
--> $DIR/exhaustiveness-unreachable-pattern.rs:24:9
|
LL | (1, 3) => {}
| ^^^^^^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:24:9
--> $DIR/exhaustiveness-unreachable-pattern.rs:25:9
|
LL | (1, 4) => {}
| ^^^^^^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:25:9
--> $DIR/exhaustiveness-unreachable-pattern.rs:26:9
|
LL | (2, 4) => {}
| ^^^^^^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:26:9
--> $DIR/exhaustiveness-unreachable-pattern.rs:27:9
|
LL | (2 | 1, 4) => {}
| ^^^^^^^^^^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:28:9
--> $DIR/exhaustiveness-unreachable-pattern.rs:29:9
|
LL | (1, 4 | 5) => {}
| ^^^^^^^^^^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:36:9
--> $DIR/exhaustiveness-unreachable-pattern.rs:37:9
|
LL | (Some(1),) => {}
| ^^^^^^^^^^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:37:9
--> $DIR/exhaustiveness-unreachable-pattern.rs:38:9
|
LL | (None,) => {}
| ^^^^^^^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:42:9
--> $DIR/exhaustiveness-unreachable-pattern.rs:43:9
|
LL | ((1..=4,),) => {}
| ^^^^^^^^^^^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:47:14
--> $DIR/exhaustiveness-unreachable-pattern.rs:48:14
|
LL | (1 | 1,) => {}
| ^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:51:19
--> $DIR/exhaustiveness-unreachable-pattern.rs:52:19
|
LL | (0 | 1) | 1 => {}
| ^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:57:14
--> $DIR/exhaustiveness-unreachable-pattern.rs:58:14
|
LL | 0 | (0 | 0) => {}
| ^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:57:18
--> $DIR/exhaustiveness-unreachable-pattern.rs:58:18
|
LL | 0 | (0 | 0) => {}
| ^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:65:13
--> $DIR/exhaustiveness-unreachable-pattern.rs:66:13
|
LL | / Some(
LL | | 0 | 0) => {}
| |______________________^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:71:15
--> $DIR/exhaustiveness-unreachable-pattern.rs:72:15
|
LL | | 0
| ^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:73:15
--> $DIR/exhaustiveness-unreachable-pattern.rs:74:15
|
LL | | 0] => {}
| ^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:81:10
--> $DIR/exhaustiveness-unreachable-pattern.rs:78:20
|
LL | (true, 0 | 0) => {}
| ^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:79:17
|
LL | (_, 0 | 0) => {}
| ^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:87:10
|
LL | [1
| ^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:93:10
--> $DIR/exhaustiveness-unreachable-pattern.rs:99:10
|
LL | [true
| ^^^^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:100:36
--> $DIR/exhaustiveness-unreachable-pattern.rs:106:36
|
LL | (true | false, None | Some(true
| ^^^^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:105:14
--> $DIR/exhaustiveness-unreachable-pattern.rs:111:14
|
LL | (true
| ^^^^
Expand All @@ -143,28 +155,34 @@ LL | (true | false, None | Some(t_or_f!())) => {}
= note: this error originates in the macro `t_or_f` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:116:14
--> $DIR/exhaustiveness-unreachable-pattern.rs:122:14
|
LL | Some(0
| ^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:135:19
--> $DIR/exhaustiveness-unreachable-pattern.rs:141:19
|
LL | | false) => {}
| ^^^^^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:143:15
--> $DIR/exhaustiveness-unreachable-pattern.rs:149:15
|
LL | | true) => {}
| ^^^^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:149:15
--> $DIR/exhaustiveness-unreachable-pattern.rs:155:15
|
LL | | true,
| ^^^^

error: aborting due to 26 previous errors
error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:160:15
|
LL | | (y, x) => {}
| ^^^^^^

error: aborting due to 29 previous errors

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0004]: non-exhaustive patterns: type `usize` is non-empty
--> $DIR/pointer-sized-int.rs:54:11
--> $DIR/pointer-sized-int.rs:59:11
|
LL | match 7usize {}
| ^^^^^^
Expand Down
Loading

0 comments on commit ee80c8d

Please sign in to comment.