-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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 #85233 - FabianWolff:issue-85227, r=petrochenkov
Improve error message for non-exhaustive matches on non-exhaustive enums This pull request fixes #85227. For an enum marked with `#[non_exhaustive]` and not defined in the current crate, the error message for non-exhaustive matches now mentions the fact that the enum is marked as non-exhaustive: ``` error[E0004]: non-exhaustive patterns: `_` not covered --> main.rs:12:11 | 12 | match e { | ^ pattern `_` not covered | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms = note: the matched value is of type `E`, which is marked as non-exhaustive ```
- Loading branch information
Showing
7 changed files
with
88 additions
and
6 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#[non_exhaustive] | ||
pub enum E1 {} | ||
|
||
#[non_exhaustive] | ||
pub enum E2 { A, B } |
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,32 @@ | ||
// aux-build:match_non_exhaustive_lib.rs | ||
|
||
/* The error message for non-exhaustive matches on non-local enums | ||
* marked as non-exhaustive should mention the fact that the enum | ||
* is marked as non-exhaustive (issue #85227). | ||
*/ | ||
|
||
// Ignore non_exhaustive in the same crate | ||
#[non_exhaustive] | ||
enum L { A, B } | ||
|
||
extern crate match_non_exhaustive_lib; | ||
use match_non_exhaustive_lib::{E1, E2}; | ||
|
||
fn foo() -> L {todo!()} | ||
fn bar() -> (E1, E2) {todo!()} | ||
|
||
fn main() { | ||
let l = foo(); | ||
// No error for enums defined in this crate | ||
match l { L::A => (), L::B => () }; | ||
// (except if the match is already non-exhaustive) | ||
match l { L::A => () }; | ||
//~^ ERROR: non-exhaustive patterns: `B` not covered [E0004] | ||
|
||
// E1 is not visibly uninhabited from here | ||
let (e1, e2) = bar(); | ||
match e1 {}; | ||
//~^ ERROR: non-exhaustive patterns: type `E1` is non-empty [E0004] | ||
match e2 { E2::A => (), E2::B => () }; | ||
//~^ ERROR: non-exhaustive patterns: `_` not covered [E0004] | ||
} |
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,36 @@ | ||
error[E0004]: non-exhaustive patterns: `B` not covered | ||
--> $DIR/match_non_exhaustive.rs:23:11 | ||
| | ||
LL | enum L { A, B } | ||
| --------------- | ||
| | | | ||
| | not covered | ||
| `L` defined here | ||
... | ||
LL | match l { L::A => () }; | ||
| ^ pattern `B` not covered | ||
| | ||
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms | ||
= note: the matched value is of type `L` | ||
|
||
error[E0004]: non-exhaustive patterns: type `E1` is non-empty | ||
--> $DIR/match_non_exhaustive.rs:28:11 | ||
| | ||
LL | match e1 {}; | ||
| ^^ | ||
| | ||
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms | ||
= note: the matched value is of type `E1`, which is marked as non-exhaustive | ||
|
||
error[E0004]: non-exhaustive patterns: `_` not covered | ||
--> $DIR/match_non_exhaustive.rs:30:11 | ||
| | ||
LL | match e2 { E2::A => (), E2::B => () }; | ||
| ^^ pattern `_` not covered | ||
| | ||
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms | ||
= note: the matched value is of type `E2`, which is marked as non-exhaustive | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0004`. |
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