Skip to content

Commit 6d37638

Browse files
authored
Unrolled build for #142776
Rollup merge of #142776 - dtolnay:hirattrstyle2, r=jdonszelmann All HIR attributes are outer Fixes #142649. Closes #142759. All HIR attributes, including parsed and not yet parsed, will now be rendered as outer attributes by `rustc_hir_pretty`. The original style of the corresponding AST attribute(s) is not relevant for pretty printing, only for diagnostics. r? ````@jdonszelmann````
2 parents 9972ebf + 6729b66 commit 6d37638

File tree

15 files changed

+197
-167
lines changed

15 files changed

+197
-167
lines changed

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,24 @@ impl AttributeExt for Attribute {
206206
}
207207
}
208208

209-
fn style(&self) -> AttrStyle {
210-
self.style
209+
fn doc_resolution_scope(&self) -> Option<AttrStyle> {
210+
match &self.kind {
211+
AttrKind::DocComment(..) => Some(self.style),
212+
AttrKind::Normal(normal)
213+
if normal.item.path == sym::doc && normal.item.value_str().is_some() =>
214+
{
215+
Some(self.style)
216+
}
217+
_ => None,
218+
}
211219
}
212220
}
213221

214222
impl Attribute {
223+
pub fn style(&self) -> AttrStyle {
224+
self.style
225+
}
226+
215227
pub fn may_have_doc_links(&self) -> bool {
216228
self.doc_str().is_some_and(|s| comments::may_have_doc_links(s.as_str()))
217229
}
@@ -806,7 +818,14 @@ pub trait AttributeExt: Debug {
806818
/// * `#[doc(...)]` returns `None`.
807819
fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)>;
808820

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

812831
// FIXME(fn_delegation): use function delegation instead of manually forwarding
@@ -881,8 +900,4 @@ impl Attribute {
881900
pub fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)> {
882901
AttributeExt::doc_str_and_comment_kind(self)
883902
}
884-
885-
pub fn style(&self) -> AttrStyle {
886-
AttributeExt::style(self)
887-
}
888903
}

compiler/rustc_hir/src/hir.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,12 +1346,13 @@ impl AttributeExt for Attribute {
13461346
}
13471347
}
13481348

1349-
#[inline]
1350-
fn style(&self) -> AttrStyle {
1351-
match &self {
1352-
Attribute::Unparsed(u) => u.style,
1353-
Attribute::Parsed(AttributeKind::DocComment { style, .. }) => *style,
1354-
_ => panic!(),
1349+
fn doc_resolution_scope(&self) -> Option<AttrStyle> {
1350+
match self {
1351+
Attribute::Parsed(AttributeKind::DocComment { style, .. }) => Some(*style),
1352+
Attribute::Unparsed(attr) if self.has_name(sym::doc) && self.value_str().is_some() => {
1353+
Some(attr.style)
1354+
}
1355+
_ => None,
13551356
}
13561357
}
13571358
}
@@ -1442,11 +1443,6 @@ impl Attribute {
14421443
pub fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)> {
14431444
AttributeExt::doc_str_and_comment_kind(self)
14441445
}
1445-
1446-
#[inline]
1447-
pub fn style(&self) -> AttrStyle {
1448-
AttributeExt::style(self)
1449-
}
14501446
}
14511447

14521448
/// Attributes owned by a HIR owner.
@@ -2286,16 +2282,9 @@ pub struct Expr<'hir> {
22862282
}
22872283

22882284
impl Expr<'_> {
2289-
pub fn precedence(
2290-
&self,
2291-
for_each_attr: &dyn Fn(HirId, &mut dyn FnMut(&Attribute)),
2292-
) -> ExprPrecedence {
2285+
pub fn precedence(&self, has_attr: &dyn Fn(HirId) -> bool) -> ExprPrecedence {
22932286
let prefix_attrs_precedence = || -> ExprPrecedence {
2294-
let mut has_outer_attr = false;
2295-
for_each_attr(self.hir_id, &mut |attr: &Attribute| {
2296-
has_outer_attr |= matches!(attr.style(), AttrStyle::Outer)
2297-
});
2298-
if has_outer_attr { ExprPrecedence::Prefix } else { ExprPrecedence::Unambiguous }
2287+
if has_attr(self.hir_id) { ExprPrecedence::Prefix } else { ExprPrecedence::Unambiguous }
22992288
};
23002289

23012290
match &self.kind {
@@ -2351,7 +2340,7 @@ impl Expr<'_> {
23512340
| ExprKind::Use(..)
23522341
| ExprKind::Err(_) => prefix_attrs_precedence(),
23532342

2354-
ExprKind::DropTemps(expr, ..) => expr.precedence(for_each_attr),
2343+
ExprKind::DropTemps(expr, ..) => expr.precedence(has_attr),
23552344
}
23562345
}
23572346

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 40 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::vec;
1010

1111
use rustc_abi::ExternAbi;
1212
use rustc_ast::util::parser::{self, ExprPrecedence, Fixity};
13-
use rustc_ast::{AttrStyle, DUMMY_NODE_ID, DelimArgs};
13+
use rustc_ast::{DUMMY_NODE_ID, DelimArgs};
1414
use rustc_ast_pretty::pp::Breaks::{Consistent, Inconsistent};
1515
use rustc_ast_pretty::pp::{self, BoxMarker, Breaks};
1616
use rustc_ast_pretty::pprust::state::MacHeader;
@@ -81,32 +81,24 @@ impl<'a> State<'a> {
8181
}
8282

8383
fn precedence(&self, expr: &hir::Expr<'_>) -> ExprPrecedence {
84-
let for_each_attr = |id: HirId, callback: &mut dyn FnMut(&hir::Attribute)| {
85-
self.attrs(id).iter().for_each(callback);
86-
};
87-
expr.precedence(&for_each_attr)
88-
}
89-
90-
fn print_attrs_as_inner(&mut self, attrs: &[hir::Attribute]) {
91-
self.print_either_attributes(attrs, ast::AttrStyle::Inner)
92-
}
93-
94-
fn print_attrs_as_outer(&mut self, attrs: &[hir::Attribute]) {
95-
self.print_either_attributes(attrs, ast::AttrStyle::Outer)
84+
let has_attr = |id: HirId| !self.attrs(id).is_empty();
85+
expr.precedence(&has_attr)
9686
}
9787

98-
fn print_either_attributes(&mut self, attrs: &[hir::Attribute], style: ast::AttrStyle) {
88+
fn print_attrs(&mut self, attrs: &[hir::Attribute]) {
9989
if attrs.is_empty() {
10090
return;
10191
}
10292

10393
for attr in attrs {
104-
self.print_attribute_inline(attr, style);
94+
self.print_attribute_as_style(attr, ast::AttrStyle::Outer);
10595
}
10696
self.hardbreak_if_not_bol();
10797
}
10898

109-
fn print_attribute_inline(&mut self, attr: &hir::Attribute, style: AttrStyle) {
99+
/// Print a single attribute as if it has style `style`, disregarding the
100+
/// actual style of the attribute.
101+
fn print_attribute_as_style(&mut self, attr: &hir::Attribute, style: ast::AttrStyle) {
110102
match &attr {
111103
hir::Attribute::Unparsed(unparsed) => {
112104
self.maybe_print_comment(unparsed.span.lo());
@@ -118,14 +110,17 @@ impl<'a> State<'a> {
118110
self.word("]");
119111
self.hardbreak()
120112
}
121-
hir::Attribute::Parsed(AttributeKind::DocComment { style, kind, comment, .. }) => {
113+
hir::Attribute::Parsed(AttributeKind::DocComment { kind, comment, .. }) => {
122114
self.word(rustc_ast_pretty::pprust::state::doc_comment_to_string(
123-
*kind, *style, *comment,
115+
*kind, style, *comment,
124116
));
125117
self.hardbreak()
126118
}
127119
hir::Attribute::Parsed(pa) => {
128-
self.word("#[attr = ");
120+
match style {
121+
ast::AttrStyle::Inner => self.word("#![attr = "),
122+
ast::AttrStyle::Outer => self.word("#[attr = "),
123+
}
129124
pa.print_attribute(self);
130125
self.word("]");
131126
self.hardbreak()
@@ -281,10 +276,17 @@ pub fn print_crate<'a>(
281276
ann,
282277
};
283278

279+
// Print all attributes, regardless of actual style, as inner attributes
280+
// since this is the crate root with nothing above it to print outer
281+
// attributes.
282+
for attr in s.attrs(hir::CRATE_HIR_ID) {
283+
s.print_attribute_as_style(attr, ast::AttrStyle::Inner);
284+
}
285+
284286
// When printing the AST, we sometimes need to inject `#[no_std]` here.
285287
// Since you can't compile the HIR, it's not necessary.
286288

287-
s.print_mod(krate, (*attrs)(hir::CRATE_HIR_ID));
289+
s.print_mod(krate);
288290
s.print_remaining_comments();
289291
s.s.eof()
290292
}
@@ -299,7 +301,7 @@ where
299301
}
300302

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

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

364-
fn print_mod(&mut self, _mod: &hir::Mod<'_>, attrs: &[hir::Attribute]) {
365-
self.print_attrs_as_inner(attrs);
366+
fn print_mod(&mut self, _mod: &hir::Mod<'_>) {
366367
for &item_id in _mod.item_ids {
367368
self.ann.nested(self, Nested::Item(item_id));
368369
}
@@ -479,7 +480,7 @@ impl<'a> State<'a> {
479480
fn print_foreign_item(&mut self, item: &hir::ForeignItem<'_>) {
480481
self.hardbreak_if_not_bol();
481482
self.maybe_print_comment(item.span.lo());
482-
self.print_attrs_as_outer(self.attrs(item.hir_id()));
483+
self.print_attrs(self.attrs(item.hir_id()));
483484
match item.kind {
484485
hir::ForeignItemKind::Fn(sig, arg_idents, generics) => {
485486
let (cb, ib) = self.head("");
@@ -565,7 +566,7 @@ impl<'a> State<'a> {
565566
self.hardbreak_if_not_bol();
566567
self.maybe_print_comment(item.span.lo());
567568
let attrs = self.attrs(item.hir_id());
568-
self.print_attrs_as_outer(attrs);
569+
self.print_attrs(attrs);
569570
self.ann.pre(self, AnnNode::Item(item));
570571
match item.kind {
571572
hir::ItemKind::ExternCrate(orig_name, ident) => {
@@ -647,14 +648,13 @@ impl<'a> State<'a> {
647648
self.print_ident(ident);
648649
self.nbsp();
649650
self.bopen(ib);
650-
self.print_mod(mod_, attrs);
651+
self.print_mod(mod_);
651652
self.bclose(item.span, cb);
652653
}
653654
hir::ItemKind::ForeignMod { abi, items } => {
654655
let (cb, ib) = self.head("extern");
655656
self.word_nbsp(abi.to_string());
656657
self.bopen(ib);
657-
self.print_attrs_as_inner(self.attrs(item.hir_id()));
658658
for item in items {
659659
self.ann.nested(self, Nested::ForeignItem(item.id));
660660
}
@@ -731,7 +731,6 @@ impl<'a> State<'a> {
731731

732732
self.space();
733733
self.bopen(ib);
734-
self.print_attrs_as_inner(attrs);
735734
for impl_item in items {
736735
self.ann.nested(self, Nested::ImplItem(impl_item.id));
737736
}
@@ -822,7 +821,7 @@ impl<'a> State<'a> {
822821
for v in variants {
823822
self.space_if_not_bol();
824823
self.maybe_print_comment(v.span.lo());
825-
self.print_attrs_as_outer(self.attrs(v.hir_id));
824+
self.print_attrs(self.attrs(v.hir_id));
826825
let ib = self.ibox(INDENT_UNIT);
827826
self.print_variant(v);
828827
self.word(",");
@@ -857,7 +856,7 @@ impl<'a> State<'a> {
857856
self.popen();
858857
self.commasep(Inconsistent, struct_def.fields(), |s, field| {
859858
s.maybe_print_comment(field.span.lo());
860-
s.print_attrs_as_outer(s.attrs(field.hir_id));
859+
s.print_attrs(s.attrs(field.hir_id));
861860
s.print_type(field.ty);
862861
});
863862
self.pclose();
@@ -878,7 +877,7 @@ impl<'a> State<'a> {
878877
for field in struct_def.fields() {
879878
self.hardbreak_if_not_bol();
880879
self.maybe_print_comment(field.span.lo());
881-
self.print_attrs_as_outer(self.attrs(field.hir_id));
880+
self.print_attrs(self.attrs(field.hir_id));
882881
self.print_ident(field.ident);
883882
self.word_nbsp(":");
884883
self.print_type(field.ty);
@@ -916,7 +915,7 @@ impl<'a> State<'a> {
916915
self.ann.pre(self, AnnNode::SubItem(ti.hir_id()));
917916
self.hardbreak_if_not_bol();
918917
self.maybe_print_comment(ti.span.lo());
919-
self.print_attrs_as_outer(self.attrs(ti.hir_id()));
918+
self.print_attrs(self.attrs(ti.hir_id()));
920919
match ti.kind {
921920
hir::TraitItemKind::Const(ty, default) => {
922921
self.print_associated_const(ti.ident, ti.generics, ty, default);
@@ -944,7 +943,7 @@ impl<'a> State<'a> {
944943
self.ann.pre(self, AnnNode::SubItem(ii.hir_id()));
945944
self.hardbreak_if_not_bol();
946945
self.maybe_print_comment(ii.span.lo());
947-
self.print_attrs_as_outer(self.attrs(ii.hir_id()));
946+
self.print_attrs(self.attrs(ii.hir_id()));
948947

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

10301029
fn print_block(&mut self, blk: &hir::Block<'_>, cb: BoxMarker, ib: BoxMarker) {
1031-
self.print_block_with_attrs(blk, &[], cb, ib)
1030+
self.print_block_maybe_unclosed(blk, Some(cb), ib)
10321031
}
10331032

10341033
fn print_block_unclosed(&mut self, blk: &hir::Block<'_>, ib: BoxMarker) {
1035-
self.print_block_maybe_unclosed(blk, &[], None, ib)
1036-
}
1037-
1038-
fn print_block_with_attrs(
1039-
&mut self,
1040-
blk: &hir::Block<'_>,
1041-
attrs: &[hir::Attribute],
1042-
cb: BoxMarker,
1043-
ib: BoxMarker,
1044-
) {
1045-
self.print_block_maybe_unclosed(blk, attrs, Some(cb), ib)
1034+
self.print_block_maybe_unclosed(blk, None, ib)
10461035
}
10471036

10481037
fn print_block_maybe_unclosed(
10491038
&mut self,
10501039
blk: &hir::Block<'_>,
1051-
attrs: &[hir::Attribute],
10521040
cb: Option<BoxMarker>,
10531041
ib: BoxMarker,
10541042
) {
@@ -1060,8 +1048,6 @@ impl<'a> State<'a> {
10601048
self.ann.pre(self, AnnNode::Block(blk));
10611049
self.bopen(ib);
10621050

1063-
self.print_attrs_as_inner(attrs);
1064-
10651051
for st in blk.stmts {
10661052
self.print_stmt(st);
10671053
}
@@ -1251,7 +1237,7 @@ impl<'a> State<'a> {
12511237

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

14521438
fn print_expr(&mut self, expr: &hir::Expr<'_>) {
14531439
self.maybe_print_comment(expr.span.lo());
1454-
self.print_attrs_as_outer(self.attrs(expr.hir_id));
1440+
self.print_attrs(self.attrs(expr.hir_id));
14551441
let ib = self.ibox(INDENT_UNIT);
14561442
self.ann.pre(self, AnnNode::Expr(expr));
14571443
match expr.kind {
@@ -2076,7 +2062,7 @@ impl<'a> State<'a> {
20762062
self.space();
20772063
}
20782064
let cb = self.cbox(INDENT_UNIT);
2079-
self.print_attrs_as_outer(self.attrs(field.hir_id));
2065+
self.print_attrs(self.attrs(field.hir_id));
20802066
if !field.is_shorthand {
20812067
self.print_ident(field.ident);
20822068
self.word_nbsp(":");
@@ -2086,7 +2072,7 @@ impl<'a> State<'a> {
20862072
}
20872073

20882074
fn print_param(&mut self, arg: &hir::Param<'_>) {
2089-
self.print_attrs_as_outer(self.attrs(arg.hir_id));
2075+
self.print_attrs(self.attrs(arg.hir_id));
20902076
self.print_pat(arg.pat);
20912077
}
20922078

@@ -2121,7 +2107,7 @@ impl<'a> State<'a> {
21212107
let cb = self.cbox(INDENT_UNIT);
21222108
self.ann.pre(self, AnnNode::Arm(arm));
21232109
let ib = self.ibox(0);
2124-
self.print_attrs_as_outer(self.attrs(arm.hir_id));
2110+
self.print_attrs(self.attrs(arm.hir_id));
21252111
self.print_pat(arm.pat);
21262112
self.space();
21272113
if let Some(ref g) = arm.guard {
@@ -2409,7 +2395,7 @@ impl<'a> State<'a> {
24092395
}
24102396

24112397
fn print_where_predicate(&mut self, predicate: &hir::WherePredicate<'_>) {
2412-
self.print_attrs_as_outer(self.attrs(predicate.hir_id));
2398+
self.print_attrs(self.attrs(predicate.hir_id));
24132399
match *predicate.kind {
24142400
hir::WherePredicateKind::BoundPredicate(hir::WhereBoundPredicate {
24152401
bound_generic_params,

0 commit comments

Comments
 (0)