Skip to content

Assign an AttrStyle to all HIR attributes #142759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Jun 20, 2025

Fixes #142649.

Prior to #135726, all hir::Attribute used to have a "style" (either AttrStyle::Outer or AttrStyle::Inner, for #[…] and #![…] respectively). After that refactor, hir::Attribute's fn style(&self) -> AttrStyle began to panic if called on certain builtin attributes like #[deprecated].

I looked into changing this method to fn style(&self) -> Option<AttrStyle> where builtin attributes would not have a style. But ultimately the HIR pretty-printer needs to print all these attributes, and needs to print them in either outer or inner position, so at least every attribute included in -Zunpretty=hir definitely needs a style, including builtin attributes.

I looked into storing every attribute's original AttrStyle in either rustc_attr_data_structures::AttributeKind or hir::Attribute. This does not make sense because there is a many-to-one correspondence between AST attributes and HIR attributes. Two different AST attributes with different style can get parsed into the same single HIR attribute. I have added an example of this in a comment.

Since the HIR pretty-printer chooses to print all of these builtin attributes as outer attributes, and we need to be able to know the style of every expression-level attribute in order to correctly handle expression precedence during pretty-printing and diagnostics, in this PR I have made hir::Attribute::style return AttrStyle::Outer instead of panicking for builtin attributes.

This solution fixes several rustc_hir_pretty bugs associated with not being able to know the style of HIR attributes. For example, some attributes used to get duplicated as both outer and inner when printed, as you can see in the tests.

@jdonszelmann @fmease

@rustbot
Copy link
Collaborator

rustbot commented Jun 20, 2025

r? @oli-obk

rustbot has assigned @oli-obk.
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

@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 Jun 20, 2025
| AttributeKind::MacroTransparency { .. }
| AttributeKind::Repr { .. }
| AttributeKind::Stability { .. },
) => AttrStyle::Outer,
Copy link
Contributor

@jdonszelmann jdonszelmann Jun 20, 2025

Choose a reason for hiding this comment

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

I expect to, within a few weeks or so, convert all attributes to be parsed. As such, I'm not sure the changes in this pr are very useful as this function will likely be deleted soon since it would effectively always return Outer. Only tool attributes might retain the ability to have an AttrStyle, though I think it'd be acceptable to always print those as outer too.

@jdonszelmann jdonszelmann self-assigned this Jun 20, 2025
@dtolnay dtolnay 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 Jun 20, 2025
@dtolnay
Copy link
Member Author

dtolnay commented Jun 20, 2025

Closing in favor of #142776.

@dtolnay dtolnay closed this Jun 20, 2025
@dtolnay dtolnay deleted the hirattrstyle branch June 20, 2025 22:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

ICE: hir: explicit panic
4 participants