Don’t report missing fields in struct exprs with syntax errors.#153227
Don’t report missing fields in struct exprs with syntax errors.#153227kpreid wants to merge 2 commits intorust-lang:mainfrom
Conversation
This prevents spurious errors when a field is intended to be present
but a preceding syntax error caused it not to be parsed. For example,
StructName { foo: 1 bar: 2 }
will not successfully parse a field `bar`, and we will report the syntax
error but not the missing field.
|
Some changes occurred in need_type_info.rs cc @lcnr Some changes occurred in src/tools/rustfmt cc @rust-lang/rustfmt Some changes occurred in src/tools/clippy cc @rust-lang/clippy |
|
r? @JohnTitor rustbot has assigned @JohnTitor. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
The only changes to rustfmt and clippy are necessary added @rustbot label -T-clippy -T-rustfmt |
|
Thanks, these types of fixes are exactly what I was talking about! |
|
Size increase of not uncommonly used AST & HIR nodes. @bors try @rust-timer queue |
|
Awaiting bors try build completion. @rustbot label: +S-waiting-on-perf |
|
⌛ Trying commit c6432b7 with merge 4e1e63f… To cancel the try build, run the command Workflow: https://github.com/rust-lang/rust/actions/runs/22529605508 |
Don’t report missing fields in struct exprs with syntax errors.
There was a problem hiding this comment.
Alternatively to all of this we could 'just' make the parser recovery smarter and try to recover $path { $ident: $expr $ident: $expr } (and similar) as $path { $ident: $expr, $ident: $expr } instead of $path { $ident: /*error*/ } which is what we currently seem to do.
@Noratrieb told me that “it is a bug if this recovery causes follow-up errors that would not be there if the user fixed the first error.” So, here’s a contribution to hide a follow-up error that annoyed me recently.
Specifically, if the user writes a struct literal with a syntax error, such as
the compiler will no longer report that the field
baris missing in addition to the syntax error.This is my first time attempting any change to the parser or AST; please let me know if there is a better way to do what I’ve done here. The part I’m least happy with is the blast radius of adding another field to
hir::ExprKind::Struct, but this seems to be in line with the style of the rest of the code. (If this were my own code, I would consider changinghir::ExprKind::Structto a nested struct, the same way it is inast::ExprKind.)