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

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


use super::prelude::*;
use crate::session_diagnostics::{self, IncorrectReprFormatGenericCause};
use crate::session_diagnostics::{self};
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.

Suggested change
use crate::session_diagnostics::{self};
use crate::session_diagnostics;

/// Parse #[repr(...)] forms.
///
/// Valid repr contents: any of the primitive integral type names (see
/// `int_type_of_word`, below) to specify enum discriminant type; `C`, to use
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.

int_type_of_word is dangling.

@@ -1,8 +1,10 @@
#### Note: this error code is no longer emitted by the compiler
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.

Please mention how this now emits another code. Same for the align error code.

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.

3 participants