ast: Stop using Mod
in Crate
Crate root is sufficiently different from `mod` items, at least at syntactic level. Also remove customization point for "`mod` item or crate root" from AST visitors.
This commit is contained in:
parent
d1462d8558
commit
eb65f15c78
23 changed files with 114 additions and 179 deletions
|
@ -15,11 +15,11 @@ use rustc_span::source_map::{respan, DesugaringKind};
|
|||
use rustc_span::symbol::{kw, sym, Ident};
|
||||
use rustc_span::Span;
|
||||
use rustc_target::spec::abi;
|
||||
|
||||
use smallvec::{smallvec, SmallVec};
|
||||
use std::collections::BTreeSet;
|
||||
use tracing::debug;
|
||||
|
||||
use std::mem;
|
||||
|
||||
pub(super) struct ItemLowerer<'a, 'lowering, 'hir> {
|
||||
pub(super) lctx: &'a mut LoweringContext<'lowering, 'hir>,
|
||||
}
|
||||
|
@ -34,25 +34,6 @@ impl ItemLowerer<'_, '_, '_> {
|
|||
}
|
||||
|
||||
impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
|
||||
fn visit_mod(&mut self, m: &'a Mod, _s: Span, _attrs: &[Attribute], n: NodeId) {
|
||||
let def_id = self.lctx.lower_node_id(n).expect_owner();
|
||||
|
||||
self.lctx.modules.insert(
|
||||
def_id,
|
||||
hir::ModuleItems {
|
||||
items: BTreeSet::new(),
|
||||
trait_items: BTreeSet::new(),
|
||||
impl_items: BTreeSet::new(),
|
||||
foreign_items: BTreeSet::new(),
|
||||
},
|
||||
);
|
||||
|
||||
let old = self.lctx.current_module;
|
||||
self.lctx.current_module = def_id;
|
||||
visit::walk_mod(self, m);
|
||||
self.lctx.current_module = old;
|
||||
}
|
||||
|
||||
fn visit_item(&mut self, item: &'a Item) {
|
||||
let mut item_hir_id = None;
|
||||
self.lctx.with_hir_id_owner(item.id, |lctx| {
|
||||
|
@ -67,10 +48,18 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
|
|||
if let Some(hir_id) = item_hir_id {
|
||||
self.lctx.with_parent_item_lifetime_defs(hir_id, |this| {
|
||||
let this = &mut ItemLowerer { lctx: this };
|
||||
if let ItemKind::Impl(box ImplKind { ref of_trait, .. }) = item.kind {
|
||||
this.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item));
|
||||
} else {
|
||||
visit::walk_item(this, item);
|
||||
match item.kind {
|
||||
ItemKind::Mod(..) => {
|
||||
let def_id = this.lctx.lower_node_id(item.id).expect_owner();
|
||||
let old_current_module =
|
||||
mem::replace(&mut this.lctx.current_module, def_id);
|
||||
visit::walk_item(this, item);
|
||||
this.lctx.current_module = old_current_module;
|
||||
}
|
||||
ItemKind::Impl(box ImplKind { ref of_trait, .. }) => {
|
||||
this.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item));
|
||||
}
|
||||
_ => visit::walk_item(this, item),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -94,13 +83,13 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
|
|||
let hir_item = lctx.lower_trait_item(item);
|
||||
let id = hir_item.trait_item_id();
|
||||
lctx.trait_items.insert(id, hir_item);
|
||||
lctx.modules.get_mut(&lctx.current_module).unwrap().trait_items.insert(id);
|
||||
lctx.modules.entry(lctx.current_module).or_default().trait_items.insert(id);
|
||||
}
|
||||
AssocCtxt::Impl => {
|
||||
let hir_item = lctx.lower_impl_item(item);
|
||||
let id = hir_item.impl_item_id();
|
||||
lctx.impl_items.insert(id, hir_item);
|
||||
lctx.modules.get_mut(&lctx.current_module).unwrap().impl_items.insert(id);
|
||||
lctx.modules.entry(lctx.current_module).or_default().impl_items.insert(id);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -113,7 +102,7 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
|
|||
let hir_item = lctx.lower_foreign_item(item);
|
||||
let id = hir_item.foreign_item_id();
|
||||
lctx.foreign_items.insert(id, hir_item);
|
||||
lctx.modules.get_mut(&lctx.current_module).unwrap().foreign_items.insert(id);
|
||||
lctx.modules.entry(lctx.current_module).or_default().foreign_items.insert(id);
|
||||
});
|
||||
|
||||
visit::walk_foreign_item(self, item);
|
||||
|
@ -157,7 +146,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
|||
&mut self,
|
||||
f: impl FnOnce(&mut LoweringContext<'_, '_>) -> T,
|
||||
) -> T {
|
||||
let old_in_scope_lifetimes = std::mem::replace(&mut self.in_scope_lifetimes, vec![]);
|
||||
let old_in_scope_lifetimes = mem::replace(&mut self.in_scope_lifetimes, vec![]);
|
||||
|
||||
// this vector is only used when walking over impl headers,
|
||||
// input types, and the like, and should not be non-empty in
|
||||
|
@ -172,12 +161,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
|||
res
|
||||
}
|
||||
|
||||
pub(super) fn lower_mod(&mut self, m: &Mod) -> hir::Mod<'hir> {
|
||||
pub(super) fn lower_mod(&mut self, items: &[P<Item>], inner: Span) -> hir::Mod<'hir> {
|
||||
hir::Mod {
|
||||
inner: m.inner,
|
||||
item_ids: self
|
||||
.arena
|
||||
.alloc_from_iter(m.items.iter().flat_map(|x| self.lower_item_id(x))),
|
||||
inner,
|
||||
item_ids: self.arena.alloc_from_iter(items.iter().flat_map(|x| self.lower_item_id(x))),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -327,7 +314,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
|||
hir::ItemKind::Fn(sig, generics, body_id)
|
||||
})
|
||||
}
|
||||
ItemKind::Mod(ref m) => hir::ItemKind::Mod(self.lower_mod(m)),
|
||||
ItemKind::Mod(ref m) => hir::ItemKind::Mod(self.lower_mod(&m.items, m.inner)),
|
||||
ItemKind::ForeignMod(ref fm) => {
|
||||
if fm.abi.is_none() {
|
||||
self.maybe_lint_missing_abi(span, id, abi::Abi::C);
|
||||
|
|
|
@ -560,7 +560,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
|||
visit::walk_crate(&mut MiscCollector { lctx: &mut self }, c);
|
||||
visit::walk_crate(&mut item::ItemLowerer { lctx: &mut self }, c);
|
||||
|
||||
let module = self.lower_mod(&c.module);
|
||||
let module = self.lower_mod(&c.items, c.span);
|
||||
let attrs = self.lower_attrs(&c.attrs);
|
||||
let body_ids = body_ids(&self.bodies);
|
||||
let proc_macros =
|
||||
|
@ -608,7 +608,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
|||
fn insert_item(&mut self, item: hir::Item<'hir>) -> hir::ItemId {
|
||||
let id = hir::ItemId { def_id: item.def_id };
|
||||
self.items.insert(id, item);
|
||||
self.modules.get_mut(&self.current_module).unwrap().items.insert(id);
|
||||
self.modules.entry(self.current_module).or_default().items.insert(id);
|
||||
id
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue