typeck: Emit structured suggestions for tuple struct syntax

And tuple variant syntax, but that didn't fit in the subject :)

Now the fact that these are suggestions is exposed both to the layout
engine and to IDEs and rustfix for automatic application.
This commit is contained in:
Camelid 2021-02-03 21:29:00 -08:00
parent e708cbd91c
commit ed6232927b
4 changed files with 18 additions and 13 deletions

View File

@ -1460,28 +1460,33 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
), ),
); );
err.span_label(field.ident.span, "field does not exist"); err.span_label(field.ident.span, "field does not exist");
err.span_label( err.span_suggestion(
ty_span, ty_span,
format!( &format!(
"`{adt}::{variant}` is a tuple {kind_name}, \ "`{adt}::{variant}` is a tuple {kind_name}, use the appropriate syntax",
use the appropriate syntax: `{adt}::{variant}(/* fields */)`",
adt = ty, adt = ty,
variant = variant.ident, variant = variant.ident,
kind_name = kind_name
), ),
format!(
"{adt}::{variant}(/* fields */)",
adt = ty,
variant = variant.ident,
),
Applicability::HasPlaceholders,
); );
} }
_ => { _ => {
err.span_label(variant.ident.span, format!("`{adt}` defined here", adt = ty)); 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(field.ident.span, "field does not exist");
err.span_label( err.span_suggestion(
ty_span, ty_span,
format!( &format!(
"`{adt}` is a tuple {kind_name}, \ "`{adt}` is a tuple {kind_name}, use the appropriate syntax",
use the appropriate syntax: `{adt}(/* fields */)`",
adt = ty, adt = ty,
kind_name = kind_name kind_name = kind_name,
), ),
format!("{adt}(/* fields */)", adt = ty),
Applicability::HasPlaceholders,
); );
} }
}, },

View File

@ -7,7 +7,7 @@ LL | struct NonCopyable(());
LL | let z = NonCopyable{ p: () }; LL | let z = NonCopyable{ p: () };
| ----------- ^ field does not exist | ----------- ^ field does not exist
| | | |
| `NonCopyable` is a tuple struct, use the appropriate syntax: `NonCopyable(/* fields */)` | help: `NonCopyable` is a tuple struct, use the appropriate syntax: `NonCopyable(/* fields */)`
error: aborting due to previous error error: aborting due to previous error

View File

@ -7,7 +7,7 @@ LL | V1(i32),
LL | Enum::V1 { x } LL | Enum::V1 { x }
| -------- ^ field does not exist | -------- ^ field does not exist
| | | |
| `Enum::V1` is a tuple variant, use the appropriate syntax: `Enum::V1(/* fields */)` | help: `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

View File

@ -7,7 +7,7 @@ LL | struct S(u8, u16);
LL | let s = S{0b1: 10, 0: 11}; LL | let s = S{0b1: 10, 0: 11};
| - ^^^ field does not exist | - ^^^ field does not exist
| | | |
| `S` is a tuple struct, use the appropriate syntax: `S(/* fields */)` | help: `S` is a tuple struct, use the appropriate syntax: `S(/* fields */)`
error[E0026]: struct `S` does not have a field named `0x1` error[E0026]: struct `S` does not have a field named `0x1`
--> $DIR/numeric-fields.rs:7:17 --> $DIR/numeric-fields.rs:7:17