Repository navigation
Recover inner attributes in algebraic data types - #162018
Conversation
|
The parser was modified, potentially altering the grammar of (stable) Rust cc @fmease |
|
r? @wesleywiser rustbot has assigned @wesleywiser. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
1dae34b to
6990400
Compare
This comment has been minimized.
This comment has been minimized.
|
@rustbot reroll |
|
@rustbot reroll |
|
@rustbot reroll |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I'm now checking before every variant and every field, and expanded the test to show this
| } | ||
|
|
||
| fn parse_enum_variant(&mut self, span: Span) -> PResult<'a, Option<Variant>> { | ||
| if self.recover_inner_attributes()? && self.check(exp![CloseBrace]) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I lifted the recover_inner_attributes call out of the method to fix this
|
@rustbot ready |
6990400 to
95c251f
Compare
|
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 comment has been minimized.
This comment has been minimized.
* Add regression test * Recover inner attributes in algebraic data types * Try to recover after every field and variant
|
🔨 3 commits were squashed into 5dd864e. |
95c251f to
5dd864e
Compare
… 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.
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)
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.
Previously, if you'd put an inner attribute in an empty ADT, such as a struct:
You'd get the following 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.