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 ```
This commit is contained in:
commit
69b352ef77
7 changed files with 88 additions and 6 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue