aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2021-05-26 00:09:31 +0100
committerJonas Schievink <[email protected]>2021-05-26 00:09:31 +0100
commit356dd3d909f20b5b1e1c44205d522b7b2ead0d0e (patch)
tree113bc5795ca3cd5118cb6754edbbe34fc150490d /crates/hir_def
parentb52df9187730abbcd9cbb132f7d184c74b9a3b7f (diff)
Clean up ItemTree lowering now that it's 1:1
Diffstat (limited to 'crates/hir_def')
-rw-r--r--crates/hir_def/src/item_tree/lower.rs79
1 files changed, 26 insertions, 53 deletions
diff --git a/crates/hir_def/src/item_tree/lower.rs b/crates/hir_def/src/item_tree/lower.rs
index a59a3dc37..798ab46dd 100644
--- a/crates/hir_def/src/item_tree/lower.rs
+++ b/crates/hir_def/src/item_tree/lower.rs
@@ -3,7 +3,6 @@
3use std::{collections::hash_map::Entry, mem, sync::Arc}; 3use std::{collections::hash_map::Entry, mem, sync::Arc};
4 4
5use hir_expand::{ast_id_map::AstIdMap, hygiene::Hygiene, name::known, HirFileId}; 5use hir_expand::{ast_id_map::AstIdMap, hygiene::Hygiene, name::known, HirFileId};
6use smallvec::SmallVec;
7use syntax::{ 6use syntax::{
8 ast::{self, ModuleItemOwner}, 7 ast::{self, ModuleItemOwner},
9 SyntaxNode, WalkEvent, 8 SyntaxNode, WalkEvent,
@@ -20,17 +19,6 @@ fn id<N: ItemTreeNode>(index: Idx<N>) -> FileItemTreeId<N> {
20 FileItemTreeId { index, _p: PhantomData } 19 FileItemTreeId { index, _p: PhantomData }
21} 20}
22 21
23struct ModItems(SmallVec<[ModItem; 1]>);
24
25impl<T> From<T> for ModItems
26where
27 T: Into<ModItem>,
28{
29 fn from(t: T) -> Self {
30 ModItems(SmallVec::from_buf([t.into(); 1]))
31 }
32}
33
34pub(super) struct Ctx<'a> { 22pub(super) struct Ctx<'a> {
35 db: &'a dyn DefDatabase, 23 db: &'a dyn DefDatabase,
36 tree: ItemTree, 24 tree: ItemTree,
@@ -53,11 +41,8 @@ impl<'a> Ctx<'a> {
53 } 41 }
54 42
55 pub(super) fn lower_module_items(mut self, item_owner: &dyn ModuleItemOwner) -> ItemTree { 43 pub(super) fn lower_module_items(mut self, item_owner: &dyn ModuleItemOwner) -> ItemTree {
56 self.tree.top_level = item_owner 44 self.tree.top_level =
57 .items() 45 item_owner.items().flat_map(|item| self.lower_mod_item(&item, false)).collect();
58 .flat_map(|item| self.lower_mod_item(&item, false))
59 .flat_map(|items| items.0)
60 .collect();
61 self.tree 46 self.tree
62 } 47 }
63 48
@@ -69,7 +54,6 @@ impl<'a> Ctx<'a> {
69 _ => None, 54 _ => None,
70 }) 55 })
71 .flat_map(|item| self.lower_mod_item(&item, false)) 56 .flat_map(|item| self.lower_mod_item(&item, false))
72 .flat_map(|items| items.0)
73 .collect(); 57 .collect();
74 58
75 // Non-items need to have their inner items collected. 59 // Non-items need to have their inner items collected.
@@ -96,7 +80,7 @@ impl<'a> Ctx<'a> {
96 self.tree.data_mut() 80 self.tree.data_mut()
97 } 81 }
98 82
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> {
100 // Collect inner items for 1-to-1-lowered items. 84 // Collect inner items for 1-to-1-lowered items.
101 match item { 85 match item {
102 ast::Item::Struct(_) 86 ast::Item::Struct(_)
@@ -127,34 +111,28 @@ impl<'a> Ctx<'a> {
127 }; 111 };
128 112
129 let attrs = RawAttrs::new(self.db, item, &self.hygiene); 113 let attrs = RawAttrs::new(self.db, item, &self.hygiene);
130 let items = match item { 114 let item: ModItem = match item {
131 ast::Item::Struct(ast) => self.lower_struct(ast).map(Into::into), 115 ast::Item::Struct(ast) => self.lower_struct(ast)?.into(),
132 ast::Item::Union(ast) => self.lower_union(ast).map(Into::into), 116 ast::Item::Union(ast) => self.lower_union(ast)?.into(),
133 ast::Item::Enum(ast) => self.lower_enum(ast).map(Into::into), 117 ast::Item::Enum(ast) => self.lower_enum(ast)?.into(),
134 ast::Item::Fn(ast) => self.lower_function(ast).map(Into::into), 118 ast::Item::Fn(ast) => self.lower_function(ast)?.into(),
135 ast::Item::TypeAlias(ast) => self.lower_type_alias(ast).map(Into::into), 119 ast::Item::TypeAlias(ast) => self.lower_type_alias(ast)?.into(),
136 ast::Item::Static(ast) => self.lower_static(ast).map(Into::into), 120 ast::Item::Static(ast) => self.lower_static(ast)?.into(),
137 ast::Item::Const(ast) => Some(self.lower_const(ast).into()), 121 ast::Item::Const(ast) => self.lower_const(ast).into(),
138 ast::Item::Module(ast) => self.lower_module(ast).map(Into::into), 122 ast::Item::Module(ast) => self.lower_module(ast)?.into(),
139 ast::Item::Trait(ast) => self.lower_trait(ast).map(Into::into), 123 ast::Item::Trait(ast) => self.lower_trait(ast)?.into(),
140 ast::Item::Impl(ast) => self.lower_impl(ast).map(Into::into), 124 ast::Item::Impl(ast) => self.lower_impl(ast)?.into(),
141 ast::Item::Use(ast) => Some(ModItems( 125 ast::Item::Use(ast) => self.lower_use(ast)?.into(),
142 self.lower_use(ast).into_iter().map(Into::into).collect::<SmallVec<_>>(), 126 ast::Item::ExternCrate(ast) => self.lower_extern_crate(ast)?.into(),
143 )), 127 ast::Item::MacroCall(ast) => self.lower_macro_call(ast)?.into(),
144 ast::Item::ExternCrate(ast) => self.lower_extern_crate(ast).map(Into::into), 128 ast::Item::MacroRules(ast) => self.lower_macro_rules(ast)?.into(),
145 ast::Item::MacroCall(ast) => self.lower_macro_call(ast).map(Into::into), 129 ast::Item::MacroDef(ast) => self.lower_macro_def(ast)?.into(),
146 ast::Item::MacroRules(ast) => self.lower_macro_rules(ast).map(Into::into), 130 ast::Item::ExternBlock(ast) => self.lower_extern_block(ast).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()),
149 }; 131 };
150 132
151 if !attrs.is_empty() { 133 self.add_attrs(item.into(), attrs.clone());
152 for item in items.iter().flat_map(|items| &items.0) {
153 self.add_attrs((*item).into(), attrs.clone());
154 }
155 }
156 134
157 items 135 Some(item)
158 } 136 }
159 137
160 fn add_attrs(&mut self, item: AttrOwner, attrs: RawAttrs) { 138 fn add_attrs(&mut self, item: AttrOwner, attrs: RawAttrs) {
@@ -188,12 +166,10 @@ impl<'a> Ctx<'a> {
188 }, 166 },
189 ast::Item(item) => { 167 ast::Item(item) => {
190 // FIXME: This triggers for macro calls in expression/pattern/type position 168 // 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);
192 let current_block = block_stack.last(); 170 let current_block = block_stack.last();
193 if let (Some(mod_items), Some(block)) = (mod_items, current_block) { 171 if let (Some(mod_item), Some(block)) = (mod_item, current_block) {
194 if !mod_items.0.is_empty() { 172 self.data().inner_items.entry(*block).or_default().push(mod_item);
195 self.data().inner_items.entry(*block).or_default().extend(mod_items.0.iter().copied());
196 }
197 } 173 }
198 }, 174 },
199 _ => {} 175 _ => {}
@@ -478,10 +454,7 @@ impl<'a> Ctx<'a> {
478 items: module 454 items: module
479 .item_list() 455 .item_list()
480 .map(|list| { 456 .map(|list| {
481 list.items() 457 list.items().flat_map(|item| self.lower_mod_item(&item, false)).collect()
482 .flat_map(|item| self.lower_mod_item(&item, false))
483 .flat_map(|items| items.0)
484 .collect()
485 }) 458 })
486 .unwrap_or_else(|| { 459 .unwrap_or_else(|| {
487 cov_mark::hit!(name_res_works_for_broken_modules); 460 cov_mark::hit!(name_res_works_for_broken_modules);