Assign an AttrStyle to all HIR attributes #142759
Closed
+195
−90
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #142649.
Prior to #135726, all
hir::Attribute
used to have a "style" (eitherAttrStyle::Outer
orAttrStyle::Inner
, for #[…] and #![…] respectively). After that refactor,hir::Attribute
'sfn 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 eitherrustc_attr_data_structures::AttributeKind
orhir::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
returnAttrStyle::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