Skip to content

Rewrite the #[repr] attribute parser#157125

Open
JonathanBrouwer wants to merge 4 commits into
rust-lang:mainfrom
JonathanBrouwer:repr-target-checking2
Open

Rewrite the #[repr] attribute parser#157125
JonathanBrouwer wants to merge 4 commits into
rust-lang:mainfrom
JonathanBrouwer:repr-target-checking2

Conversation

@JonathanBrouwer
Copy link
Copy Markdown
Contributor

@JonathanBrouwer JonathanBrouwer commented May 29, 2026

The #[repr] attribute parser was one of the first attribute parsers we made, and didn't use a lot of the awesome infra we have nowadays yet, so in preparation for a new approach I'm trying for #156569 I decided to clean it up.

This PR should have no observable effect other than improved diagnostic messages.

r? @mejrs

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 29, 2026

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) 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 May 29, 2026
cx.adcx().expected_specific_argument(
param.span(),
&[
sym::align,
Copy link
Copy Markdown
Contributor Author

@JonathanBrouwer JonathanBrouwer May 29, 2026

Choose a reason for hiding this comment

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

One thing that still annoys me is that in a lot of attribute parsers have these match statements, and then we need to list all branches of the match statement again for the expected_specific_argument error

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd like a solution where these symbols are only listed once.

Maybe make an array of [(Symbol, constructor); _] (or maybe [(Symbol, checker, constructor); _]) and if you don't find the symbol in the array, do something like array.iter().map(|x|x.0).collect() and pass that to expected_specific_argument.

See

const DIAGNOSTIC_ATTRIBUTES: &[(Symbol, Option<Symbol>)] = &[
for inspiration.

Copy link
Copy Markdown
Contributor Author

@JonathanBrouwer JonathanBrouwer May 30, 2026

Choose a reason for hiding this comment

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

(let's leave this for a separate PR tho, since this affects more places that I'd like to fix at the same time)

Copy link
Copy Markdown
Contributor

@mejrs mejrs left a comment

Choose a reason for hiding this comment

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

I'll continue reviewing tomorrow, just some comments so far.

The error code for the cfg_select errors are (and were) unfortunate. I will file an issue for that later.

View changes since this review

cx.adcx().expected_specific_argument(
param.span(),
&[
sym::align,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd like a solution where these symbols are only listed once.

Maybe make an array of [(Symbol, constructor); _] (or maybe [(Symbol, checker, constructor); _]) and if you don't find the symbol in the array, do something like array.iter().map(|x|x.0).collect() and pass that to expected_specific_argument.

See

const DIAGNOSTIC_ATTRIBUTES: &[(Symbol, Option<Symbol>)] = &[
for inspiration.

Comment thread compiler/rustc_attr_parsing/src/attributes/repr.rs Outdated
Comment thread compiler/rustc_attr_parsing/src/attributes/repr.rs Outdated
Comment thread compiler/rustc_error_codes/src/error_codes/E0552.md Outdated
@theemathas
Copy link
Copy Markdown
Contributor

Does this conflict with #157036 ?

@JonathanBrouwer
Copy link
Copy Markdown
Contributor Author

JonathanBrouwer commented May 30, 2026

Does this conflict with #157036 ?

Not really. The code will have merge conflicts, but that shouldn't be too severe. This PR doesn't have any observable changes other than changes in diagnostics, I'm leaving those for #157036

@JonathanBrouwer JonathanBrouwer force-pushed the repr-target-checking2 branch from f4e215b to 7aef596 Compare May 30, 2026 09:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) 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.

4 participants