Clean up ItemTree lowering now that it's 1:1

This commit is contained in:
Jonas Schievink 2021-05-26 01:09:31 +02:00
parent b52df91877
commit 356dd3d909

View File

@ -3,7 +3,6 @@
use std::{collections::hash_map::Entry, mem, sync::Arc};
use hir_expand::{ast_id_map::AstIdMap, hygiene::Hygiene, name::known, HirFileId};
use smallvec::SmallVec;
use syntax::{
ast::{self, ModuleItemOwner},
SyntaxNode, WalkEvent,
@ -20,17 +19,6 @@ fn id<N: ItemTreeNode>(index: Idx<N>) -> FileItemTreeId<N> {
FileItemTreeId { index, _p: PhantomData }
}
struct ModItems(SmallVec<[ModItem; 1]>);
impl<T> From<T> for ModItems
where
T: Into<ModItem>,
{
fn from(t: T) -> Self {
ModItems(SmallVec::from_buf([t.into(); 1]))
}
}
pub(super) struct Ctx<'a> {
db: &'a dyn DefDatabase,
tree: ItemTree,
@ -53,11 +41,8 @@ pub(super) fn new(db: &'a dyn DefDatabase, hygiene: Hygiene, file: HirFileId) ->
}
pub(super) fn lower_module_items(mut self, item_owner: &dyn ModuleItemOwner) -> ItemTree {
self.tree.top_level = item_owner
.items()
.flat_map(|item| self.lower_mod_item(&item, false))
.flat_map(|items| items.0)
.collect();
self.tree.top_level =
item_owner.items().flat_map(|item| self.lower_mod_item(&item, false)).collect();
self.tree
}
@ -69,7 +54,6 @@ pub(super) fn lower_macro_stmts(mut self, stmts: ast::MacroStmts) -> ItemTree {
_ => None,
})
.flat_map(|item| self.lower_mod_item(&item, false))
.flat_map(|items| items.0)
.collect();
// Non-items need to have their inner items collected.
@ -96,7 +80,7 @@ fn data(&mut self) -> &mut ItemTreeData {
self.tree.data_mut()
}
fn lower_mod_item(&mut self, item: &ast::Item, inner: bool) -> Option<ModItems> {
fn lower_mod_item(&mut self, item: &ast::Item, inner: bool) -> Option<ModItem> {
// Collect inner items for 1-to-1-lowered items.
match item {
ast::Item::Struct(_)
@ -127,34 +111,28 @@ fn lower_mod_item(&mut self, item: &ast::Item, inner: bool) -> Option<ModItems>
};
let attrs = RawAttrs::new(self.db, item, &self.hygiene);
let items = match item {
ast::Item::Struct(ast) => self.lower_struct(ast).map(Into::into),
ast::Item::Union(ast) => self.lower_union(ast).map(Into::into),
ast::Item::Enum(ast) => self.lower_enum(ast).map(Into::into),
ast::Item::Fn(ast) => self.lower_function(ast).map(Into::into),
ast::Item::TypeAlias(ast) => self.lower_type_alias(ast).map(Into::into),
ast::Item::Static(ast) => self.lower_static(ast).map(Into::into),
ast::Item::Const(ast) => Some(self.lower_const(ast).into()),
ast::Item::Module(ast) => self.lower_module(ast).map(Into::into),
ast::Item::Trait(ast) => self.lower_trait(ast).map(Into::into),
ast::Item::Impl(ast) => self.lower_impl(ast).map(Into::into),
ast::Item::Use(ast) => Some(ModItems(
self.lower_use(ast).into_iter().map(Into::into).collect::<SmallVec<_>>(),
)),
ast::Item::ExternCrate(ast) => self.lower_extern_crate(ast).map(Into::into),
ast::Item::MacroCall(ast) => self.lower_macro_call(ast).map(Into::into),
ast::Item::MacroRules(ast) => self.lower_macro_rules(ast).map(Into::into),
ast::Item::MacroDef(ast) => self.lower_macro_def(ast).map(Into::into),
ast::Item::ExternBlock(ast) => Some(self.lower_extern_block(ast).into()),
let item: ModItem = match item {
ast::Item::Struct(ast) => self.lower_struct(ast)?.into(),
ast::Item::Union(ast) => self.lower_union(ast)?.into(),
ast::Item::Enum(ast) => self.lower_enum(ast)?.into(),
ast::Item::Fn(ast) => self.lower_function(ast)?.into(),
ast::Item::TypeAlias(ast) => self.lower_type_alias(ast)?.into(),
ast::Item::Static(ast) => self.lower_static(ast)?.into(),
ast::Item::Const(ast) => self.lower_const(ast).into(),
ast::Item::Module(ast) => self.lower_module(ast)?.into(),
ast::Item::Trait(ast) => self.lower_trait(ast)?.into(),
ast::Item::Impl(ast) => self.lower_impl(ast)?.into(),
ast::Item::Use(ast) => self.lower_use(ast)?.into(),
ast::Item::ExternCrate(ast) => self.lower_extern_crate(ast)?.into(),
ast::Item::MacroCall(ast) => self.lower_macro_call(ast)?.into(),
ast::Item::MacroRules(ast) => self.lower_macro_rules(ast)?.into(),
ast::Item::MacroDef(ast) => self.lower_macro_def(ast)?.into(),
ast::Item::ExternBlock(ast) => self.lower_extern_block(ast).into(),
};
if !attrs.is_empty() {
for item in items.iter().flat_map(|items| &items.0) {
self.add_attrs((*item).into(), attrs.clone());
}
}
self.add_attrs(item.into(), attrs.clone());
items
Some(item)
}
fn add_attrs(&mut self, item: AttrOwner, attrs: RawAttrs) {
@ -188,12 +166,10 @@ fn collect_inner_items(&mut self, container: &SyntaxNode) {
},
ast::Item(item) => {
// FIXME: This triggers for macro calls in expression/pattern/type position
let mod_items = self.lower_mod_item(&item, true);
let mod_item = self.lower_mod_item(&item, true);
let current_block = block_stack.last();
if let (Some(mod_items), Some(block)) = (mod_items, current_block) {
if !mod_items.0.is_empty() {
self.data().inner_items.entry(*block).or_default().extend(mod_items.0.iter().copied());
}
if let (Some(mod_item), Some(block)) = (mod_item, current_block) {
self.data().inner_items.entry(*block).or_default().push(mod_item);
}
},
_ => {}
@ -478,10 +454,7 @@ fn lower_module(&mut self, module: &ast::Module) -> Option<FileItemTreeId<Mod>>
items: module
.item_list()
.map(|list| {
list.items()
.flat_map(|item| self.lower_mod_item(&item, false))
.flat_map(|items| items.0)
.collect()
list.items().flat_map(|item| self.lower_mod_item(&item, false)).collect()
})
.unwrap_or_else(|| {
cov_mark::hit!(name_res_works_for_broken_modules);