Skip to content

Commit 0e2f90b

Browse files
fee1-deadcjgillot
andcommitted
Make lowering incremental.
Co-authored-by: Camille Gillot <[email protected]>
1 parent 478a0e5 commit 0e2f90b

File tree

14 files changed

+221
-237
lines changed

14 files changed

+221
-237
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4033,6 +4033,17 @@ impl TryFrom<ItemKind> for ForeignItemKind {
40334033

40344034
pub type ForeignItem = Item<ForeignItemKind>;
40354035

4036+
#[derive(Debug)]
4037+
pub enum AstOwner<'a> {
4038+
NonOwner,
4039+
Synthetic(rustc_span::def_id::LocalDefId),
4040+
Crate(&'a Crate),
4041+
Item(&'a Item),
4042+
TraitItem(&'a AssocItem),
4043+
ImplItem(&'a AssocItem),
4044+
ForeignItem(&'a ForeignItem),
4045+
}
4046+
40364047
// Some nodes are used a lot. Make sure they don't unintentionally get bigger.
40374048
#[cfg(target_pointer_width = "64")]
40384049
mod size_asserts {

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 54 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
use rustc_abi::ExternAbi;
22
use rustc_ast::ptr::P;
3-
use rustc_ast::visit::AssocCtxt;
43
use rustc_ast::*;
54
use rustc_errors::ErrorGuaranteed;
65
use rustc_hir::def::{DefKind, PerNS, Res};
7-
use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
6+
use rustc_hir::def_id::CRATE_DEF_ID;
87
use rustc_hir::{self as hir, HirId, LifetimeSource, PredicateOrigin};
9-
use rustc_index::{IndexSlice, IndexVec};
108
use rustc_middle::span_bug;
119
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
1210
use rustc_span::edit_distance::find_best_match_for_name;
13-
use rustc_span::{DUMMY_SP, DesugaringKind, Ident, Span, Symbol, kw, sym};
11+
use rustc_span::{DesugaringKind, Ident, Span, Symbol, kw, sym};
1412
use smallvec::{SmallVec, smallvec};
1513
use thin_vec::ThinVec;
1614
use tracing::instrument;
@@ -21,15 +19,13 @@ use super::errors::{
2119
};
2220
use super::stability::{enabled_names, gate_unstable_abi};
2321
use super::{
24-
AstOwner, FnDeclKind, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
22+
FnDeclKind, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
2523
ResolverAstLoweringExt,
2624
};
2725

28-
pub(super) struct ItemLowerer<'a, 'hir> {
26+
pub(super) struct ItemLowerer<'hir> {
2927
pub(super) tcx: TyCtxt<'hir>,
3028
pub(super) resolver: &'hir ResolverAstLowering,
31-
pub(super) ast_index: &'a IndexSlice<LocalDefId, AstOwner<'a>>,
32-
pub(super) owners: &'a mut IndexVec<LocalDefId, hir::MaybeOwner<'hir>>,
3329
}
3430

3531
/// When we have a ty alias we *may* have two where clauses. To give the best diagnostics, we set the span
@@ -53,51 +49,47 @@ fn add_ty_alias_where_clause(
5349
generics.where_clause.span = where_clause.span;
5450
}
5551

56-
impl<'a, 'hir> ItemLowerer<'a, 'hir> {
52+
impl<'hir> ItemLowerer<'hir> {
5753
fn with_lctx(
5854
&mut self,
5955
owner: NodeId,
6056
f: impl FnOnce(&mut LoweringContext<'hir>) -> hir::OwnerNode<'hir>,
61-
) {
62-
let mut lctx = LoweringContext::new(self.tcx, self.resolver);
63-
lctx.with_hir_id_owner(owner, |lctx| f(lctx));
64-
65-
for (def_id, info) in lctx.children {
66-
let owner = self.owners.ensure_contains_elem(def_id, || hir::MaybeOwner::Phantom);
67-
assert!(
68-
matches!(owner, hir::MaybeOwner::Phantom),
69-
"duplicate copy of {def_id:?} in lctx.children"
70-
);
71-
*owner = info;
72-
}
57+
) -> hir::MaybeOwner<'hir> {
58+
let mut lctx = LoweringContext::new(self.tcx, self.resolver, owner);
59+
60+
let item = f(&mut lctx);
61+
debug_assert_eq!(lctx.current_hir_id_owner, item.def_id());
62+
63+
let info = lctx.make_owner_info(item);
64+
65+
hir::MaybeOwner::Owner(lctx.arena.alloc(info))
7366
}
7467

75-
pub(super) fn lower_node(&mut self, def_id: LocalDefId) {
76-
let owner = self.owners.ensure_contains_elem(def_id, || hir::MaybeOwner::Phantom);
77-
if let hir::MaybeOwner::Phantom = owner {
78-
let node = self.ast_index[def_id];
79-
match node {
80-
AstOwner::NonOwner => {}
81-
AstOwner::Crate(c) => {
82-
assert_eq!(self.resolver.node_id_to_def_id[&CRATE_NODE_ID], CRATE_DEF_ID);
83-
self.with_lctx(CRATE_NODE_ID, |lctx| {
84-
let module = lctx.lower_mod(&c.items, &c.spans);
85-
// FIXME(jdonszelman): is dummy span ever a problem here?
86-
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, DUMMY_SP);
87-
hir::OwnerNode::Crate(module)
88-
})
89-
}
90-
AstOwner::Item(item) => {
91-
self.with_lctx(item.id, |lctx| hir::OwnerNode::Item(lctx.lower_item(item)))
92-
}
93-
AstOwner::AssocItem(item, ctxt) => {
94-
self.with_lctx(item.id, |lctx| lctx.lower_assoc_item(item, ctxt))
95-
}
96-
AstOwner::ForeignItem(item) => self.with_lctx(item.id, |lctx| {
97-
hir::OwnerNode::ForeignItem(lctx.lower_foreign_item(item))
98-
}),
99-
}
100-
}
68+
#[instrument(level = "debug", skip(self, c))]
69+
pub(super) fn lower_crate(&mut self, c: &Crate) -> hir::MaybeOwner<'hir> {
70+
debug_assert_eq!(self.resolver.node_id_to_def_id[&CRATE_NODE_ID], CRATE_DEF_ID);
71+
self.with_lctx(CRATE_NODE_ID, |lctx| {
72+
let module = lctx.lower_mod(&c.items, &c.spans);
73+
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, c.spans.inner_span);
74+
hir::OwnerNode::Crate(module)
75+
})
76+
}
77+
78+
#[instrument(level = "debug", skip(self))]
79+
pub(super) fn lower_item(&mut self, item: &Item) -> hir::MaybeOwner<'hir> {
80+
self.with_lctx(item.id, |lctx| hir::OwnerNode::Item(lctx.lower_item(item)))
81+
}
82+
83+
pub(super) fn lower_trait_item(&mut self, item: &AssocItem) -> hir::MaybeOwner<'hir> {
84+
self.with_lctx(item.id, |lctx| hir::OwnerNode::TraitItem(lctx.lower_trait_item(item)))
85+
}
86+
87+
pub(super) fn lower_impl_item(&mut self, item: &AssocItem) -> hir::MaybeOwner<'hir> {
88+
self.with_lctx(item.id, |lctx| hir::OwnerNode::ImplItem(lctx.lower_impl_item(item)))
89+
}
90+
91+
pub(super) fn lower_foreign_item(&mut self, item: &ForeignItem) -> hir::MaybeOwner<'hir> {
92+
self.with_lctx(item.id, |lctx| hir::OwnerNode::ForeignItem(lctx.lower_foreign_item(item)))
10193
}
10294
}
10395

@@ -137,12 +129,13 @@ impl<'hir> LoweringContext<'hir> {
137129
}
138130

139131
fn lower_item(&mut self, i: &Item) -> &'hir hir::Item<'hir> {
132+
let owner_id = self.current_hir_id_owner;
133+
let hir_id: HirId = owner_id.into();
140134
let vis_span = self.lower_span(i.vis.span);
141-
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
142135
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
143136
let kind = self.lower_item_kind(i.span, i.id, hir_id, attrs, vis_span, &i.kind);
144137
let item = hir::Item {
145-
owner_id: hir_id.expect_owner(),
138+
owner_id,
146139
kind,
147140
vis_span,
148141
span: self.lower_span(i.span),
@@ -627,21 +620,9 @@ impl<'hir> LoweringContext<'hir> {
627620
}
628621
}
629622

630-
fn lower_assoc_item(&mut self, item: &AssocItem, ctxt: AssocCtxt) -> hir::OwnerNode<'hir> {
631-
// Evaluate with the lifetimes in `params` in-scope.
632-
// This is used to track which lifetimes have already been defined,
633-
// and which need to be replicated when lowering an async fn.
634-
match ctxt {
635-
AssocCtxt::Trait => hir::OwnerNode::TraitItem(self.lower_trait_item(item)),
636-
AssocCtxt::Impl { of_trait } => {
637-
hir::OwnerNode::ImplItem(self.lower_impl_item(item, of_trait))
638-
}
639-
}
640-
}
641-
642623
fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> {
643-
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
644-
let owner_id = hir_id.expect_owner();
624+
let owner_id = self.current_hir_id_owner;
625+
let hir_id: HirId = owner_id.into();
645626
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
646627
let (ident, kind) = match &i.kind {
647628
ForeignItemKind::Fn(box Fn { sig, ident, generics, define_opaque, .. }) => {
@@ -818,9 +799,9 @@ impl<'hir> LoweringContext<'hir> {
818799
}
819800

820801
fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
821-
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
802+
let trait_item_def_id = self.current_hir_id_owner;
803+
let hir_id: HirId = trait_item_def_id.into();
822804
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
823-
let trait_item_def_id = hir_id.expect_owner();
824805

825806
let (ident, generics, kind, has_default) = match &i.kind {
826807
AssocItemKind::Const(box ConstItem {
@@ -1004,15 +985,16 @@ impl<'hir> LoweringContext<'hir> {
1004985
self.expr(span, hir::ExprKind::Err(guar))
1005986
}
1006987

1007-
fn lower_impl_item(
1008-
&mut self,
1009-
i: &AssocItem,
1010-
is_in_trait_impl: bool,
1011-
) -> &'hir hir::ImplItem<'hir> {
988+
fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
989+
let owner_id = self.current_hir_id_owner;
990+
let hir_id: HirId = owner_id.into();
991+
let parent_id = self.tcx.local_parent(owner_id.def_id);
992+
let is_in_trait_impl =
993+
matches!(self.tcx.def_kind(parent_id), DefKind::Impl { of_trait: true });
994+
1012995
// Since `default impl` is not yet implemented, this is always true in impls.
1013996
let has_value = true;
1014997
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
1015-
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
1016998
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
1017999

10181000
let (ident, (generics, kind)) = match &i.kind {
@@ -1119,7 +1101,7 @@ impl<'hir> LoweringContext<'hir> {
11191101
};
11201102

11211103
let item = hir::ImplItem {
1122-
owner_id: hir_id.expect_owner(),
1104+
owner_id,
11231105
ident: self.lower_ident(ident),
11241106
generics,
11251107
kind,

0 commit comments

Comments
 (0)