1
Fork 0

Print actual enum variant

This commit is contained in:
Dániel Buga 2021-01-02 11:06:30 +01:00
parent eb0d5be441
commit e0300716ef
3 changed files with 39 additions and 16 deletions

View file

@ -1381,19 +1381,42 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ty, ty,
); );
match variant.ctor_kind { match variant.ctor_kind {
CtorKind::Fn => { CtorKind::Fn => match ty.kind() {
err.span_label(variant.ident.span, format!("`{adt}` defined here", adt = ty)); ty::Adt(adt, ..) if adt.is_enum() => {
err.span_label(field.ident.span, "field does not exist"); err.span_label(
err.span_label( variant.ident.span,
ty_span, format!(
format!( "`{adt}::{variant}` defined here",
"`{adt}` is a tuple {kind_name}, \ adt = ty,
use the appropriate syntax: `{adt}(/* fields */)`", variant = variant.ident,
adt = ty, ),
kind_name = kind_name );
), 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 // prevent all specified fields from being suggested
let skip_fields = skip_fields.iter().map(|ref x| x.ident.name); let skip_fields = skip_fields.iter().map(|ref x| x.ident.name);

View file

@ -4,7 +4,7 @@ pub enum Enum {
} }
pub fn foo(x: i32) -> Enum { pub fn foo(x: i32) -> Enum {
Enum::V1 { x } //~ ERROR field does not exist Enum::V1 { x } //~ ERROR `Enum::V1` has no field named `x`
} }
fn main() {} fn main() {}

View file

@ -2,12 +2,12 @@ error[E0559]: variant `Enum::V1` has no field named `x`
--> $DIR/issue-80607.rs:7:16 --> $DIR/issue-80607.rs:7:16
| |
LL | V1(i32), LL | V1(i32),
| -- `Enum` defined here | -- `Enum::V1` defined here
... ...
LL | Enum::V1 { x } LL | Enum::V1 { x }
| -------- ^ field does not exist | -------- ^ field does not exist
| | | |
| `Enum` is a tuple variant, use the appropriate syntax: `Enum(/* fields */)` | `Enum::V1` is a tuple variant, use the appropriate syntax: `Enum::V1(/* fields */)`
error: aborting due to previous error error: aborting due to previous error