-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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 #66129 - Nadrieril:refactor-slice-pat-usefulness, r=varkor
Refactor slice pattern usefulness checking As a follow up to #65874, this PR changes how variable-length slice patterns are handled in usefulness checking. The objectives are: cleaning up that code to make it easier to understand, and paving the way to handling fixed-length slices more cleverly too, for #53820. Before this, variable-length slice patterns were eagerly expanded into a union of fixed-length slices. Now they have their own special constructor, which allows expanding them a bit more lazily. As a nice side-effect, this improves diagnostics. This PR shows a slight performance improvement, mostly due to 149792b. This will probably have to be reverted in some way when we implement or-patterns.
- Loading branch information
Showing
21 changed files
with
627 additions
and
264 deletions.
There are no files selected for viewing
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
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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
75 changes: 75 additions & 0 deletions
75
src/test/ui/pattern/usefulness/slice-patterns-exhaustiveness.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,75 @@ | ||
#![feature(slice_patterns)] | ||
|
||
fn main() { | ||
let s: &[bool] = &[true; 0]; | ||
let s1: &[bool; 1] = &[false; 1]; | ||
let s2: &[bool; 2] = &[false; 2]; | ||
let s3: &[bool; 3] = &[false; 3]; | ||
|
||
match s1 { | ||
[true, ..] => {} | ||
[.., false] => {} | ||
} | ||
match s2 { | ||
//~^ ERROR `&[false, true]` not covered | ||
[true, ..] => {} | ||
[.., false] => {} | ||
} | ||
match s3 { | ||
//~^ ERROR `&[false, _, true]` not covered | ||
[true, ..] => {} | ||
[.., false] => {} | ||
} | ||
match s { | ||
//~^ ERROR `&[false, .., true]` not covered | ||
[] => {} | ||
[true, ..] => {} | ||
[.., false] => {} | ||
} | ||
|
||
match s3 { | ||
//~^ ERROR `&[false, _, _]` not covered | ||
[true, .., true] => {} | ||
} | ||
match s { | ||
//~^ ERROR `&[_, ..]` not covered | ||
[] => {} | ||
} | ||
match s { | ||
//~^ ERROR `&[_, _, ..]` not covered | ||
[] => {} | ||
[_] => {} | ||
} | ||
match s { | ||
//~^ ERROR `&[false, ..]` not covered | ||
[] => {} | ||
[true, ..] => {} | ||
} | ||
match s { | ||
//~^ ERROR `&[false, _, ..]` not covered | ||
[] => {} | ||
[_] => {} | ||
[true, ..] => {} | ||
} | ||
match s { | ||
//~^ ERROR `&[_, .., false]` not covered | ||
[] => {} | ||
[_] => {} | ||
[.., true] => {} | ||
} | ||
|
||
match s { | ||
//~^ ERROR `&[_, _, .., true]` not covered | ||
[] => {} | ||
[_] => {} | ||
[_, _] => {} | ||
[.., false] => {} | ||
} | ||
match s { | ||
//~^ ERROR `&[true, _, .., _]` not covered | ||
[] => {} | ||
[_] => {} | ||
[_, _] => {} | ||
[false, .., false] => {} | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
src/test/ui/pattern/usefulness/slice-patterns-exhaustiveness.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,91 @@ | ||
error[E0004]: non-exhaustive patterns: `&[false, true]` not covered | ||
--> $DIR/slice-patterns-exhaustiveness.rs:13:11 | ||
| | ||
LL | match s2 { | ||
| ^^ pattern `&[false, true]` not covered | ||
| | ||
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms | ||
|
||
error[E0004]: non-exhaustive patterns: `&[false, _, true]` not covered | ||
--> $DIR/slice-patterns-exhaustiveness.rs:18:11 | ||
| | ||
LL | match s3 { | ||
| ^^ pattern `&[false, _, true]` not covered | ||
| | ||
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms | ||
|
||
error[E0004]: non-exhaustive patterns: `&[false, .., true]` not covered | ||
--> $DIR/slice-patterns-exhaustiveness.rs:23:11 | ||
| | ||
LL | match s { | ||
| ^ pattern `&[false, .., true]` not covered | ||
| | ||
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms | ||
|
||
error[E0004]: non-exhaustive patterns: `&[false, _, _]` not covered | ||
--> $DIR/slice-patterns-exhaustiveness.rs:30:11 | ||
| | ||
LL | match s3 { | ||
| ^^ pattern `&[false, _, _]` not covered | ||
| | ||
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms | ||
|
||
error[E0004]: non-exhaustive patterns: `&[_, ..]` not covered | ||
--> $DIR/slice-patterns-exhaustiveness.rs:34:11 | ||
| | ||
LL | match s { | ||
| ^ pattern `&[_, ..]` not covered | ||
| | ||
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms | ||
|
||
error[E0004]: non-exhaustive patterns: `&[_, _, ..]` not covered | ||
--> $DIR/slice-patterns-exhaustiveness.rs:38:11 | ||
| | ||
LL | match s { | ||
| ^ pattern `&[_, _, ..]` not covered | ||
| | ||
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms | ||
|
||
error[E0004]: non-exhaustive patterns: `&[false, ..]` not covered | ||
--> $DIR/slice-patterns-exhaustiveness.rs:43:11 | ||
| | ||
LL | match s { | ||
| ^ pattern `&[false, ..]` not covered | ||
| | ||
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms | ||
|
||
error[E0004]: non-exhaustive patterns: `&[false, _, ..]` not covered | ||
--> $DIR/slice-patterns-exhaustiveness.rs:48:11 | ||
| | ||
LL | match s { | ||
| ^ pattern `&[false, _, ..]` not covered | ||
| | ||
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms | ||
|
||
error[E0004]: non-exhaustive patterns: `&[_, .., false]` not covered | ||
--> $DIR/slice-patterns-exhaustiveness.rs:54:11 | ||
| | ||
LL | match s { | ||
| ^ pattern `&[_, .., false]` not covered | ||
| | ||
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms | ||
|
||
error[E0004]: non-exhaustive patterns: `&[_, _, .., true]` not covered | ||
--> $DIR/slice-patterns-exhaustiveness.rs:61:11 | ||
| | ||
LL | match s { | ||
| ^ pattern `&[_, _, .., true]` not covered | ||
| | ||
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms | ||
|
||
error[E0004]: non-exhaustive patterns: `&[true, _, .., _]` not covered | ||
--> $DIR/slice-patterns-exhaustiveness.rs:68:11 | ||
| | ||
LL | match s { | ||
| ^ pattern `&[true, _, .., _]` not covered | ||
| | ||
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms | ||
|
||
error: aborting due to 11 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0004`. |
27 changes: 27 additions & 0 deletions
27
src/test/ui/pattern/usefulness/slice-patterns-irrefutable.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,27 @@ | ||
// check-pass | ||
#![feature(slice_patterns)] | ||
|
||
fn main() { | ||
let s: &[bool] = &[true; 0]; | ||
let s0: &[bool; 0] = &[]; | ||
let s1: &[bool; 1] = &[false; 1]; | ||
let s2: &[bool; 2] = &[false; 2]; | ||
|
||
let [] = s0; | ||
let [_] = s1; | ||
let [_, _] = s2; | ||
|
||
let [..] = s; | ||
let [..] = s0; | ||
let [..] = s1; | ||
let [..] = s2; | ||
|
||
let [_, ..] = s1; | ||
let [.., _] = s1; | ||
let [_, ..] = s2; | ||
let [.., _] = s2; | ||
|
||
let [_, _, ..] = s2; | ||
let [_, .., _] = s2; | ||
let [.., _, _] = s2; | ||
} |
26 changes: 26 additions & 0 deletions
26
src/test/ui/pattern/usefulness/slice-patterns-reachability.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,26 @@ | ||
#![feature(slice_patterns)] | ||
#![deny(unreachable_patterns)] | ||
|
||
fn main() { | ||
let s: &[bool] = &[true; 0]; | ||
|
||
match s { | ||
[true, ..] => {} | ||
[true, ..] => {} //~ ERROR unreachable pattern | ||
[true] => {} //~ ERROR unreachable pattern | ||
[..] => {} | ||
} | ||
match s { | ||
[.., true] => {} | ||
[.., true] => {} //~ ERROR unreachable pattern | ||
[true] => {} //~ ERROR unreachable pattern | ||
[..] => {} | ||
} | ||
match s { | ||
[false, .., true] => {} | ||
[false, .., true] => {} //~ ERROR unreachable pattern | ||
[false, true] => {} //~ ERROR unreachable pattern | ||
[false] => {} | ||
[..] => {} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/test/ui/pattern/usefulness/slice-patterns-reachability.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,44 @@ | ||
error: unreachable pattern | ||
--> $DIR/slice-patterns-reachability.rs:9:9 | ||
| | ||
LL | [true, ..] => {} | ||
| ^^^^^^^^^^ | ||
| | ||
note: lint level defined here | ||
--> $DIR/slice-patterns-reachability.rs:2:9 | ||
| | ||
LL | #![deny(unreachable_patterns)] | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: unreachable pattern | ||
--> $DIR/slice-patterns-reachability.rs:10:9 | ||
| | ||
LL | [true] => {} | ||
| ^^^^^^ | ||
|
||
error: unreachable pattern | ||
--> $DIR/slice-patterns-reachability.rs:15:9 | ||
| | ||
LL | [.., true] => {} | ||
| ^^^^^^^^^^ | ||
|
||
error: unreachable pattern | ||
--> $DIR/slice-patterns-reachability.rs:16:9 | ||
| | ||
LL | [true] => {} | ||
| ^^^^^^ | ||
|
||
error: unreachable pattern | ||
--> $DIR/slice-patterns-reachability.rs:21:9 | ||
| | ||
LL | [false, .., true] => {} | ||
| ^^^^^^^^^^^^^^^^^ | ||
|
||
error: unreachable pattern | ||
--> $DIR/slice-patterns-reachability.rs:22:9 | ||
| | ||
LL | [false, true] => {} | ||
| ^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 6 previous errors | ||
|
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