Skip to content

Commit 8ba90fa

Browse files
fee1-deadcjgillot
andcommitted
Make lowering incremental.
Co-authored-by: Camille Gillot <gillot.camille@gmail.com>
1 parent c52f549 commit 8ba90fa

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
@@ -4054,6 +4054,17 @@ impl TryFrom<ItemKind> for ForeignItemKind {
40544054

40554055
pub type ForeignItem = Item<ForeignItemKind>;
40564056

4057+
#[derive(Debug)]
4058+
pub enum AstOwner<'a> {
4059+
NonOwner,
4060+
Synthetic(rustc_span::def_id::LocalDefId),
4061+
Crate(&'a Crate),
4062+
Item(&'a Item),
4063+
TraitItem(&'a AssocItem),
4064+
ImplItem(&'a AssocItem),
4065+
ForeignItem(&'a ForeignItem),
4066+
}
4067+
40574068
// Some nodes are used a lot. Make sure they don't unintentionally get bigger.
40584069
#[cfg(target_pointer_width = "64")]
40594070
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),
@@ -626,21 +619,9 @@ impl<'hir> LoweringContext<'hir> {
626619
}
627620
}
628621

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

813794
fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
814-
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
795+
let trait_item_def_id = self.current_hir_id_owner;
796+
let hir_id: HirId = trait_item_def_id.into();
815797
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
816-
let trait_item_def_id = hir_id.expect_owner();
817798

818799
let (ident, generics, kind, has_default) = match &i.kind {
819800
AssocItemKind::Const(box ConstItem {
@@ -973,15 +954,16 @@ impl<'hir> LoweringContext<'hir> {
973954
self.expr(span, hir::ExprKind::Err(guar))
974955
}
975956

976-
fn lower_impl_item(
977-
&mut self,
978-
i: &AssocItem,
979-
is_in_trait_impl: bool,
980-
) -> &'hir hir::ImplItem<'hir> {
957+
fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
958+
let owner_id = self.current_hir_id_owner;
959+
let hir_id: HirId = owner_id.into();
960+
let parent_id = self.tcx.local_parent(owner_id.def_id);
961+
let is_in_trait_impl =
962+
matches!(self.tcx.def_kind(parent_id), DefKind::Impl { of_trait: true });
963+
981964
// Since `default impl` is not yet implemented, this is always true in impls.
982965
let has_value = true;
983966
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
984-
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
985967
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
986968

987969
let (ident, (generics, kind)) = match &i.kind {
@@ -1088,7 +1070,7 @@ impl<'hir> LoweringContext<'hir> {
10881070
};
10891071

10901072
let item = hir::ImplItem {
1091-
owner_id: hir_id.expect_owner(),
1073+
owner_id,
10921074
ident: self.lower_ident(ident),
10931075
generics,
10941076
kind,

0 commit comments

Comments
 (0)