Skip to content

All HIR attributes are outer #142776

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
29 changes: 22 additions & 7 deletions compiler/rustc_ast/src/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,24 @@ impl AttributeExt for Attribute {
}
}

fn style(&self) -> AttrStyle {
self.style
fn doc_resolution_scope(&self) -> Option<AttrStyle> {
match &self.kind {
AttrKind::DocComment(..) => Some(self.style),
AttrKind::Normal(normal)
if normal.item.path == sym::doc && normal.item.value_str().is_some() =>
{
Some(self.style)
}
_ => None,
}
}
}

impl Attribute {
pub fn style(&self) -> AttrStyle {
self.style
}

pub fn may_have_doc_links(&self) -> bool {
self.doc_str().is_some_and(|s| comments::may_have_doc_links(s.as_str()))
}
Expand Down Expand Up @@ -806,7 +818,14 @@ pub trait AttributeExt: Debug {
/// * `#[doc(...)]` returns `None`.
fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)>;

fn style(&self) -> AttrStyle;
/// Returns outer or inner if this is a doc attribute or a sugared doc
/// comment, otherwise None.
///
/// This is used in the case of doc comments on modules, to decide whether
/// to resolve intra-doc links against the symbols in scope within the
/// commented module (for inner doc) vs within its parent module (for outer
/// doc).
fn doc_resolution_scope(&self) -> Option<AttrStyle>;
}

// FIXME(fn_delegation): use function delegation instead of manually forwarding
Expand Down Expand Up @@ -881,8 +900,4 @@ impl Attribute {
pub fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)> {
AttributeExt::doc_str_and_comment_kind(self)
}

pub fn style(&self) -> AttrStyle {
AttributeExt::style(self)
}
}
31 changes: 10 additions & 21 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1345,12 +1345,13 @@ impl AttributeExt for Attribute {
}
}

#[inline]
fn style(&self) -> AttrStyle {
match &self {
Attribute::Unparsed(u) => u.style,
Attribute::Parsed(AttributeKind::DocComment { style, .. }) => *style,
_ => panic!(),
fn doc_resolution_scope(&self) -> Option<AttrStyle> {
match self {
Attribute::Parsed(AttributeKind::DocComment { style, .. }) => Some(*style),
Attribute::Unparsed(attr) if self.has_name(sym::doc) && self.value_str().is_some() => {
Some(attr.style)
}
_ => None,
}
}
}
Expand Down Expand Up @@ -1441,11 +1442,6 @@ impl Attribute {
pub fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)> {
AttributeExt::doc_str_and_comment_kind(self)
}

#[inline]
pub fn style(&self) -> AttrStyle {
AttributeExt::style(self)
}
}

/// Attributes owned by a HIR owner.
Expand Down Expand Up @@ -2285,16 +2281,9 @@ pub struct Expr<'hir> {
}

impl Expr<'_> {
pub fn precedence(
&self,
for_each_attr: &dyn Fn(HirId, &mut dyn FnMut(&Attribute)),
) -> ExprPrecedence {
pub fn precedence(&self, has_attr: &dyn Fn(HirId) -> bool) -> ExprPrecedence {
let prefix_attrs_precedence = || -> ExprPrecedence {
let mut has_outer_attr = false;
for_each_attr(self.hir_id, &mut |attr: &Attribute| {
has_outer_attr |= matches!(attr.style(), AttrStyle::Outer)
});
if has_outer_attr { ExprPrecedence::Prefix } else { ExprPrecedence::Unambiguous }
if has_attr(self.hir_id) { ExprPrecedence::Prefix } else { ExprPrecedence::Unambiguous }
};

match &self.kind {
Expand Down Expand Up @@ -2350,7 +2339,7 @@ impl Expr<'_> {
| ExprKind::Use(..)
| ExprKind::Err(_) => prefix_attrs_precedence(),

ExprKind::DropTemps(expr, ..) => expr.precedence(for_each_attr),
ExprKind::DropTemps(expr, ..) => expr.precedence(has_attr),
}
}

Expand Down
94 changes: 40 additions & 54 deletions compiler/rustc_hir_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::vec;

use rustc_abi::ExternAbi;
use rustc_ast::util::parser::{self, ExprPrecedence, Fixity};
use rustc_ast::{AttrStyle, DUMMY_NODE_ID, DelimArgs};
use rustc_ast::{DUMMY_NODE_ID, DelimArgs};
use rustc_ast_pretty::pp::Breaks::{Consistent, Inconsistent};
use rustc_ast_pretty::pp::{self, BoxMarker, Breaks};
use rustc_ast_pretty::pprust::state::MacHeader;
Expand Down Expand Up @@ -81,32 +81,24 @@ impl<'a> State<'a> {
}

fn precedence(&self, expr: &hir::Expr<'_>) -> ExprPrecedence {
let for_each_attr = |id: HirId, callback: &mut dyn FnMut(&hir::Attribute)| {
self.attrs(id).iter().for_each(callback);
};
expr.precedence(&for_each_attr)
}

fn print_attrs_as_inner(&mut self, attrs: &[hir::Attribute]) {
self.print_either_attributes(attrs, ast::AttrStyle::Inner)
}

fn print_attrs_as_outer(&mut self, attrs: &[hir::Attribute]) {
self.print_either_attributes(attrs, ast::AttrStyle::Outer)
let has_attr = |id: HirId| !self.attrs(id).is_empty();
expr.precedence(&has_attr)
}

fn print_either_attributes(&mut self, attrs: &[hir::Attribute], style: ast::AttrStyle) {
fn print_attrs(&mut self, attrs: &[hir::Attribute]) {
if attrs.is_empty() {
return;
}

for attr in attrs {
self.print_attribute_inline(attr, style);
self.print_attribute_as_style(attr, ast::AttrStyle::Outer);
}
self.hardbreak_if_not_bol();
}

fn print_attribute_inline(&mut self, attr: &hir::Attribute, style: AttrStyle) {
/// Print a single attribute as if it has style `style`, disregarding the
/// actual style of the attribute.
fn print_attribute_as_style(&mut self, attr: &hir::Attribute, style: ast::AttrStyle) {
match &attr {
hir::Attribute::Unparsed(unparsed) => {
self.maybe_print_comment(unparsed.span.lo());
Expand All @@ -118,14 +110,17 @@ impl<'a> State<'a> {
self.word("]");
self.hardbreak()
}
hir::Attribute::Parsed(AttributeKind::DocComment { style, kind, comment, .. }) => {
hir::Attribute::Parsed(AttributeKind::DocComment { kind, comment, .. }) => {
self.word(rustc_ast_pretty::pprust::state::doc_comment_to_string(
*kind, *style, *comment,
*kind, style, *comment,
));
self.hardbreak()
}
hir::Attribute::Parsed(pa) => {
self.word("#[attr = ");
match style {
ast::AttrStyle::Inner => self.word("#![attr = "),
ast::AttrStyle::Outer => self.word("#[attr = "),
}
pa.print_attribute(self);
self.word("]");
self.hardbreak()
Expand Down Expand Up @@ -281,10 +276,17 @@ pub fn print_crate<'a>(
ann,
};

// Print all attributes, regardless of actual style, as inner attributes
// since this is the crate root with nothing above it to print outer
// attributes.
for attr in s.attrs(hir::CRATE_HIR_ID) {
s.print_attribute_as_style(attr, ast::AttrStyle::Inner);
}

// When printing the AST, we sometimes need to inject `#[no_std]` here.
// Since you can't compile the HIR, it's not necessary.

s.print_mod(krate, (*attrs)(hir::CRATE_HIR_ID));
s.print_mod(krate);
s.print_remaining_comments();
s.s.eof()
}
Expand All @@ -299,7 +301,7 @@ where
}

pub fn attribute_to_string(ann: &dyn PpAnn, attr: &hir::Attribute) -> String {
to_string(ann, |s| s.print_attribute_inline(attr, AttrStyle::Outer))
to_string(ann, |s| s.print_attribute_as_style(attr, ast::AttrStyle::Outer))
}

pub fn ty_to_string(ann: &dyn PpAnn, ty: &hir::Ty<'_>) -> String {
Expand Down Expand Up @@ -361,8 +363,7 @@ impl<'a> State<'a> {
self.commasep_cmnt(b, exprs, |s, e| s.print_expr(e), |e| e.span);
}

fn print_mod(&mut self, _mod: &hir::Mod<'_>, attrs: &[hir::Attribute]) {
self.print_attrs_as_inner(attrs);
fn print_mod(&mut self, _mod: &hir::Mod<'_>) {
for &item_id in _mod.item_ids {
self.ann.nested(self, Nested::Item(item_id));
}
Expand Down Expand Up @@ -479,7 +480,7 @@ impl<'a> State<'a> {
fn print_foreign_item(&mut self, item: &hir::ForeignItem<'_>) {
self.hardbreak_if_not_bol();
self.maybe_print_comment(item.span.lo());
self.print_attrs_as_outer(self.attrs(item.hir_id()));
self.print_attrs(self.attrs(item.hir_id()));
match item.kind {
hir::ForeignItemKind::Fn(sig, arg_idents, generics) => {
let (cb, ib) = self.head("");
Expand Down Expand Up @@ -565,7 +566,7 @@ impl<'a> State<'a> {
self.hardbreak_if_not_bol();
self.maybe_print_comment(item.span.lo());
let attrs = self.attrs(item.hir_id());
self.print_attrs_as_outer(attrs);
self.print_attrs(attrs);
self.ann.pre(self, AnnNode::Item(item));
match item.kind {
hir::ItemKind::ExternCrate(orig_name, ident) => {
Expand Down Expand Up @@ -647,14 +648,13 @@ impl<'a> State<'a> {
self.print_ident(ident);
self.nbsp();
self.bopen(ib);
self.print_mod(mod_, attrs);
self.print_mod(mod_);
self.bclose(item.span, cb);
}
hir::ItemKind::ForeignMod { abi, items } => {
let (cb, ib) = self.head("extern");
self.word_nbsp(abi.to_string());
self.bopen(ib);
self.print_attrs_as_inner(self.attrs(item.hir_id()));
for item in items {
self.ann.nested(self, Nested::ForeignItem(item.id));
}
Expand Down Expand Up @@ -731,7 +731,6 @@ impl<'a> State<'a> {

self.space();
self.bopen(ib);
self.print_attrs_as_inner(attrs);
for impl_item in items {
self.ann.nested(self, Nested::ImplItem(impl_item.id));
}
Expand Down Expand Up @@ -822,7 +821,7 @@ impl<'a> State<'a> {
for v in variants {
self.space_if_not_bol();
self.maybe_print_comment(v.span.lo());
self.print_attrs_as_outer(self.attrs(v.hir_id));
self.print_attrs(self.attrs(v.hir_id));
let ib = self.ibox(INDENT_UNIT);
self.print_variant(v);
self.word(",");
Expand Down Expand Up @@ -857,7 +856,7 @@ impl<'a> State<'a> {
self.popen();
self.commasep(Inconsistent, struct_def.fields(), |s, field| {
s.maybe_print_comment(field.span.lo());
s.print_attrs_as_outer(s.attrs(field.hir_id));
s.print_attrs(s.attrs(field.hir_id));
s.print_type(field.ty);
});
self.pclose();
Expand All @@ -878,7 +877,7 @@ impl<'a> State<'a> {
for field in struct_def.fields() {
self.hardbreak_if_not_bol();
self.maybe_print_comment(field.span.lo());
self.print_attrs_as_outer(self.attrs(field.hir_id));
self.print_attrs(self.attrs(field.hir_id));
self.print_ident(field.ident);
self.word_nbsp(":");
self.print_type(field.ty);
Expand Down Expand Up @@ -916,7 +915,7 @@ impl<'a> State<'a> {
self.ann.pre(self, AnnNode::SubItem(ti.hir_id()));
self.hardbreak_if_not_bol();
self.maybe_print_comment(ti.span.lo());
self.print_attrs_as_outer(self.attrs(ti.hir_id()));
self.print_attrs(self.attrs(ti.hir_id()));
match ti.kind {
hir::TraitItemKind::Const(ty, default) => {
self.print_associated_const(ti.ident, ti.generics, ty, default);
Expand Down Expand Up @@ -944,7 +943,7 @@ impl<'a> State<'a> {
self.ann.pre(self, AnnNode::SubItem(ii.hir_id()));
self.hardbreak_if_not_bol();
self.maybe_print_comment(ii.span.lo());
self.print_attrs_as_outer(self.attrs(ii.hir_id()));
self.print_attrs(self.attrs(ii.hir_id()));

match ii.kind {
hir::ImplItemKind::Const(ty, expr) => {
Expand Down Expand Up @@ -1028,27 +1027,16 @@ impl<'a> State<'a> {
}

fn print_block(&mut self, blk: &hir::Block<'_>, cb: BoxMarker, ib: BoxMarker) {
self.print_block_with_attrs(blk, &[], cb, ib)
self.print_block_maybe_unclosed(blk, Some(cb), ib)
}

fn print_block_unclosed(&mut self, blk: &hir::Block<'_>, ib: BoxMarker) {
self.print_block_maybe_unclosed(blk, &[], None, ib)
}

fn print_block_with_attrs(
&mut self,
blk: &hir::Block<'_>,
attrs: &[hir::Attribute],
cb: BoxMarker,
ib: BoxMarker,
) {
self.print_block_maybe_unclosed(blk, attrs, Some(cb), ib)
self.print_block_maybe_unclosed(blk, None, ib)
}

fn print_block_maybe_unclosed(
&mut self,
blk: &hir::Block<'_>,
attrs: &[hir::Attribute],
cb: Option<BoxMarker>,
ib: BoxMarker,
) {
Expand All @@ -1060,8 +1048,6 @@ impl<'a> State<'a> {
self.ann.pre(self, AnnNode::Block(blk));
self.bopen(ib);

self.print_attrs_as_inner(attrs);

for st in blk.stmts {
self.print_stmt(st);
}
Expand Down Expand Up @@ -1251,7 +1237,7 @@ impl<'a> State<'a> {

fn print_expr_field(&mut self, field: &hir::ExprField<'_>) {
let cb = self.cbox(INDENT_UNIT);
self.print_attrs_as_outer(self.attrs(field.hir_id));
self.print_attrs(self.attrs(field.hir_id));
if !field.is_shorthand {
self.print_ident(field.ident);
self.word_space(":");
Expand Down Expand Up @@ -1451,7 +1437,7 @@ impl<'a> State<'a> {

fn print_expr(&mut self, expr: &hir::Expr<'_>) {
self.maybe_print_comment(expr.span.lo());
self.print_attrs_as_outer(self.attrs(expr.hir_id));
self.print_attrs(self.attrs(expr.hir_id));
let ib = self.ibox(INDENT_UNIT);
self.ann.pre(self, AnnNode::Expr(expr));
match expr.kind {
Expand Down Expand Up @@ -2076,7 +2062,7 @@ impl<'a> State<'a> {
self.space();
}
let cb = self.cbox(INDENT_UNIT);
self.print_attrs_as_outer(self.attrs(field.hir_id));
self.print_attrs(self.attrs(field.hir_id));
if !field.is_shorthand {
self.print_ident(field.ident);
self.word_nbsp(":");
Expand All @@ -2086,7 +2072,7 @@ impl<'a> State<'a> {
}

fn print_param(&mut self, arg: &hir::Param<'_>) {
self.print_attrs_as_outer(self.attrs(arg.hir_id));
self.print_attrs(self.attrs(arg.hir_id));
self.print_pat(arg.pat);
}

Expand Down Expand Up @@ -2121,7 +2107,7 @@ impl<'a> State<'a> {
let cb = self.cbox(INDENT_UNIT);
self.ann.pre(self, AnnNode::Arm(arm));
let ib = self.ibox(0);
self.print_attrs_as_outer(self.attrs(arm.hir_id));
self.print_attrs(self.attrs(arm.hir_id));
self.print_pat(arm.pat);
self.space();
if let Some(ref g) = arm.guard {
Expand Down Expand Up @@ -2409,7 +2395,7 @@ impl<'a> State<'a> {
}

fn print_where_predicate(&mut self, predicate: &hir::WherePredicate<'_>) {
self.print_attrs_as_outer(self.attrs(predicate.hir_id));
self.print_attrs(self.attrs(predicate.hir_id));
match *predicate.kind {
hir::WherePredicateKind::BoundPredicate(hir::WhereBoundPredicate {
bound_generic_params,
Expand Down
Loading
Loading