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

diagnostics: error messages when struct literals fail to parse #99030

Merged
merged 3 commits into from
Jul 13, 2022
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
18 changes: 18 additions & 0 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3012,6 +3012,11 @@ impl<'a> Parser<'a> {
}
};

let is_shorthand = parsed_field.as_ref().map_or(false, |f| f.is_shorthand);
// A shorthand field can be turned into a full field with `:`.
// We should point this out.
self.check_or_expected(!is_shorthand, TokenType::Token(token::Colon));

match self.expect_one_of(&[token::Comma], &[token::CloseDelim(close_delim)]) {
Ok(_) => {
if let Some(f) = parsed_field.or(recovery_field) {
Expand All @@ -3032,6 +3037,19 @@ impl<'a> Parser<'a> {
",",
Applicability::MachineApplicable,
);
} else if is_shorthand
&& (AssocOp::from_token(&self.token).is_some()
|| matches!(&self.token.kind, token::OpenDelim(_))
|| self.token.kind == token::Dot)
{
// Looks like they tried to write a shorthand, complex expression.
let ident = parsed_field.expect("is_shorthand implies Some").ident;
e.span_suggestion(
ident.span.shrink_to_lo(),
"try naming a field",
&format!("{ident}: "),
Applicability::HasPlaceholders,
);
}
}
if !recover {
Expand Down
7 changes: 4 additions & 3 deletions src/test/ui/parser/issues/issue-52496.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ error: float literals must have an integer part
LL | let _ = Foo { bar: .5, baz: 42 };
| ^^ help: must have an integer part: `0.5`

error: expected one of `,` or `}`, found `.`
error: expected one of `,`, `:`, or `}`, found `.`
--> $DIR/issue-52496.rs:8:22
|
LL | let _ = Foo { bar.into(), bat: -1, . };
| --- ^ expected one of `,` or `}`
| |
| --- - ^ expected one of `,`, `:`, or `}`
| | |
| | help: try naming a field: `bar:`
| while parsing this struct

error: expected identifier, found `.`
Expand Down
16 changes: 12 additions & 4 deletions src/test/ui/parser/issues/issue-62973.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,23 @@ LL |
LL |
| ^

error: expected one of `,` or `}`, found `{`
error: expected one of `,`, `:`, or `}`, found `{`
--> $DIR/issue-62973.rs:6:8
|
LL | fn p() { match s { v, E { [) {) }
| ^ - -^ expected one of `,` or `}`
| | | |
| | | help: `}` may belong here
| ^ - ^ expected one of `,`, `:`, or `}`
| | |
| | while parsing this struct
| unclosed delimiter
|
help: `}` may belong here
|
LL | fn p() { match s { v, E} { [) {) }
| +
help: try naming a field
|
LL | fn p() { match s { v, E: E { [) {) }
| ++

error: struct literals are not allowed here
--> $DIR/issue-62973.rs:6:16
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/parser/removed-syntax-with-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ fn main() {

let a = S { foo: (), bar: () };
let b = S { foo: (), with a };
//~^ ERROR expected one of `,` or `}`, found `a`
//~^ ERROR expected one of `,`, `:`, or `}`, found `a`
//~| ERROR missing field `bar` in initializer of `S`
}
4 changes: 2 additions & 2 deletions src/test/ui/parser/removed-syntax-with-2.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: expected one of `,` or `}`, found `a`
error: expected one of `,`, `:`, or `}`, found `a`
--> $DIR/removed-syntax-with-2.rs:8:31
|
LL | let b = S { foo: (), with a };
| - ^ expected one of `,` or `}`
| - ^ expected one of `,`, `:`, or `}`
| |
| while parsing this struct

Expand Down