Skip to content

Recover shorthand struct literals in if conditions - #162295

Open
chenyukang wants to merge 3 commits into
rust-lang:mainfrom
chenyukang:yukang-fix-147877-struct-literal-condition-diagnostic
Open

chenyukang wants to merge 3 commits into
rust-lang:mainfrom
chenyukang:yukang-fix-147877-struct-literal-condition-diagnostic

Conversation

@chenyukang

@chenyukang chenyukang commented Sep 4, 2026 •

Copy link
Copy Markdown
Member

Fixes #147877

@rustbot

rustbot commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

The parser was modified, potentially altering the grammar of (stable) Rust
which would be a breaking change.

cc @fmease

@rustbot rustbot added 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. labels Sep 4, 2026
@rustbot

rustbot commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

r? @khyperia

rustbot has assigned @khyperia.
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 75 candidates
  • Random selection from 20 candidates

Comment thread compiler/rustc_parse/src/parser/expr.rs Outdated
Comment on lines +3754 to +3762
(self.look_ahead(1, |t| t.is_ident())
&& self.look_ahead(2, |t| t == &token::Comma || t == &token::Colon))
// When struct literals are prohibited, `{ ident }` is normally parsed as a block. A
// second opening brace strongly suggests that the first pair belongs to a struct
// literal and the second starts the surrounding body.
|| (self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL)
&& self.look_ahead(1, Token::is_non_reserved_ident)
&& self.look_ahead(2, |token| *token == token::CloseBrace)
&& self.look_ahead(3, |token| *token == token::OpenBrace))

@fmease fmease Sep 4, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This modifies the stable grammar of Rust and is thus not acceptable.

It regresses stable code like the following:

fn scope(cond: bool) {
    let u = ();

    if cond {
        u
    }

    {
        println!();
    }
}

or smaller:

#[cfg(false)] fn f() { if x { x } {} }

View changes since the review

@chenyukang chenyukang Sep 4, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

aha, yes! I didn't notice is_likely_struct_lit is on happy path, let me think about it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I updated the code.

This fix adds a lightweight extra { check after parsing each if or while body in error_on_ambiguous_struct_literal. If another block follows, it inspects the parsed AST to detect whether a shorthand struct literal was misinterpreted as the body.

I am not entirely satisfied with adding work to the normal parsing path, but I have not found a cleaner recovery point.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 4, 2026
@chenyukang
chenyukang force-pushed the yukang-fix-147877-struct-literal-condition-diagnostic branch 2 times, most recently from e8c0c3c to ac7bb57 Compare September 4, 2026 23:31
@fmease fmease assigned fmease and unassigned khyperia Sep 7, 2026
@fmease fmease added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 7, 2026

@fmease fmease left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looking good! Some comments but we're almost good to go!

View changes since this review

Comment thread compiler/rustc_parse/src/parser/expr.rs Resolved
Comment thread compiler/rustc_parse/src/parser/expr.rs Resolved
Comment thread compiler/rustc_parse/src/parser/expr.rs Resolved
@fmease fmease added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 23, 2026
@chenyukang
chenyukang force-pushed the yukang-fix-147877-struct-literal-condition-diagnostic branch from ac7bb57 to e470f2d Compare October 25, 2026 04:18
@rustbot

This comment has been minimized.

@chenyukang
chenyukang force-pushed the yukang-fix-147877-struct-literal-condition-diagnostic branch from e470f2d to 6612f74 Compare October 25, 2026 04:29
@fmease fmease added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 1, 2026
@rust-bors

This comment has been minimized.

@fmease

fmease commented Nov 9, 2026

Copy link
Copy Markdown
Member

Tmr I'm gonna rebase for you & re-review the changes. I'm so sorry for the long wait times!

@chenyukang
chenyukang force-pushed the yukang-fix-147877-struct-literal-condition-diagnostic branch from cca7701 to 1c286c2 Compare November 10, 2026 02:39
@rustbot

This comment has been minimized.

@chenyukang

Copy link
Copy Markdown
Member Author

@fmease rebased, no worry and take your time, i'm also busy for other things these days.

@rust-bors

This comment has been minimized.

@chenyukang
chenyukang force-pushed the yukang-fix-147877-struct-literal-condition-diagnostic branch from 1c286c2 to 6148cde Compare November 29, 2026 04:32
@rustbot

rustbot commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

This branch has not been deployed

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

Labels

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.

Weird error message when a struct literal is used in a condition of an if statement

4 participants