Skip to content

Commit 1499c08

Browse files
Rollup merge of rust-lang#101499 - spastorino:add-lowering-arena, r=oli-obk
Introduce lowering_arena to avoid creating AST nodes on the fly `@oli-obk` requested this and other changes as a way of simplifying rust-lang#101345. This is just going to make the diff of rust-lang#101345 smaller. r? `@oli-obk` `@cjgillot`
2 parents 2c4dc4f + d9a1faa commit 1499c08

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

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(),

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
}
@@ -974,12 +994,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
974994
}
975995
GenericArgs::Parenthesized(ref data) => {
976996
self.emit_bad_parenthesized_trait_in_assoc_ty(data);
977-
self.lower_angle_bracketed_parameter_data(
978-
&data.as_angle_bracketed_args(),
979-
ParamMode::Explicit,
980-
itctx,
981-
)
982-
.0
997+
let aba = self.ast_arena.aba.alloc(data.as_angle_bracketed_args());
998+
self.lower_angle_bracketed_parameter_data(aba, ParamMode::Explicit, itctx).0
983999
}
9841000
};
9851001
gen_args_ctor.into_generic_args(self)
@@ -1048,15 +1064,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10481064

10491065
self.with_dyn_type_scope(false, |this| {
10501066
let node_id = this.next_node_id();
1051-
let ty = this.lower_ty(
1052-
&Ty {
1053-
id: node_id,
1054-
kind: TyKind::ImplTrait(impl_trait_node_id, bounds.clone()),
1055-
span: this.lower_span(constraint.span),
1056-
tokens: None,
1057-
},
1058-
itctx,
1059-
);
1067+
let ty = this.ast_arena.tys.alloc(Ty {
1068+
id: node_id,
1069+
kind: TyKind::ImplTrait(impl_trait_node_id, bounds.clone()),
1070+
span: this.lower_span(constraint.span),
1071+
tokens: None,
1072+
});
1073+
let ty = this.lower_ty(ty, itctx);
10601074

10611075
hir::TypeBindingKind::Equality { term: ty.into() }
10621076
})
@@ -1192,12 +1206,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11921206
&& let Res::Def(DefKind::Trait | DefKind::TraitAlias, _) = partial_res.base_res()
11931207
{
11941208
let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| {
1209+
let poly_trait_ref = this.ast_arena.ptr.alloc(PolyTraitRef {
1210+
bound_generic_params: vec![],
1211+
trait_ref: TraitRef { path: path.clone(), ref_id: t.id },
1212+
span: t.span
1213+
});
11951214
let bound = this.lower_poly_trait_ref(
1196-
&PolyTraitRef {
1197-
bound_generic_params: vec![],
1198-
trait_ref: TraitRef { path: path.clone(), ref_id: t.id },
1199-
span: t.span
1200-
},
1215+
poly_trait_ref,
12011216
itctx,
12021217
);
12031218
let bounds = this.arena.alloc_from_iter([bound]);

0 commit comments

Comments
 (0)