Skip to content

Commit d9a1faa

Browse files
committed
Introduce lowering_arena to avoid creating AST nodes on the fly
1 parent 3c72788 commit d9a1faa

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

Diff for: compiler/rustc_ast_lowering/src/item.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::errors::{InvalidAbi, MisplacedRelaxTraitBound};
22
use super::ResolverAstLoweringExt;
3-
use super::{AstOwner, ImplTraitContext, ImplTraitPosition};
3+
use super::{Arena, AstOwner, ImplTraitContext, ImplTraitPosition};
44
use super::{FnDeclKind, LoweringContext, ParamMode};
55

66
use rustc_ast::ptr::P;
@@ -25,6 +25,7 @@ use std::iter;
2525
pub(super) struct ItemLowerer<'a, 'hir> {
2626
pub(super) tcx: TyCtxt<'hir>,
2727
pub(super) resolver: &'a mut ResolverAstLowering,
28+
pub(super) ast_arena: &'a Arena<'static>,
2829
pub(super) ast_index: &'a IndexVec<LocalDefId, AstOwner<'a>>,
2930
pub(super) owners: &'a mut IndexVec<LocalDefId, hir::MaybeOwner<&'hir hir::OwnerInfo<'hir>>>,
3031
}
@@ -60,6 +61,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
6061
tcx: self.tcx,
6162
resolver: self.resolver,
6263
arena: self.tcx.hir_arena,
64+
ast_arena: self.ast_arena,
6365

6466
// HirId handling.
6567
bodies: Vec::new(),

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+35-20
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ extern crate tracing;
4444

4545
use crate::errors::{AssocTyParentheses, AssocTyParenthesesSub, MisplacedImplTrait};
4646

47+
use rustc_arena::declare_arena;
4748
use rustc_ast::ptr::P;
4849
use rustc_ast::visit;
4950
use rustc_ast::{self as ast, *};
@@ -95,6 +96,13 @@ struct LoweringContext<'a, 'hir> {
9596
/// Used to allocate HIR nodes.
9697
arena: &'hir hir::Arena<'hir>,
9798

99+
/// Used to allocate temporary AST nodes for use during lowering.
100+
/// This allows us to create "fake" AST -- these nodes can sometimes
101+
/// be allocated on the stack, but other times we need them to live longer
102+
/// than the current stack frame, so they can be collected into vectors
103+
/// and things like that.
104+
ast_arena: &'a Arena<'static>,
105+
98106
/// Bodies inside the owner being lowered.
99107
bodies: Vec<(hir::ItemLocalId, &'hir hir::Body<'hir>)>,
100108
/// Attributes inside the owner being lowered.
@@ -140,6 +148,15 @@ struct LoweringContext<'a, 'hir> {
140148
generics_def_id_map: Vec<FxHashMap<LocalDefId, LocalDefId>>,
141149
}
142150

151+
declare_arena!([
152+
[] tys: rustc_ast::Ty,
153+
[] aba: rustc_ast::AngleBracketedArgs,
154+
[] ptr: rustc_ast::PolyTraitRef,
155+
// This _marker field is needed because `declare_arena` creates `Arena<'tcx>` and we need to
156+
// use `'tcx`. If we don't have this we get a compile error.
157+
[] _marker: std::marker::PhantomData<&'tcx ()>,
158+
]);
159+
143160
trait ResolverAstLoweringExt {
144161
fn legacy_const_generic_args(&self, expr: &Expr) -> Option<Vec<usize>>;
145162
fn get_partial_res(&self, id: NodeId) -> Option<PartialRes>;
@@ -401,10 +418,13 @@ pub fn lower_to_hir<'hir>(tcx: TyCtxt<'hir>, (): ()) -> hir::Crate<'hir> {
401418
tcx.definitions_untracked().def_index_count(),
402419
);
403420

421+
let ast_arena = Arena::default();
422+
404423
for def_id in ast_index.indices() {
405424
item::ItemLowerer {
406425
tcx,
407426
resolver: &mut resolver,
427+
ast_arena: &ast_arena,
408428
ast_index: &ast_index,
409429
owners: &mut owners,
410430
}
@@ -964,12 +984,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
964984
}
965985
GenericArgs::Parenthesized(ref data) => {
966986
self.emit_bad_parenthesized_trait_in_assoc_ty(data);
967-
self.lower_angle_bracketed_parameter_data(
968-
&data.as_angle_bracketed_args(),
969-
ParamMode::Explicit,
970-
itctx,
971-
)
972-
.0
987+
let aba = self.ast_arena.aba.alloc(data.as_angle_bracketed_args());
988+
self.lower_angle_bracketed_parameter_data(aba, ParamMode::Explicit, itctx).0
973989
}
974990
};
975991
gen_args_ctor.into_generic_args(self)
@@ -1037,15 +1053,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10371053

10381054
self.with_dyn_type_scope(false, |this| {
10391055
let node_id = this.next_node_id();
1040-
let ty = this.lower_ty(
1041-
&Ty {
1042-
id: node_id,
1043-
kind: TyKind::ImplTrait(impl_trait_node_id, bounds.clone()),
1044-
span: this.lower_span(constraint.span),
1045-
tokens: None,
1046-
},
1047-
itctx,
1048-
);
1056+
let ty = this.ast_arena.tys.alloc(Ty {
1057+
id: node_id,
1058+
kind: TyKind::ImplTrait(impl_trait_node_id, bounds.clone()),
1059+
span: this.lower_span(constraint.span),
1060+
tokens: None,
1061+
});
1062+
let ty = this.lower_ty(ty, itctx);
10491063

10501064
hir::TypeBindingKind::Equality { term: ty.into() }
10511065
})
@@ -1181,12 +1195,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11811195
&& let Res::Def(DefKind::Trait | DefKind::TraitAlias, _) = partial_res.base_res()
11821196
{
11831197
let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| {
1198+
let poly_trait_ref = this.ast_arena.ptr.alloc(PolyTraitRef {
1199+
bound_generic_params: vec![],
1200+
trait_ref: TraitRef { path: path.clone(), ref_id: t.id },
1201+
span: t.span
1202+
});
11841203
let bound = this.lower_poly_trait_ref(
1185-
&PolyTraitRef {
1186-
bound_generic_params: vec![],
1187-
trait_ref: TraitRef { path: path.clone(), ref_id: t.id },
1188-
span: t.span
1189-
},
1204+
poly_trait_ref,
11901205
itctx,
11911206
);
11921207
let bounds = this.arena.alloc_from_iter([bound]);

0 commit comments

Comments
 (0)