Skip to content

Commit

Permalink
Auto merge of #85233 - FabianWolff:issue-85227, r=petrochenkov
Browse files Browse the repository at this point in the history
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
bors committed May 14, 2021
2 parents 91f2e2d + 57291b8 commit 69b352e
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 6 deletions.
11 changes: 10 additions & 1 deletion compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,12 +496,21 @@ fn non_exhaustive_match<'p, 'tcx>(
err.span_label(sp, pattern_not_covered_label(&witnesses, &joined_patterns));
};

let is_variant_list_non_exhaustive = match scrut_ty.kind() {
ty::Adt(def, _) if def.is_variant_list_non_exhaustive() && !def.did.is_local() => true,
_ => false,
};

adt_defined_here(cx, &mut err, scrut_ty, &witnesses);
err.help(
"ensure that all possible cases are being handled, \
possibly by adding wildcards or more match arms",
);
err.note(&format!("the matched value is of type `{}`", scrut_ty));
err.note(&format!(
"the matched value is of type `{}`{}",
scrut_ty,
if is_variant_list_non_exhaustive { ", which is marked as non-exhaustive" } else { "" }
));
if (scrut_ty == cx.tcx.types.usize || scrut_ty == cx.tcx.types.isize)
&& !is_empty_match
&& witnesses.len() == 1
Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/match/auxiliary/match_non_exhaustive_lib.rs
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 }
32 changes: 32 additions & 0 deletions src/test/ui/match/match_non_exhaustive.rs
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]
}
36 changes: 36 additions & 0 deletions src/test/ui/match/match_non_exhaustive.stderr
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`.
6 changes: 3 additions & 3 deletions src/test/ui/rfc-2008-non-exhaustive/enum.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | match x {}
| ^
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
= note: the matched value is of type `EmptyNonExhaustiveEnum`
= note: the matched value is of type `EmptyNonExhaustiveEnum`, which is marked as non-exhaustive

error[E0004]: non-exhaustive patterns: `_` not covered
--> $DIR/enum.rs:16:11
Expand All @@ -14,7 +14,7 @@ LL | match enum_unit {
| ^^^^^^^^^ 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 `NonExhaustiveEnum`
= note: the matched value is of type `NonExhaustiveEnum`, which is marked as non-exhaustive

error[E0004]: non-exhaustive patterns: `_` not covered
--> $DIR/enum.rs:23:11
Expand All @@ -23,7 +23,7 @@ LL | match enum_unit {};
| ^^^^^^^^^ 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 `NonExhaustiveEnum`
= note: the matched value is of type `NonExhaustiveEnum`, which is marked as non-exhaustive

error: aborting due to 3 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | match x {}
| ^
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
= note: the matched value is of type `UninhabitedEnum`
= note: the matched value is of type `UninhabitedEnum`, which is marked as non-exhaustive

error[E0004]: non-exhaustive patterns: type `UninhabitedStruct` is non-empty
--> $DIR/match.rs:23:11
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | match x {}
| ^
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
= note: the matched value is of type `UninhabitedEnum`
= note: the matched value is of type `UninhabitedEnum`, which is marked as non-exhaustive

error[E0004]: non-exhaustive patterns: type `UninhabitedStruct` is non-empty
--> $DIR/match_with_exhaustive_patterns.rs:26:11
Expand Down

0 comments on commit 69b352e

Please sign in to comment.