Skip to content

Commit 92df99e

Browse files
committed
All HIR attributes are outer
1 parent 49b2636 commit 92df99e

File tree

9 files changed

+128
-151
lines changed

9 files changed

+128
-151
lines changed

compiler/rustc_hir/src/hir.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,14 +1207,6 @@ pub enum Attribute {
12071207
}
12081208

12091209
impl Attribute {
1210-
pub fn style(&self) -> AttrStyle {
1211-
match &self {
1212-
Attribute::Unparsed(u) => u.style,
1213-
Attribute::Parsed(AttributeKind::DocComment { style, .. }) => *style,
1214-
_ => panic!(),
1215-
}
1216-
}
1217-
12181210
pub fn get_normal_item(&self) -> &AttrItem {
12191211
match &self {
12201212
Attribute::Unparsed(normal) => &normal,
@@ -2289,16 +2281,9 @@ pub struct Expr<'hir> {
22892281
}
22902282

22912283
impl Expr<'_> {
2292-
pub fn precedence(
2293-
&self,
2294-
for_each_attr: &dyn Fn(HirId, &mut dyn FnMut(&Attribute)),
2295-
) -> ExprPrecedence {
2284+
pub fn precedence(&self, has_attr: &dyn Fn(HirId) -> bool) -> ExprPrecedence {
22962285
let prefix_attrs_precedence = || -> ExprPrecedence {
2297-
let mut has_outer_attr = false;
2298-
for_each_attr(self.hir_id, &mut |attr: &Attribute| {
2299-
has_outer_attr |= matches!(attr.style(), AttrStyle::Outer)
2300-
});
2301-
if has_outer_attr { ExprPrecedence::Prefix } else { ExprPrecedence::Unambiguous }
2286+
if has_attr(self.hir_id) { ExprPrecedence::Prefix } else { ExprPrecedence::Unambiguous }
23022287
};
23032288

23042289
match &self.kind {
@@ -2354,7 +2339,7 @@ impl Expr<'_> {
23542339
| ExprKind::Use(..)
23552340
| ExprKind::Err(_) => prefix_attrs_precedence(),
23562341

2357-
ExprKind::DropTemps(expr, ..) => expr.precedence(for_each_attr),
2342+
ExprKind::DropTemps(expr, ..) => expr.precedence(has_attr),
23582343
}
23592344
}
23602345

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,

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_errors::{
1818
use rustc_hir::def::{CtorKind, DefKind, Res};
1919
use rustc_hir::def_id::DefId;
2020
use rustc_hir::lang_items::LangItem;
21-
use rustc_hir::{Attribute, ExprKind, HirId, QPath};
21+
use rustc_hir::{ExprKind, HirId, QPath};
2222
use rustc_hir_analysis::NoVariantNamed;
2323
use rustc_hir_analysis::hir_ty_lowering::{FeedConstTy, HirTyLowerer as _};
2424
use rustc_infer::infer;
@@ -56,7 +56,7 @@ use crate::{
5656

5757
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
5858
pub(crate) fn precedence(&self, expr: &hir::Expr<'_>) -> ExprPrecedence {
59-
let for_each_attr = |id: HirId, callback: &mut dyn FnMut(&Attribute)| {
59+
let has_attr = |id: HirId| -> bool {
6060
for attr in self.tcx.hir_attrs(id) {
6161
// For the purpose of rendering suggestions, disregard attributes
6262
// that originate from desugaring of any kind. For example, `x?`
@@ -72,11 +72,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
7272
// let y: u32 = (x?).try_into().unwrap();
7373
// + +++++++++++++++++++++
7474
if attr.span().desugaring_kind().is_none() {
75-
callback(attr);
75+
return true;
7676
}
7777
}
78+
false
7879
};
79-
expr.precedence(&for_each_attr)
80+
expr.precedence(&has_attr)
8081
}
8182

8283
/// Check an expr with an expectation type, and also demand that the expr's

compiler/rustc_lint/src/context.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -855,14 +855,15 @@ impl<'tcx> LateContext<'tcx> {
855855
/// rendering diagnostic. This is not the same as the precedence that would
856856
/// be used for pretty-printing HIR by rustc_hir_pretty.
857857
pub fn precedence(&self, expr: &hir::Expr<'_>) -> ExprPrecedence {
858-
let for_each_attr = |id: hir::HirId, callback: &mut dyn FnMut(&hir::Attribute)| {
858+
let has_attr = |id: hir::HirId| -> bool {
859859
for attr in self.tcx.hir_attrs(id) {
860860
if attr.span().desugaring_kind().is_none() {
861-
callback(attr);
861+
return true;
862862
}
863863
}
864+
false
864865
};
865-
expr.precedence(&for_each_attr)
866+
expr.precedence(&has_attr)
866867
}
867868

868869
/// If the given expression is a local binding, find the initializer expression.

0 commit comments

Comments
 (0)