Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typeck: Emit structured suggestions for tuple struct syntax #81737

Merged
merged 1 commit into from
Feb 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions compiler/rustc_typeck/src/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1460,28 +1460,33 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
),
);
err.span_label(field.ident.span, "field does not exist");
err.span_label(
err.span_suggestion(
ty_span,
&format!(
"`{adt}::{variant}` is a tuple {kind_name}, use the appropriate syntax",
adt = ty,
variant = variant.ident,
),
format!(
"`{adt}::{variant}` is a tuple {kind_name}, \
use the appropriate syntax: `{adt}::{variant}(/* fields */)`",
"{adt}::{variant}(/* fields */)",
adt = ty,
variant = variant.ident,
kind_name = kind_name
),
Applicability::HasPlaceholders,
);
}
_ => {
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(
err.span_suggestion(
ty_span,
format!(
"`{adt}` is a tuple {kind_name}, \
use the appropriate syntax: `{adt}(/* fields */)`",
&format!(
"`{adt}` is a tuple {kind_name}, use the appropriate syntax",
adt = ty,
kind_name = kind_name
kind_name = kind_name,
),
format!("{adt}(/* fields */)", adt = ty),
Applicability::HasPlaceholders,
);
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-4736.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | struct NonCopyable(());
LL | let z = NonCopyable{ p: () };
| ----------- ^ 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

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-80607.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | V1(i32),
LL | Enum::V1 { x }
| -------- ^ 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

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/numeric/numeric-fields.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | struct S(u8, u16);
LL | let s = S{0b1: 10, 0: 11};
| - ^^^ 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`
--> $DIR/numeric-fields.rs:7:17
Expand Down