Skip to content

Commit 6e4fb20

Browse files
committed
Make lowering pull-based.
1 parent 41902f2 commit 6e4fb20

File tree

2 files changed

+120
-55
lines changed

2 files changed

+120
-55
lines changed

Diff for: compiler/rustc_ast_lowering/src/item.rs

+62-46
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
use super::{AnonymousLifetimeMode, LoweringContext, ParamMode};
2-
use super::{ImplTraitContext, ImplTraitPosition};
2+
use super::{AstOwner, ImplTraitContext, ImplTraitPosition};
33
use crate::{Arena, FnDeclKind};
44

55
use rustc_ast::ptr::P;
6-
use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor};
6+
use rustc_ast::visit::AssocCtxt;
77
use rustc_ast::*;
88
use rustc_data_structures::fx::FxHashSet;
99
use rustc_errors::struct_span_err;
1010
use rustc_hir as hir;
1111
use rustc_hir::def::{DefKind, Res};
12-
use rustc_hir::def_id::LocalDefId;
13-
use rustc_index::vec::Idx;
12+
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
13+
use rustc_index::vec::{Idx, IndexVec};
1414
use rustc_span::source_map::{respan, DesugaringKind};
1515
use rustc_span::symbol::{kw, sym, Ident};
1616
use rustc_span::Span;
@@ -23,6 +23,7 @@ use std::mem;
2323

2424
pub(super) struct ItemLowerer<'a, 'lowering, 'hir> {
2525
pub(super) lctx: &'a mut LoweringContext<'lowering, 'hir>,
26+
pub(super) ast_index: &'a IndexVec<LocalDefId, AstOwner<'lowering>>,
2627
}
2728

2829
/// When we have a ty alias we *may* have two where clauses. To give the best diagnostics, we set the span
@@ -45,7 +46,7 @@ fn add_ty_alias_where_clause(
4546
}
4647
}
4748

48-
impl ItemLowerer<'_, '_, '_> {
49+
impl<'a, 'hir> ItemLowerer<'_, 'a, 'hir> {
4950
/// Clears (and restores) the `in_scope_lifetimes` field. Used when
5051
/// visiting nested items, which never inherit in-scope lifetimes
5152
/// from their surrounding environment.
@@ -73,10 +74,9 @@ impl ItemLowerer<'_, '_, '_> {
7374
/// for them.
7475
fn with_parent_item_lifetime_defs(
7576
&mut self,
76-
parent_item: LocalDefId,
77+
parent_hir: &'hir hir::Item<'hir>,
7778
f: impl FnOnce(&mut Self),
7879
) {
79-
let parent_hir = self.lctx.owners[parent_item].unwrap().node().expect_item();
8080
let parent_generics = match parent_hir.kind {
8181
hir::ItemKind::Impl(hir::Impl { ref generics, .. })
8282
| hir::ItemKind::Trait(_, _, ref generics, ..) => generics.params,
@@ -98,65 +98,81 @@ impl ItemLowerer<'_, '_, '_> {
9898
self.lctx.in_scope_lifetimes = old_in_scope_lifetimes;
9999
}
100100

101-
fn with_trait_impl_ref(&mut self, impl_ref: &Option<TraitRef>, f: impl FnOnce(&mut Self)) {
101+
fn with_trait_impl_ref(
102+
&mut self,
103+
impl_ref: &Option<hir::TraitRef<'_>>,
104+
f: impl FnOnce(&mut Self),
105+
) {
102106
let old = self.lctx.is_in_trait_impl;
103107
self.lctx.is_in_trait_impl = impl_ref.is_some();
104108
let ret = f(self);
105109
self.lctx.is_in_trait_impl = old;
106110
ret
107111
}
108-
}
109112

110-
impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
111-
fn visit_attribute(&mut self, _: &'a Attribute) {
112-
// We do not want to lower expressions that appear in attributes,
113-
// as they are not accessible to the rest of the HIR.
113+
pub(super) fn lower_node(
114+
&mut self,
115+
def_id: LocalDefId,
116+
) -> hir::MaybeOwner<&'hir hir::OwnerInfo<'hir>> {
117+
self.lctx.owners.ensure_contains_elem(def_id, || hir::MaybeOwner::Phantom);
118+
if let hir::MaybeOwner::Phantom = self.lctx.owners[def_id] {
119+
let node = self.ast_index[def_id];
120+
match node {
121+
AstOwner::NonOwner => {}
122+
AstOwner::Crate(c) => self.lower_crate(c),
123+
AstOwner::Item(item) => self.lower_item(item),
124+
AstOwner::AssocItem(item, ctxt) => self.lower_assoc_item(item, ctxt),
125+
AstOwner::ForeignItem(item) => self.lower_foreign_item(item),
126+
}
127+
}
128+
129+
self.lctx.owners[def_id]
114130
}
115131

116-
fn visit_item(&mut self, item: &'a Item) {
117-
let hir_id = self.without_in_scope_lifetime_defs(|this| {
118-
this.lctx.with_hir_id_owner(item.id, |lctx| {
119-
let node = lctx.lower_item(item);
120-
hir::OwnerNode::Item(node)
121-
})
122-
});
132+
fn lower_crate(&mut self, c: &'a Crate) {
133+
debug_assert_eq!(self.lctx.resolver.local_def_id(CRATE_NODE_ID), CRATE_DEF_ID);
123134

124-
self.with_parent_item_lifetime_defs(hir_id, |this| match item.kind {
125-
ItemKind::Impl(box Impl { ref of_trait, .. }) => {
126-
this.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item));
127-
}
128-
_ => visit::walk_item(this, item),
129-
})
135+
self.lctx.with_hir_id_owner(CRATE_NODE_ID, |lctx| {
136+
let module = lctx.lower_mod(&c.items, c.spans.inner_span);
137+
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs);
138+
hir::OwnerNode::Crate(lctx.arena.alloc(module))
139+
});
130140
}
131141

132-
fn visit_fn(&mut self, fk: FnKind<'a>, sp: Span, _: NodeId) {
133-
match fk {
134-
FnKind::Fn(FnCtxt::Foreign, _, sig, _, _) => {
135-
self.visit_fn_header(&sig.header);
136-
visit::walk_fn_decl(self, &sig.decl);
137-
// Don't visit the foreign function body even if it has one, since lowering the
138-
// body would have no meaning and will have already been caught as a parse error.
139-
}
140-
_ => visit::walk_fn(self, fk, sp),
141-
}
142+
fn lower_item(&mut self, item: &'a Item) {
143+
self.without_in_scope_lifetime_defs(|this| {
144+
this.lctx.with_hir_id_owner(item.id, |lctx| hir::OwnerNode::Item(lctx.lower_item(item)))
145+
});
142146
}
143147

144-
fn visit_assoc_item(&mut self, item: &'a AssocItem, ctxt: AssocCtxt) {
145-
debug!(in_scope_lifetimes = ?self.lctx.in_scope_lifetimes);
146-
self.lctx.with_hir_id_owner(item.id, |lctx| match ctxt {
147-
AssocCtxt::Trait => hir::OwnerNode::TraitItem(lctx.lower_trait_item(item)),
148-
AssocCtxt::Impl => hir::OwnerNode::ImplItem(lctx.lower_impl_item(item)),
149-
});
148+
fn lower_assoc_item(&mut self, item: &'a AssocItem, ctxt: AssocCtxt) {
149+
let def_id = self.lctx.resolver.local_def_id(item.id);
150150

151-
visit::walk_assoc_item(self, item, ctxt);
151+
let do_lower = |lctx: &mut LoweringContext<'_, '_>| {
152+
lctx.with_hir_id_owner(item.id, |lctx| match ctxt {
153+
AssocCtxt::Trait => hir::OwnerNode::TraitItem(lctx.lower_trait_item(item)),
154+
AssocCtxt::Impl => hir::OwnerNode::ImplItem(lctx.lower_impl_item(item)),
155+
});
156+
};
157+
158+
let parent_id = {
159+
let parent = self.lctx.resolver.definitions().def_key(def_id).parent;
160+
let local_def_index = parent.unwrap();
161+
LocalDefId { local_def_index }
162+
};
163+
let parent_hir = self.lower_node(parent_id).unwrap().node().expect_item();
164+
self.with_parent_item_lifetime_defs(parent_hir, |this| match parent_hir.kind {
165+
hir::ItemKind::Impl(hir::Impl { ref of_trait, .. }) => {
166+
this.with_trait_impl_ref(of_trait, |this| do_lower(this.lctx))
167+
}
168+
_ => do_lower(this.lctx),
169+
});
152170
}
153171

154-
fn visit_foreign_item(&mut self, item: &'a ForeignItem) {
172+
fn lower_foreign_item(&mut self, item: &'a ForeignItem) {
155173
self.lctx.with_hir_id_owner(item.id, |lctx| {
156174
hir::OwnerNode::ForeignItem(lctx.lower_foreign_item(item))
157175
});
158-
159-
visit::walk_foreign_item(self, item);
160176
}
161177
}
162178

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+58-9
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,59 @@ impl FnDeclKind {
332332
}
333333
}
334334

335+
#[derive(Copy, Clone)]
336+
enum AstOwner<'a> {
337+
NonOwner,
338+
Crate(&'a ast::Crate),
339+
Item(&'a ast::Item),
340+
AssocItem(&'a ast::AssocItem, visit::AssocCtxt),
341+
ForeignItem(&'a ast::ForeignItem),
342+
}
343+
344+
fn index_crate<'a>(
345+
resolver: &dyn ResolverAstLowering,
346+
krate: &'a Crate,
347+
) -> IndexVec<LocalDefId, AstOwner<'a>> {
348+
let mut indexer = Indexer { resolver, index: IndexVec::new() };
349+
indexer.index.ensure_contains_elem(CRATE_DEF_ID, || AstOwner::NonOwner);
350+
indexer.index[CRATE_DEF_ID] = AstOwner::Crate(krate);
351+
visit::walk_crate(&mut indexer, krate);
352+
return indexer.index;
353+
354+
struct Indexer<'s, 'a> {
355+
resolver: &'s dyn ResolverAstLowering,
356+
index: IndexVec<LocalDefId, AstOwner<'a>>,
357+
}
358+
359+
impl<'a> visit::Visitor<'a> for Indexer<'_, 'a> {
360+
fn visit_attribute(&mut self, _: &'a Attribute) {
361+
// We do not want to lower expressions that appear in attributes,
362+
// as they are not accessible to the rest of the HIR.
363+
}
364+
365+
fn visit_item(&mut self, item: &'a ast::Item) {
366+
let def_id = self.resolver.local_def_id(item.id);
367+
self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner);
368+
self.index[def_id] = AstOwner::Item(item);
369+
visit::walk_item(self, item)
370+
}
371+
372+
fn visit_assoc_item(&mut self, item: &'a ast::AssocItem, ctxt: visit::AssocCtxt) {
373+
let def_id = self.resolver.local_def_id(item.id);
374+
self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner);
375+
self.index[def_id] = AstOwner::AssocItem(item, ctxt);
376+
visit::walk_assoc_item(self, item, ctxt);
377+
}
378+
379+
fn visit_foreign_item(&mut self, item: &'a ast::ForeignItem) {
380+
let def_id = self.resolver.local_def_id(item.id);
381+
self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner);
382+
self.index[def_id] = AstOwner::ForeignItem(item);
383+
visit::walk_foreign_item(self, item);
384+
}
385+
}
386+
}
387+
335388
/// Compute the hash for the HIR of the full crate.
336389
/// This hash will then be part of the crate_hash which is stored in the metadata.
337390
fn compute_hir_hash(
@@ -363,6 +416,8 @@ pub fn lower_crate<'a, 'hir>(
363416
) -> &'hir hir::Crate<'hir> {
364417
let _prof_timer = sess.prof.verbose_generic_activity("hir_lowering");
365418

419+
let ast_index = index_crate(resolver, krate);
420+
366421
let owners =
367422
IndexVec::from_fn_n(|_| hir::MaybeOwner::Phantom, resolver.definitions().def_index_count());
368423
let mut lctx = LoweringContext {
@@ -395,15 +450,9 @@ pub fn lower_crate<'a, 'hir>(
395450
allow_into_future: Some([sym::into_future][..].into()),
396451
};
397452

398-
// Lower the root module manually.
399-
debug_assert_eq!(lctx.resolver.local_def_id(CRATE_NODE_ID), CRATE_DEF_ID);
400-
lctx.with_hir_id_owner(CRATE_NODE_ID, |lctx| {
401-
let module = lctx.lower_mod(&krate.items, krate.spans.inner_span);
402-
lctx.lower_attrs(hir::CRATE_HIR_ID, &krate.attrs);
403-
hir::OwnerNode::Crate(lctx.arena.alloc(module))
404-
});
405-
406-
visit::walk_crate(&mut item::ItemLowerer { lctx: &mut lctx }, krate);
453+
for def_id in ast_index.indices() {
454+
item::ItemLowerer { lctx: &mut lctx, ast_index: &ast_index }.lower_node(def_id);
455+
}
407456
let owners = lctx.owners;
408457

409458
let hir_hash = compute_hir_hash(resolver, &owners);

0 commit comments

Comments
 (0)