Skip to content

Commit c135761

Browse files
fee1-deadcjgillot
andcommitted
Make lowering incremental.
Co-authored-by: Camille Gillot <[email protected]>
1 parent 50ae203 commit c135761

File tree

14 files changed

+223
-237
lines changed

14 files changed

+223
-237
lines changed

compiler/rustc_ast/src/ast.rs

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

41094109
pub type ForeignItem = Item<ForeignItemKind>;
41104110

4111+
#[derive(Debug)]
4112+
pub enum AstOwner<'a> {
4113+
NonOwner,
4114+
Synthetic(rustc_span::def_id::LocalDefId),
4115+
Crate(&'a Crate),
4116+
Item(&'a Item),
4117+
TraitItem(&'a AssocItem),
4118+
ImplItem(&'a AssocItem),
4119+
ForeignItem(&'a ForeignItem),
4120+
}
4121+
41114122
// Some nodes are used a lot. Make sure they don't unintentionally get bigger.
41124123
#[cfg(target_pointer_width = "64")]
41134124
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,17 +1,15 @@
11
use rustc_abi::ExternAbi;
22
use rustc_ast::ptr::P;
3-
use rustc_ast::visit::AssocCtxt;
43
use rustc_ast::*;
54
use rustc_attr_data_structures::{AttributeKind, find_attr};
65
use rustc_errors::{E0570, ErrorGuaranteed, struct_span_code_err};
76
use rustc_hir::def::{DefKind, PerNS, Res};
8-
use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
7+
use rustc_hir::def_id::CRATE_DEF_ID;
98
use rustc_hir::{self as hir, HirId, LifetimeSource, PredicateOrigin};
10-
use rustc_index::{IndexSlice, IndexVec};
119
use rustc_middle::span_bug;
1210
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
1311
use rustc_span::edit_distance::find_best_match_for_name;
14-
use rustc_span::{DUMMY_SP, DesugaringKind, Ident, Span, Symbol, kw, sym};
12+
use rustc_span::{DesugaringKind, Ident, Span, Symbol, kw, sym};
1513
use smallvec::{SmallVec, smallvec};
1614
use thin_vec::ThinVec;
1715
use tracing::instrument;
@@ -22,15 +20,13 @@ use super::errors::{
2220
};
2321
use super::stability::{enabled_names, gate_unstable_abi};
2422
use super::{
25-
AstOwner, FnDeclKind, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
23+
FnDeclKind, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
2624
ResolverAstLoweringExt,
2725
};
2826

29-
pub(super) struct ItemLowerer<'a, 'hir> {
27+
pub(super) struct ItemLowerer<'hir> {
3028
pub(super) tcx: TyCtxt<'hir>,
3129
pub(super) resolver: &'hir ResolverAstLowering,
32-
pub(super) ast_index: &'a IndexSlice<LocalDefId, AstOwner<'a>>,
33-
pub(super) owners: &'a mut IndexVec<LocalDefId, hir::MaybeOwner<'hir>>,
3430
}
3531

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

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

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

@@ -138,12 +130,13 @@ impl<'hir> LoweringContext<'hir> {
138130
}
139131

140132
fn lower_item(&mut self, i: &Item) -> &'hir hir::Item<'hir> {
133+
let owner_id = self.current_hir_id_owner;
134+
let hir_id: HirId = owner_id.into();
141135
let vis_span = self.lower_span(i.vis.span);
142-
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
143136
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
144137
let kind = self.lower_item_kind(i.span, i.id, hir_id, attrs, vis_span, &i.kind);
145138
let item = hir::Item {
146-
owner_id: hir_id.expect_owner(),
139+
owner_id,
147140
kind,
148141
vis_span,
149142
span: self.lower_span(i.span),
@@ -635,21 +628,9 @@ impl<'hir> LoweringContext<'hir> {
635628
}
636629
}
637630

638-
fn lower_assoc_item(&mut self, item: &AssocItem, ctxt: AssocCtxt) -> hir::OwnerNode<'hir> {
639-
// Evaluate with the lifetimes in `params` in-scope.
640-
// This is used to track which lifetimes have already been defined,
641-
// and which need to be replicated when lowering an async fn.
642-
match ctxt {
643-
AssocCtxt::Trait => hir::OwnerNode::TraitItem(self.lower_trait_item(item)),
644-
AssocCtxt::Impl { of_trait } => {
645-
hir::OwnerNode::ImplItem(self.lower_impl_item(item, of_trait))
646-
}
647-
}
648-
}
649-
650631
fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> {
651-
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
652-
let owner_id = hir_id.expect_owner();
632+
let owner_id = self.current_hir_id_owner;
633+
let hir_id: HirId = owner_id.into();
653634
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
654635
let (ident, kind) = match &i.kind {
655636
ForeignItemKind::Fn(box Fn { sig, ident, generics, define_opaque, .. }) => {
@@ -820,9 +801,9 @@ impl<'hir> LoweringContext<'hir> {
820801
}
821802

822803
fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
823-
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
804+
let trait_item_def_id = self.current_hir_id_owner;
805+
let hir_id: HirId = trait_item_def_id.into();
824806
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
825-
let trait_item_def_id = hir_id.expect_owner();
826807

827808
let (ident, generics, kind, has_default) = match &i.kind {
828809
AssocItemKind::Const(box ConstItem {
@@ -982,15 +963,16 @@ impl<'hir> LoweringContext<'hir> {
982963
self.expr(span, hir::ExprKind::Err(guar))
983964
}
984965

985-
fn lower_impl_item(
986-
&mut self,
987-
i: &AssocItem,
988-
is_in_trait_impl: bool,
989-
) -> &'hir hir::ImplItem<'hir> {
966+
fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
967+
let owner_id = self.current_hir_id_owner;
968+
let hir_id: HirId = owner_id.into();
969+
let parent_id = self.tcx.local_parent(owner_id.def_id);
970+
let is_in_trait_impl =
971+
matches!(self.tcx.def_kind(parent_id), DefKind::Impl { of_trait: true });
972+
990973
// Since `default impl` is not yet implemented, this is always true in impls.
991974
let has_value = true;
992975
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
993-
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
994976
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
995977

996978
let (ident, (generics, kind)) = match &i.kind {
@@ -1097,7 +1079,7 @@ impl<'hir> LoweringContext<'hir> {
10971079
};
10981080

10991081
let item = hir::ImplItem {
1100-
owner_id: hir_id.expect_owner(),
1082+
owner_id,
11011083
ident: self.lower_ident(ident),
11021084
generics,
11031085
kind,

0 commit comments

Comments
 (0)