-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Report unreachable subpatterns consistently #120097
Merged
bors
merged 3 commits into
rust-lang:master
from
Nadrieril:consistent_unreachable_subpats
Jan 22, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
44 changes: 44 additions & 0 deletions
44
tests/ui/rfcs/rfc-0000-never_patterns/unreachable.exh_pats.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/unreachable.rs:17:9 | ||
| | ||
LL | Err(!), | ||
| ^^^^^^ | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/unreachable.rs:7:9 | ||
| | ||
LL | #![deny(unreachable_patterns)] | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: unreachable pattern | ||
--> $DIR/unreachable.rs:20:19 | ||
| | ||
LL | let (Ok(_x) | Err(!)) = res_void; | ||
| ^^^^^^ | ||
|
||
error: unreachable pattern | ||
--> $DIR/unreachable.rs:22:12 | ||
| | ||
LL | if let Err(!) = res_void {} | ||
| ^^^^^^ | ||
|
||
error: unreachable pattern | ||
--> $DIR/unreachable.rs:24:24 | ||
| | ||
LL | if let (Ok(true) | Err(!)) = res_void {} | ||
| ^^^^^^ | ||
|
||
error: unreachable pattern | ||
--> $DIR/unreachable.rs:26:23 | ||
| | ||
LL | for (Ok(mut _x) | Err(!)) in [res_void] {} | ||
| ^^^^^^ | ||
|
||
error: unreachable pattern | ||
--> $DIR/unreachable.rs:30:18 | ||
| | ||
LL | fn foo((Ok(_x) | Err(!)): Result<bool, Void>) {} | ||
| ^^^^^^ | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// revisions: normal exh_pats | ||
//[normal] check-pass | ||
#![feature(never_patterns)] | ||
#![allow(incomplete_features)] | ||
#![cfg_attr(exh_pats, feature(exhaustive_patterns))] | ||
#![allow(dead_code, unreachable_code)] | ||
#![deny(unreachable_patterns)] | ||
|
||
#[derive(Copy, Clone)] | ||
enum Void {} | ||
|
||
fn main() { | ||
let res_void: Result<bool, Void> = Ok(true); | ||
|
||
match res_void { | ||
Ok(_x) => {} | ||
Err(!), | ||
//[exh_pats]~^ ERROR unreachable | ||
} | ||
let (Ok(_x) | Err(!)) = res_void; | ||
//[exh_pats]~^ ERROR unreachable | ||
if let Err(!) = res_void {} | ||
//[exh_pats]~^ ERROR unreachable | ||
if let (Ok(true) | Err(!)) = res_void {} | ||
//[exh_pats]~^ ERROR unreachable | ||
for (Ok(mut _x) | Err(!)) in [res_void] {} | ||
//[exh_pats]~^ ERROR unreachable | ||
} | ||
|
||
fn foo((Ok(_x) | Err(!)): Result<bool, Void>) {} | ||
//[exh_pats]~^ ERROR unreachable |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// run-pass | ||
#![allow(unreachable_patterns)] | ||
|
||
#[derive(Copy, Clone)] | ||
#[allow(dead_code)] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, just double checking my understanding -- with exhaustive patterns, we don't need to care about never patterns b/c the scrutinee already disqualifies those patterns?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Said differently, never patterns are shorthand syntax:
Err(!)
is short forErr(_) => unreachable_unchecked!()
. The rules for which arms are required are orthogonal, and hereexhaustive_patterns
says "we don't need that arm". I've updated the tracking issue to clarify that while I was at it