Introduce lowering_arena to avoid creating AST nodes on the fly

This commit is contained in:
Santiago Pastorino 2022-09-06 15:36:13 -03:00
parent 3c72788461
commit d9a1faaa9c
No known key found for this signature in database
GPG Key ID: 8131A24E0C79EFAF
2 changed files with 38 additions and 21 deletions

View File

@ -1,6 +1,6 @@
use super::errors::{InvalidAbi, MisplacedRelaxTraitBound}; use super::errors::{InvalidAbi, MisplacedRelaxTraitBound};
use super::ResolverAstLoweringExt; use super::ResolverAstLoweringExt;
use super::{AstOwner, ImplTraitContext, ImplTraitPosition}; use super::{Arena, AstOwner, ImplTraitContext, ImplTraitPosition};
use super::{FnDeclKind, LoweringContext, ParamMode}; use super::{FnDeclKind, LoweringContext, ParamMode};
use rustc_ast::ptr::P; use rustc_ast::ptr::P;
@ -25,6 +25,7 @@
pub(super) struct ItemLowerer<'a, 'hir> { pub(super) struct ItemLowerer<'a, 'hir> {
pub(super) tcx: TyCtxt<'hir>, pub(super) tcx: TyCtxt<'hir>,
pub(super) resolver: &'a mut ResolverAstLowering, pub(super) resolver: &'a mut ResolverAstLowering,
pub(super) ast_arena: &'a Arena<'static>,
pub(super) ast_index: &'a IndexVec<LocalDefId, AstOwner<'a>>, pub(super) ast_index: &'a IndexVec<LocalDefId, AstOwner<'a>>,
pub(super) owners: &'a mut IndexVec<LocalDefId, hir::MaybeOwner<&'hir hir::OwnerInfo<'hir>>>, pub(super) owners: &'a mut IndexVec<LocalDefId, hir::MaybeOwner<&'hir hir::OwnerInfo<'hir>>>,
} }
@ -60,6 +61,7 @@ fn with_lctx(
tcx: self.tcx, tcx: self.tcx,
resolver: self.resolver, resolver: self.resolver,
arena: self.tcx.hir_arena, arena: self.tcx.hir_arena,
ast_arena: self.ast_arena,
// HirId handling. // HirId handling.
bodies: Vec::new(), bodies: Vec::new(),

View File

@ -44,6 +44,7 @@
use crate::errors::{AssocTyParentheses, AssocTyParenthesesSub, MisplacedImplTrait}; use crate::errors::{AssocTyParentheses, AssocTyParenthesesSub, MisplacedImplTrait};
use rustc_arena::declare_arena;
use rustc_ast::ptr::P; use rustc_ast::ptr::P;
use rustc_ast::visit; use rustc_ast::visit;
use rustc_ast::{self as ast, *}; use rustc_ast::{self as ast, *};
@ -95,6 +96,13 @@ struct LoweringContext<'a, 'hir> {
/// Used to allocate HIR nodes. /// Used to allocate HIR nodes.
arena: &'hir hir::Arena<'hir>, arena: &'hir hir::Arena<'hir>,
/// Used to allocate temporary AST nodes for use during lowering.
/// This allows us to create "fake" AST -- these nodes can sometimes
/// be allocated on the stack, but other times we need them to live longer
/// than the current stack frame, so they can be collected into vectors
/// and things like that.
ast_arena: &'a Arena<'static>,
/// Bodies inside the owner being lowered. /// Bodies inside the owner being lowered.
bodies: Vec<(hir::ItemLocalId, &'hir hir::Body<'hir>)>, bodies: Vec<(hir::ItemLocalId, &'hir hir::Body<'hir>)>,
/// Attributes inside the owner being lowered. /// Attributes inside the owner being lowered.
@ -140,6 +148,15 @@ struct LoweringContext<'a, 'hir> {
generics_def_id_map: Vec<FxHashMap<LocalDefId, LocalDefId>>, generics_def_id_map: Vec<FxHashMap<LocalDefId, LocalDefId>>,
} }
declare_arena!([
[] tys: rustc_ast::Ty,
[] aba: rustc_ast::AngleBracketedArgs,
[] ptr: rustc_ast::PolyTraitRef,
// This _marker field is needed because `declare_arena` creates `Arena<'tcx>` and we need to
// use `'tcx`. If we don't have this we get a compile error.
[] _marker: std::marker::PhantomData<&'tcx ()>,
]);
trait ResolverAstLoweringExt { trait ResolverAstLoweringExt {
fn legacy_const_generic_args(&self, expr: &Expr) -> Option<Vec<usize>>; fn legacy_const_generic_args(&self, expr: &Expr) -> Option<Vec<usize>>;
fn get_partial_res(&self, id: NodeId) -> Option<PartialRes>; 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> {
tcx.definitions_untracked().def_index_count(), tcx.definitions_untracked().def_index_count(),
); );
let ast_arena = Arena::default();
for def_id in ast_index.indices() { for def_id in ast_index.indices() {
item::ItemLowerer { item::ItemLowerer {
tcx, tcx,
resolver: &mut resolver, resolver: &mut resolver,
ast_arena: &ast_arena,
ast_index: &ast_index, ast_index: &ast_index,
owners: &mut owners, owners: &mut owners,
} }
@ -964,12 +984,8 @@ fn lower_assoc_ty_constraint(
} }
GenericArgs::Parenthesized(ref data) => { GenericArgs::Parenthesized(ref data) => {
self.emit_bad_parenthesized_trait_in_assoc_ty(data); self.emit_bad_parenthesized_trait_in_assoc_ty(data);
self.lower_angle_bracketed_parameter_data( let aba = self.ast_arena.aba.alloc(data.as_angle_bracketed_args());
&data.as_angle_bracketed_args(), self.lower_angle_bracketed_parameter_data(aba, ParamMode::Explicit, itctx).0
ParamMode::Explicit,
itctx,
)
.0
} }
}; };
gen_args_ctor.into_generic_args(self) gen_args_ctor.into_generic_args(self)
@ -1037,15 +1053,13 @@ fn lower_assoc_ty_constraint(
self.with_dyn_type_scope(false, |this| { self.with_dyn_type_scope(false, |this| {
let node_id = this.next_node_id(); let node_id = this.next_node_id();
let ty = this.lower_ty( let ty = this.ast_arena.tys.alloc(Ty {
&Ty { id: node_id,
id: node_id, kind: TyKind::ImplTrait(impl_trait_node_id, bounds.clone()),
kind: TyKind::ImplTrait(impl_trait_node_id, bounds.clone()), span: this.lower_span(constraint.span),
span: this.lower_span(constraint.span), tokens: None,
tokens: None, });
}, let ty = this.lower_ty(ty, itctx);
itctx,
);
hir::TypeBindingKind::Equality { term: ty.into() } hir::TypeBindingKind::Equality { term: ty.into() }
}) })
@ -1181,12 +1195,13 @@ fn lower_path_ty(
&& let Res::Def(DefKind::Trait | DefKind::TraitAlias, _) = partial_res.base_res() && let Res::Def(DefKind::Trait | DefKind::TraitAlias, _) = partial_res.base_res()
{ {
let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| { let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| {
let poly_trait_ref = this.ast_arena.ptr.alloc(PolyTraitRef {
bound_generic_params: vec![],
trait_ref: TraitRef { path: path.clone(), ref_id: t.id },
span: t.span
});
let bound = this.lower_poly_trait_ref( let bound = this.lower_poly_trait_ref(
&PolyTraitRef { poly_trait_ref,
bound_generic_params: vec![],
trait_ref: TraitRef { path: path.clone(), ref_id: t.id },
span: t.span
},
itctx, itctx,
); );
let bounds = this.arena.alloc_from_iter([bound]); let bounds = this.arena.alloc_from_iter([bound]);