Skip to content

Assign an AttrStyle to all HIR attributes#142759

Closed
dtolnay wants to merge 2 commits intorust-lang:masterfrom
dtolnay:hirattrstyle
Closed

Assign an AttrStyle to all HIR attributes#142759
dtolnay wants to merge 2 commits intorust-lang:masterfrom
dtolnay:hirattrstyle

Conversation

@dtolnay
Copy link
Copy Markdown
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

Loading
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