Skip to content

Don’t report missing fields in struct exprs with syntax errors.#153227

Open
kpreid wants to merge 2 commits intorust-lang:mainfrom
kpreid:struct-missing-field
Open

Don’t report missing fields in struct exprs with syntax errors.#153227
kpreid wants to merge 2 commits intorust-lang:mainfrom
kpreid:struct-missing-field

Conversation

@kpreid
Copy link
Contributor

@kpreid kpreid commented Feb 28, 2026

@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

StructName { foo: 1 bar: 2 }

the compiler will no longer report that the field bar is 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 changing hir::ExprKind::Struct to a nested struct, the same way it is in ast::ExprKind.)

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.
@rustbot
Copy link
Collaborator

rustbot commented Feb 28, 2026

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

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustfmt Relevant to the rustfmt team, which will review and decide on the PR/issue. labels Feb 28, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 28, 2026

r? @JohnTitor

rustbot has assigned @JohnTitor.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 69 candidates
  • Random selection from 15 candidates

@kpreid
Copy link
Contributor Author

kpreid commented Feb 28, 2026

The only changes to rustfmt and clippy are necessary added _ patterns.

@rustbot label -T-clippy -T-rustfmt

@rustbot rustbot removed T-clippy Relevant to the Clippy team. T-rustfmt Relevant to the rustfmt team, which will review and decide on the PR/issue. labels Feb 28, 2026
@Noratrieb
Copy link
Member

Noratrieb commented Feb 28, 2026

Thanks, these types of fixes are exactly what I was talking about!
An alternative that's less "infectious" would be to recover the invalid struct expr into an error expr, but that'd lose out on the correct fields, so I think this approach is good too.
I won't have time to review it in detail though, so I'll leave that to others.

@fmease
Copy link
Member

fmease commented Feb 28, 2026

Size increase of not uncommonly used AST & HIR nodes. @bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 28, 2026

⌛ Trying commit c6432b7 with merge 4e1e63f

To cancel the try build, run the command @bors try cancel.

Workflow: https://github.com/rust-lang/rust/actions/runs/22529605508

rust-bors bot pushed a commit that referenced this pull request Feb 28, 2026
Don’t report missing fields in struct exprs with syntax errors.
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 28, 2026
Copy link
Member

@fmease fmease Feb 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-perf Status: Waiting on a perf run to be completed. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants