Skip to content

Recover inner attributes in algebraic data types - #162018

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
JonathanBrouwer:attr_empty_struct
Nov 2, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
JonathanBrouwer:attr_empty_struct

Conversation

@JonathanBrouwer

Copy link
Copy Markdown
Member

Previously, if you'd put an inner attribute in an empty ADT, such as a struct:

struct Test1 {
    #![inline]
}

You'd get the following errors

error: an inner attribute is not permitted in this context
 --> src/lib.rs:5:5
  |
5 |     #![cfg(false)]
  |     ^^^^^^^^^^^^^^
  |
  = note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
  = note: outer attributes, like `#[test]`, annotate the item following them

error: expected identifier, found `}`
 --> src/lib.rs:6:1
  |
4 | struct Test3 {
  |        ----- while parsing this struct
5 |     #![cfg(false)]
6 | }
  | ^ expected identifier

error: could not compile `rust-test` (lib) due to 2 previous errors

The second error is spurious. It was caused by us only recovering inner attributes just before parsing a field, meaning if we see an inner attribute and recover we expect a field next. This PR fixes that by recovering inner attributes just after the curly brace as well.

@rustbot

rustbot commented Aug 30, 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 Aug 30, 2026
@rustbot

rustbot commented Aug 30, 2026

Copy link
Copy Markdown
Collaborator

r? @wesleywiser

rustbot has assigned @wesleywiser.
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 19 candidates

@rust-bors

This comment has been minimized.

@rustbot

This comment has been minimized.

@JonathanBrouwer

Copy link
Copy Markdown
Member Author

@rustbot reroll

@JonathanBrouwer

Copy link
Copy Markdown
Member Author

@rustbot reroll

@JonathanBrouwer

Copy link
Copy Markdown
Member Author

@rustbot reroll

@rustbot rustbot assigned adwinwhite and unassigned JohnTitor Oct 28, 2026

@fmease fmease Oct 29, 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.

In the case of enums you're recovering from inner attributes before each variant and in the case of "records" you're recovering from inner attributes once before all fields. I would pick the same approach for both.

View changes since the review

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'm now checking before every variant and every field, and expanded the test to show this

Comment thread compiler/rustc_parse/src/parser/item.rs Outdated
}

fn parse_enum_variant(&mut self, span: Span) -> PResult<'a, Option<Variant>> {
if self.recover_inner_attributes()? && self.check(exp![CloseBrace]) {

@fmease fmease Oct 29, 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.

Checking for CloseBrace here in parse_enum_variant is a bit "assumptious" abstraction breaking since strictly speaking } it doesn't belong to syntax fragment / production rule EnumVariant. Ideally that check would happen outside of this function. Of course, sometimes blurring is unavoidable.

View changes since the review

@JonathanBrouwer JonathanBrouwer Oct 30, 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.

I lifted the recover_inner_attributes call out of the method to fix this

@fmease fmease assigned fmease and unassigned adwinwhite Oct 29, 2026
@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 29, 2026
@JonathanBrouwer

Copy link
Copy Markdown
Member Author

@rustbot ready

@rustbot rustbot 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 Oct 30, 2026
@rustbot

rustbot commented Oct 30, 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.

@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.

Thanks!

@bors rollup
@bors r+ squash

View changes since this review

@rust-bors

This comment has been minimized.

* Add regression test
* Recover inner attributes in algebraic data types
* Try to recover after every field and variant
@rust-bors

rust-bors Bot commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

🔨 3 commits were squashed into 5dd864e.

@rust-bors
rust-bors Bot force-pushed the attr_empty_struct branch from 95c251f to 5dd864e Compare November 1, 2026 21:47
@rust-bors

rust-bors Bot commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 5dd864e has been tentatively approved by fmease

It will be put into the queue for this repository once PR CI succeeds.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 1, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Nov 2, 2026
… r=fmease

Recover inner attributes in algebraic data types

Previously, if you'd put an inner attribute in an empty ADT, such as a struct:
```rust
struct Test1 {
    #![inline]
}
```
You'd get the following errors
```
error: an inner attribute is not permitted in this context
 --> src/lib.rs:5:5
  |
5 |     #![cfg(false)]
  |     ^^^^^^^^^^^^^^
  |
  = note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
  = note: outer attributes, like `#[test]`, annotate the item following them

error: expected identifier, found `}`
 --> src/lib.rs:6:1
  |
4 | struct Test3 {
  |        ----- while parsing this struct
5 |     #![cfg(false)]
6 | }
  | ^ expected identifier

error: could not compile `rust-test` (lib) due to 2 previous errors
```

The second error is spurious. It was caused by us only recovering inner attributes just before parsing a field, meaning if we see an inner attribute and recover we expect a field next. This PR fixes that by recovering inner attributes just after the curly brace as well.
rust-bors Bot pushed a commit that referenced this pull request Nov 2, 2026
Rollup of 9 pull requests

Successful merges:

 - #162018 (Recover inner attributes in algebraic data types)
 - #162819 (Make the suggestion message of `unused_must_use` scarier.)
 - #163053 (Use verbose suggestion for type literal with wrong syntax)
 - #163245 (Fix multiple pattern comma suggestions in diagnostics)
 - #163369 (Don't lint unused_qualifications when the unqualified path is ambiguous)
 - #165282 (Improve span for unused_mut lint)
 - #165285 (Forward `AbiAlign` `Debug` impl to `Align`)
 - #165294 (unstable.rs: add missing `]` in description of `cfg_ub_checks`)
 - #165310 (Clarify AGENTS.md around pushing to a fork)
@rust-bors
rust-bors Bot merged commit 27a11b4 into rust-lang:main Nov 2, 2026
14 checks passed
rust-bors Bot pushed a commit that referenced this pull request Nov 2, 2026
Rollup merge of #162018 - JonathanBrouwer:attr_empty_struct, r=fmease

Recover inner attributes in algebraic data types

Previously, if you'd put an inner attribute in an empty ADT, such as a struct:
```rust
struct Test1 {
    #![inline]
}
```
You'd get the following errors
```
error: an inner attribute is not permitted in this context
 --> src/lib.rs:5:5
  |
5 |     #![cfg(false)]
  |     ^^^^^^^^^^^^^^
  |
  = note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
  = note: outer attributes, like `#[test]`, annotate the item following them

error: expected identifier, found `}`
 --> src/lib.rs:6:1
  |
4 | struct Test3 {
  |        ----- while parsing this struct
5 |     #![cfg(false)]
6 | }
  | ^ expected identifier

error: could not compile `rust-test` (lib) due to 2 previous errors
```

The second error is spurious. It was caused by us only recovering inner attributes just before parsing a field, meaning if we see an inner attribute and recover we expect a field next. This PR fixes that by recovering inner attributes just after the curly brace as well.
@rustbot rustbot added this to the 1.101.0 milestone Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

7 participants