Rollup merge of #80613 - bugadani:issue-80607, r=matthewjasper
Diag: print enum variant instead of enum type Closes #80607
This commit is contained in:
commit
4172756c80
3 changed files with 60 additions and 13 deletions
|
@ -1381,19 +1381,42 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
ty,
|
||||
);
|
||||
match variant.ctor_kind {
|
||||
CtorKind::Fn => {
|
||||
err.span_label(variant.ident.span, format!("`{adt}` defined here", adt = ty));
|
||||
err.span_label(field.ident.span, "field does not exist");
|
||||
err.span_label(
|
||||
ty_span,
|
||||
format!(
|
||||
"`{adt}` is a tuple {kind_name}, \
|
||||
use the appropriate syntax: `{adt}(/* fields */)`",
|
||||
adt = ty,
|
||||
kind_name = kind_name
|
||||
),
|
||||
);
|
||||
}
|
||||
CtorKind::Fn => match ty.kind() {
|
||||
ty::Adt(adt, ..) if adt.is_enum() => {
|
||||
err.span_label(
|
||||
variant.ident.span,
|
||||
format!(
|
||||
"`{adt}::{variant}` defined here",
|
||||
adt = ty,
|
||||
variant = variant.ident,
|
||||
),
|
||||
);
|
||||
err.span_label(field.ident.span, "field does not exist");
|
||||
err.span_label(
|
||||
ty_span,
|
||||
format!(
|
||||
"`{adt}::{variant}` is a tuple {kind_name}, \
|
||||
use the appropriate syntax: `{adt}::{variant}(/* fields */)`",
|
||||
adt = ty,
|
||||
variant = variant.ident,
|
||||
kind_name = kind_name
|
||||
),
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
err.span_label(variant.ident.span, format!("`{adt}` defined here", adt = ty));
|
||||
err.span_label(field.ident.span, "field does not exist");
|
||||
err.span_label(
|
||||
ty_span,
|
||||
format!(
|
||||
"`{adt}` is a tuple {kind_name}, \
|
||||
use the appropriate syntax: `{adt}(/* fields */)`",
|
||||
adt = ty,
|
||||
kind_name = kind_name
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
// prevent all specified fields from being suggested
|
||||
let skip_fields = skip_fields.iter().map(|ref x| x.ident.name);
|
||||
|
|
10
src/test/ui/issues/issue-80607.rs
Normal file
10
src/test/ui/issues/issue-80607.rs
Normal file
|
@ -0,0 +1,10 @@
|
|||
// This tests makes sure the diagnostics print the offending enum variant, not just the type.
|
||||
pub enum Enum {
|
||||
V1(i32),
|
||||
}
|
||||
|
||||
pub fn foo(x: i32) -> Enum {
|
||||
Enum::V1 { x } //~ ERROR `Enum::V1` has no field named `x`
|
||||
}
|
||||
|
||||
fn main() {}
|
14
src/test/ui/issues/issue-80607.stderr
Normal file
14
src/test/ui/issues/issue-80607.stderr
Normal file
|
@ -0,0 +1,14 @@
|
|||
error[E0559]: variant `Enum::V1` has no field named `x`
|
||||
--> $DIR/issue-80607.rs:7:16
|
||||
|
|
||||
LL | V1(i32),
|
||||
| -- `Enum::V1` defined here
|
||||
...
|
||||
LL | Enum::V1 { x }
|
||||
| -------- ^ field does not exist
|
||||
| |
|
||||
| `Enum::V1` is a tuple variant, use the appropriate syntax: `Enum::V1(/* fields */)`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0559`.
|
Loading…
Add table
Add a link
Reference in a new issue