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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1350,7 +1350,42 @@ impl AttributeExt for Attribute {
match &self {
Attribute::Unparsed(u) => u.style,
Attribute::Parsed(AttributeKind::DocComment { style, .. }) => *style,
_ => panic!(),
// The following HIR attributes may originally have been parsed from
// an outer or inner AST attribute, or indeed both. For example, the
// two AST attributes on this function:
//
// #[rustc_allow_const_fn_unstable(const_eval_select)]
// const fn f() {
// #![rustc_allow_const_fn_unstable(const_precise_live_drops)]
// ...
// }
//
// will be parsed to a single HIR attribute containing both symbols.
//
// Regardless of where they came from, for the purpose of HIR, we
// consider all of these to be outer attributes and rustc_hir_pretty
// will render them as such.
//
// #[attr = AllowConstFnUnstable(["const_eval_select", "const_precise_live_drops"])]
// const fn f() {
// ...
// }
//
Attribute::Parsed(
AttributeKind::Align { .. }
| AttributeKind::AllowConstFnUnstable { .. }
| AttributeKind::AllowInternalUnstable { .. }
| AttributeKind::AsPtr { .. }
| AttributeKind::BodyStability { .. }
| AttributeKind::Confusables { .. }
| AttributeKind::ConstStability { .. }
| AttributeKind::ConstStabilityIndirect { .. }
| AttributeKind::Deprecation { .. }
| AttributeKind::Inline { .. }
| 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.

}
}
}
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_hir/src/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pub trait HirTyCtxt<'hir> {
/// Retrieves the `Node` corresponding to `id`.
fn hir_node(&self, hir_id: HirId) -> Node<'hir>;
fn hir_body(&self, id: BodyId) -> &'hir Body<'hir>;
fn hir_body_owner(&self, id: BodyId) -> HirId;
fn hir_item(&self, id: ItemId) -> &'hir Item<'hir>;
fn hir_trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir>;
fn hir_impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir>;
Expand All @@ -127,6 +128,9 @@ impl<'hir> HirTyCtxt<'hir> for ! {
fn hir_body(&self, _: BodyId) -> &'hir Body<'hir> {
unreachable!();
}
fn hir_body_owner(&self, _: BodyId) -> HirId {
unreachable!();
}
fn hir_item(&self, _: ItemId) -> &'hir Item<'hir> {
unreachable!();
}
Expand Down
Loading
Loading