Skip to content

Commit 356dd3d

Browse files
Clean up ItemTree lowering now that it's 1:1
1 parent b52df91 commit 356dd3d

File tree

1 file changed

+26
-53
lines changed

1 file changed

+26
-53
lines changed

crates/hir_def/src/item_tree/lower.rs

Lines changed: 26 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use std::{collections::hash_map::Entry, mem, sync::Arc};
44

55
use hir_expand::{ast_id_map::AstIdMap, hygiene::Hygiene, name::known, HirFileId};
6-
use smallvec::SmallVec;
76
use syntax::{
87
ast::{self, ModuleItemOwner},
98
SyntaxNode, WalkEvent,
@@ -20,17 +19,6 @@ fn id<N: ItemTreeNode>(index: Idx<N>) -> FileItemTreeId<N> {
2019
FileItemTreeId { index, _p: PhantomData }
2120
}
2221

23-
struct ModItems(SmallVec<[ModItem; 1]>);
24-
25-
impl<T> From<T> for ModItems
26-
where
27-
T: Into<ModItem>,
28-
{
29-
fn from(t: T) -> Self {
30-
ModItems(SmallVec::from_buf([t.into(); 1]))
31-
}
32-
}
33-
3422
pub(super) struct Ctx<'a> {
3523
db: &'a dyn DefDatabase,
3624
tree: ItemTree,
@@ -53,11 +41,8 @@ impl<'a> Ctx<'a> {
5341
}
5442

5543
pub(super) fn lower_module_items(mut self, item_owner: &dyn ModuleItemOwner) -> ItemTree {
56-
self.tree.top_level = item_owner
57-
.items()
58-
.flat_map(|item| self.lower_mod_item(&item, false))
59-
.flat_map(|items| items.0)
60-
.collect();
44+
self.tree.top_level =
45+
item_owner.items().flat_map(|item| self.lower_mod_item(&item, false)).collect();
6146
self.tree
6247
}
6348

@@ -69,7 +54,6 @@ impl<'a> Ctx<'a> {
6954
_ => None,
7055
})
7156
.flat_map(|item| self.lower_mod_item(&item, false))
72-
.flat_map(|items| items.0)
7357
.collect();
7458

7559
// Non-items need to have their inner items collected.
@@ -96,7 +80,7 @@ impl<'a> Ctx<'a> {
9680
self.tree.data_mut()
9781
}
9882

99-
fn lower_mod_item(&mut self, item: &ast::Item, inner: bool) -> Option<ModItems> {
83+
fn lower_mod_item(&mut self, item: &ast::Item, inner: bool) -> Option<ModItem> {
10084
// Collect inner items for 1-to-1-lowered items.
10185
match item {
10286
ast::Item::Struct(_)
@@ -127,34 +111,28 @@ impl<'a> Ctx<'a> {
127111
};
128112

129113
let attrs = RawAttrs::new(self.db, item, &self.hygiene);
130-
let items = match item {
131-
ast::Item::Struct(ast) => self.lower_struct(ast).map(Into::into),
132-
ast::Item::Union(ast) => self.lower_union(ast).map(Into::into),
133-
ast::Item::Enum(ast) => self.lower_enum(ast).map(Into::into),
134-
ast::Item::Fn(ast) => self.lower_function(ast).map(Into::into),
135-
ast::Item::TypeAlias(ast) => self.lower_type_alias(ast).map(Into::into),
136-
ast::Item::Static(ast) => self.lower_static(ast).map(Into::into),
137-
ast::Item::Const(ast) => Some(self.lower_const(ast).into()),
138-
ast::Item::Module(ast) => self.lower_module(ast).map(Into::into),
139-
ast::Item::Trait(ast) => self.lower_trait(ast).map(Into::into),
140-
ast::Item::Impl(ast) => self.lower_impl(ast).map(Into::into),
141-
ast::Item::Use(ast) => Some(ModItems(
142-
self.lower_use(ast).into_iter().map(Into::into).collect::<SmallVec<_>>(),
143-
)),
144-
ast::Item::ExternCrate(ast) => self.lower_extern_crate(ast).map(Into::into),
145-
ast::Item::MacroCall(ast) => self.lower_macro_call(ast).map(Into::into),
146-
ast::Item::MacroRules(ast) => self.lower_macro_rules(ast).map(Into::into),
147-
ast::Item::MacroDef(ast) => self.lower_macro_def(ast).map(Into::into),
148-
ast::Item::ExternBlock(ast) => Some(self.lower_extern_block(ast).into()),
114+
let item: ModItem = match item {
115+
ast::Item::Struct(ast) => self.lower_struct(ast)?.into(),
116+
ast::Item::Union(ast) => self.lower_union(ast)?.into(),
117+
ast::Item::Enum(ast) => self.lower_enum(ast)?.into(),
118+
ast::Item::Fn(ast) => self.lower_function(ast)?.into(),
119+
ast::Item::TypeAlias(ast) => self.lower_type_alias(ast)?.into(),
120+
ast::Item::Static(ast) => self.lower_static(ast)?.into(),
121+
ast::Item::Const(ast) => self.lower_const(ast).into(),
122+
ast::Item::Module(ast) => self.lower_module(ast)?.into(),
123+
ast::Item::Trait(ast) => self.lower_trait(ast)?.into(),
124+
ast::Item::Impl(ast) => self.lower_impl(ast)?.into(),
125+
ast::Item::Use(ast) => self.lower_use(ast)?.into(),
126+
ast::Item::ExternCrate(ast) => self.lower_extern_crate(ast)?.into(),
127+
ast::Item::MacroCall(ast) => self.lower_macro_call(ast)?.into(),
128+
ast::Item::MacroRules(ast) => self.lower_macro_rules(ast)?.into(),
129+
ast::Item::MacroDef(ast) => self.lower_macro_def(ast)?.into(),
130+
ast::Item::ExternBlock(ast) => self.lower_extern_block(ast).into(),
149131
};
150132

151-
if !attrs.is_empty() {
152-
for item in items.iter().flat_map(|items| &items.0) {
153-
self.add_attrs((*item).into(), attrs.clone());
154-
}
155-
}
133+
self.add_attrs(item.into(), attrs.clone());
156134

157-
items
135+
Some(item)
158136
}
159137

160138
fn add_attrs(&mut self, item: AttrOwner, attrs: RawAttrs) {
@@ -188,12 +166,10 @@ impl<'a> Ctx<'a> {
188166
},
189167
ast::Item(item) => {
190168
// FIXME: This triggers for macro calls in expression/pattern/type position
191-
let mod_items = self.lower_mod_item(&item, true);
169+
let mod_item = self.lower_mod_item(&item, true);
192170
let current_block = block_stack.last();
193-
if let (Some(mod_items), Some(block)) = (mod_items, current_block) {
194-
if !mod_items.0.is_empty() {
195-
self.data().inner_items.entry(*block).or_default().extend(mod_items.0.iter().copied());
196-
}
171+
if let (Some(mod_item), Some(block)) = (mod_item, current_block) {
172+
self.data().inner_items.entry(*block).or_default().push(mod_item);
197173
}
198174
},
199175
_ => {}
@@ -478,10 +454,7 @@ impl<'a> Ctx<'a> {
478454
items: module
479455
.item_list()
480456
.map(|list| {
481-
list.items()
482-
.flat_map(|item| self.lower_mod_item(&item, false))
483-
.flat_map(|items| items.0)
484-
.collect()
457+
list.items().flat_map(|item| self.lower_mod_item(&item, false)).collect()
485458
})
486459
.unwrap_or_else(|| {
487460
cov_mark::hit!(name_res_works_for_broken_modules);

0 commit comments

Comments
 (0)