Skip to content

Commit dff1edf

Browse files
committed
Auto merge of #79519 - cjgillot:noattr, r=wesleywiser
Store HIR attributes in a side table Same idea as #72015 but for attributes. The objective is to reduce incr-comp invalidations due to modified attributes. Notably, those due to modified doc comments. Implementation: - collect attributes during AST->HIR lowering, in `LocalDefId -> ItemLocalId -> &[Attributes]` nested tables; - access the attributes through a `hir_owner_attrs` query; - local refactorings to use this access; - remove `attrs` from HIR data structures one-by-one. Change in behaviour: - the HIR visitor traverses all attributes at once instead of parent-by-parent; - attribute arrays are sometimes duplicated: for statements and variant constructors; - as a consequence, attributes are marked as used after unused-attribute lint emission to avoid duplicate lints. ~~Current bug: the lint level is not correctly applied in `std::backtrace_rs`, triggering an unused attribute warning on `#![no_std]`. I welcome suggestions.~~
2 parents 861872b + 77c0f21 commit dff1edf

File tree

88 files changed

+869
-853
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+869
-853
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+30-43
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
258258
ex.span = e.span;
259259
}
260260
// Merge attributes into the inner expression.
261-
let mut attrs: Vec<_> = e.attrs.iter().map(|a| self.lower_attr(a)).collect();
262-
attrs.extend::<Vec<_>>(ex.attrs.into());
263-
ex.attrs = attrs.into();
261+
if !e.attrs.is_empty() {
262+
let old_attrs = self.attrs.get(&ex.hir_id).map(|la| *la).unwrap_or(&[]);
263+
self.attrs.insert(
264+
ex.hir_id,
265+
&*self.arena.alloc_from_iter(
266+
e.attrs
267+
.iter()
268+
.map(|a| self.lower_attr(a))
269+
.chain(old_attrs.iter().cloned()),
270+
),
271+
);
272+
}
264273
return ex;
265274
}
266275

@@ -272,12 +281,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
272281
ExprKind::MacCall(_) => panic!("{:?} shouldn't exist here", e.span),
273282
};
274283

275-
hir::Expr {
276-
hir_id: self.lower_node_id(e.id),
277-
kind,
278-
span: e.span,
279-
attrs: e.attrs.iter().map(|a| self.lower_attr(a)).collect::<Vec<_>>().into(),
280-
}
284+
let hir_id = self.lower_node_id(e.id);
285+
self.lower_attrs(hir_id, &e.attrs);
286+
hir::Expr { hir_id, kind, span: e.span }
281287
})
282288
}
283289

@@ -618,14 +624,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
618624
hir::Guard::If(self.lower_expr(cond))
619625
}
620626
});
621-
hir::Arm {
622-
hir_id: self.next_id(),
623-
attrs: self.lower_attrs(&arm.attrs),
624-
pat,
625-
guard,
626-
body: self.lower_expr(&arm.body),
627-
span: arm.span,
628-
}
627+
let hir_id = self.next_id();
628+
self.lower_attrs(hir_id, &arm.attrs);
629+
hir::Arm { hir_id, pat, guard, body: self.lower_expr(&arm.body), span: arm.span }
629630
}
630631

631632
/// Lower an `async` construct to a generator that is then wrapped so it implements `Future`.
@@ -669,7 +670,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
669670
Ident::with_dummy_span(sym::_task_context),
670671
hir::BindingAnnotation::Mutable,
671672
);
672-
let param = hir::Param { attrs: &[], hir_id: self.next_id(), pat, ty_span: span, span };
673+
let param = hir::Param { hir_id: self.next_id(), pat, ty_span: span, span };
673674
let params = arena_vec![self; param];
674675

675676
let body_id = self.lower_body(move |this| {
@@ -690,12 +691,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
690691
span,
691692
Some(hir::Movability::Static),
692693
);
693-
let generator = hir::Expr {
694-
hir_id: self.lower_node_id(closure_node_id),
695-
kind: generator_kind,
696-
span,
697-
attrs: ThinVec::new(),
698-
};
694+
let generator =
695+
hir::Expr { hir_id: self.lower_node_id(closure_node_id), kind: generator_kind, span };
699696

700697
// `future::from_generator`:
701698
let unstable_span =
@@ -849,7 +846,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
849846
hir_id: loop_hir_id,
850847
kind: hir::ExprKind::Loop(loop_block, None, hir::LoopSource::Loop, span),
851848
span,
852-
attrs: ThinVec::new(),
853849
});
854850

855851
// mut pinned => loop { ... }
@@ -1026,7 +1022,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10261022

10271023
// Introduce a `let` for destructuring: `let (lhs1, lhs2) = t`.
10281024
let destructure_let = self.stmt_let_pat(
1029-
ThinVec::new(),
1025+
None,
10301026
whole_span,
10311027
Some(rhs),
10321028
pat,
@@ -1785,7 +1781,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
17851781

17861782
// `let mut __next`
17871783
let next_let = self.stmt_let_pat(
1788-
ThinVec::new(),
1784+
None,
17891785
desugared_span,
17901786
None,
17911787
next_pat,
@@ -1795,7 +1791,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
17951791
// `let <pat> = __next`
17961792
let pat = self.lower_pat(pat);
17971793
let pat_let = self.stmt_let_pat(
1798-
ThinVec::new(),
1794+
None,
17991795
desugared_span,
18001796
Some(next_expr),
18011797
pat,
@@ -1819,12 +1815,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
18191815
hir::LoopSource::ForLoop,
18201816
e.span.with_hi(orig_head_span.hi()),
18211817
);
1822-
let loop_expr = self.arena.alloc(hir::Expr {
1823-
hir_id: self.lower_node_id(e.id),
1824-
kind,
1825-
span: e.span,
1826-
attrs: ThinVec::new(),
1827-
});
1818+
let loop_expr =
1819+
self.arena.alloc(hir::Expr { hir_id: self.lower_node_id(e.id), kind, span: e.span });
18281820

18291821
// `mut iter => { ... }`
18301822
let iter_arm = self.arm(iter_pat, loop_expr);
@@ -2159,21 +2151,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
21592151
kind: hir::ExprKind<'hir>,
21602152
attrs: AttrVec,
21612153
) -> hir::Expr<'hir> {
2162-
hir::Expr { hir_id: self.next_id(), kind, span, attrs }
2154+
let hir_id = self.next_id();
2155+
self.lower_attrs(hir_id, &attrs);
2156+
hir::Expr { hir_id, kind, span }
21632157
}
21642158

21652159
fn field(&mut self, ident: Ident, expr: &'hir hir::Expr<'hir>, span: Span) -> hir::Field<'hir> {
21662160
hir::Field { hir_id: self.next_id(), ident, span, expr, is_shorthand: false }
21672161
}
21682162

21692163
fn arm(&mut self, pat: &'hir hir::Pat<'hir>, expr: &'hir hir::Expr<'hir>) -> hir::Arm<'hir> {
2170-
hir::Arm {
2171-
hir_id: self.next_id(),
2172-
attrs: &[],
2173-
pat,
2174-
guard: None,
2175-
span: expr.span,
2176-
body: expr,
2177-
}
2164+
hir::Arm { hir_id: self.next_id(), pat, guard: None, span: expr.span, body: expr }
21782165
}
21792166
}

0 commit comments

Comments
 (0)