diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-26 16:16:54 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-26 16:16:54 +0000 |
commit | a82755e24102bea85be450e0b210d45b05b9f246 (patch) | |
tree | 52f347ddfc4586e3dfa4467827b211d1684aaa74 /crates/ra_hir/src/nameres/raw.rs | |
parent | 96acf4f7ec4257af0d5fd0415b31ab6757b475ad (diff) | |
parent | 1325a31e34a3b4bf5104a743bcb8217ef5c4f3cd (diff) |
Merge #1059
1059: Typed ids r=matklad a=matklad
just some type-safety and refactorings.
closes https://github.com/rust-analyzer/rust-analyzer/issues/1054
bors r+
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/nameres/raw.rs')
-rw-r--r-- | crates/ra_hir/src/nameres/raw.rs | 69 |
1 files changed, 40 insertions, 29 deletions
diff --git a/crates/ra_hir/src/nameres/raw.rs b/crates/ra_hir/src/nameres/raw.rs index f32004601..0936229ac 100644 --- a/crates/ra_hir/src/nameres/raw.rs +++ b/crates/ra_hir/src/nameres/raw.rs | |||
@@ -12,7 +12,7 @@ use ra_syntax::{ | |||
12 | 12 | ||
13 | use crate::{ | 13 | use crate::{ |
14 | DefDatabase, Name, AsName, Path, HirFileId, ModuleSource, | 14 | DefDatabase, Name, AsName, Path, HirFileId, ModuleSource, |
15 | SourceFileItemId, SourceFileItems, | 15 | AstIdMap, FileAstId, |
16 | }; | 16 | }; |
17 | 17 | ||
18 | /// `RawItems` is a set of top-level items in a file (except for impls). | 18 | /// `RawItems` is a set of top-level items in a file (except for impls). |
@@ -60,7 +60,7 @@ impl RawItems { | |||
60 | ) -> (Arc<RawItems>, Arc<ImportSourceMap>) { | 60 | ) -> (Arc<RawItems>, Arc<ImportSourceMap>) { |
61 | let mut collector = RawItemsCollector { | 61 | let mut collector = RawItemsCollector { |
62 | raw_items: RawItems::default(), | 62 | raw_items: RawItems::default(), |
63 | source_file_items: db.file_items(file_id.into()), | 63 | source_ast_id_map: db.ast_id_map(file_id.into()), |
64 | source_map: ImportSourceMap::default(), | 64 | source_map: ImportSourceMap::default(), |
65 | }; | 65 | }; |
66 | let source_file = db.hir_parse(file_id); | 66 | let source_file = db.hir_parse(file_id); |
@@ -115,8 +115,8 @@ impl_arena_id!(Module); | |||
115 | 115 | ||
116 | #[derive(Debug, PartialEq, Eq)] | 116 | #[derive(Debug, PartialEq, Eq)] |
117 | pub(super) enum ModuleData { | 117 | pub(super) enum ModuleData { |
118 | Declaration { name: Name, source_item_id: SourceFileItemId }, | 118 | Declaration { name: Name, ast_id: FileAstId<ast::Module> }, |
119 | Definition { name: Name, source_item_id: SourceFileItemId, items: Vec<RawItem> }, | 119 | Definition { name: Name, ast_id: FileAstId<ast::Module>, items: Vec<RawItem> }, |
120 | } | 120 | } |
121 | 121 | ||
122 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 122 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
@@ -138,20 +138,19 @@ impl_arena_id!(Def); | |||
138 | 138 | ||
139 | #[derive(Debug, PartialEq, Eq)] | 139 | #[derive(Debug, PartialEq, Eq)] |
140 | pub(super) struct DefData { | 140 | pub(super) struct DefData { |
141 | pub(super) source_item_id: SourceFileItemId, | ||
142 | pub(super) name: Name, | 141 | pub(super) name: Name, |
143 | pub(super) kind: DefKind, | 142 | pub(super) kind: DefKind, |
144 | } | 143 | } |
145 | 144 | ||
146 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | 145 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] |
147 | pub(super) enum DefKind { | 146 | pub(super) enum DefKind { |
148 | Function, | 147 | Function(FileAstId<ast::FnDef>), |
149 | Struct, | 148 | Struct(FileAstId<ast::StructDef>), |
150 | Enum, | 149 | Enum(FileAstId<ast::EnumDef>), |
151 | Const, | 150 | Const(FileAstId<ast::ConstDef>), |
152 | Static, | 151 | Static(FileAstId<ast::StaticDef>), |
153 | Trait, | 152 | Trait(FileAstId<ast::TraitDef>), |
154 | TypeAlias, | 153 | TypeAlias(FileAstId<ast::TypeAliasDef>), |
155 | } | 154 | } |
156 | 155 | ||
157 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 156 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
@@ -160,7 +159,7 @@ impl_arena_id!(Macro); | |||
160 | 159 | ||
161 | #[derive(Debug, PartialEq, Eq)] | 160 | #[derive(Debug, PartialEq, Eq)] |
162 | pub(super) struct MacroData { | 161 | pub(super) struct MacroData { |
163 | pub(super) source_item_id: SourceFileItemId, | 162 | pub(super) ast_id: FileAstId<ast::MacroCall>, |
164 | pub(super) path: Path, | 163 | pub(super) path: Path, |
165 | pub(super) name: Option<Name>, | 164 | pub(super) name: Option<Name>, |
166 | pub(super) export: bool, | 165 | pub(super) export: bool, |
@@ -168,7 +167,7 @@ pub(super) struct MacroData { | |||
168 | 167 | ||
169 | struct RawItemsCollector { | 168 | struct RawItemsCollector { |
170 | raw_items: RawItems, | 169 | raw_items: RawItems, |
171 | source_file_items: Arc<SourceFileItems>, | 170 | source_ast_id_map: Arc<AstIdMap>, |
172 | source_map: ImportSourceMap, | 171 | source_map: ImportSourceMap, |
173 | } | 172 | } |
174 | 173 | ||
@@ -200,18 +199,31 @@ impl RawItemsCollector { | |||
200 | // impls don't participate in name resolution | 199 | // impls don't participate in name resolution |
201 | return; | 200 | return; |
202 | } | 201 | } |
203 | ast::ModuleItemKind::StructDef(it) => (DefKind::Struct, it.name()), | 202 | ast::ModuleItemKind::StructDef(it) => { |
204 | ast::ModuleItemKind::EnumDef(it) => (DefKind::Enum, it.name()), | 203 | (DefKind::Struct(self.source_ast_id_map.ast_id(it)), it.name()) |
205 | ast::ModuleItemKind::FnDef(it) => (DefKind::Function, it.name()), | 204 | } |
206 | ast::ModuleItemKind::TraitDef(it) => (DefKind::Trait, it.name()), | 205 | ast::ModuleItemKind::EnumDef(it) => { |
207 | ast::ModuleItemKind::TypeAliasDef(it) => (DefKind::TypeAlias, it.name()), | 206 | (DefKind::Enum(self.source_ast_id_map.ast_id(it)), it.name()) |
208 | ast::ModuleItemKind::ConstDef(it) => (DefKind::Const, it.name()), | 207 | } |
209 | ast::ModuleItemKind::StaticDef(it) => (DefKind::Static, it.name()), | 208 | ast::ModuleItemKind::FnDef(it) => { |
209 | (DefKind::Function(self.source_ast_id_map.ast_id(it)), it.name()) | ||
210 | } | ||
211 | ast::ModuleItemKind::TraitDef(it) => { | ||
212 | (DefKind::Trait(self.source_ast_id_map.ast_id(it)), it.name()) | ||
213 | } | ||
214 | ast::ModuleItemKind::TypeAliasDef(it) => { | ||
215 | (DefKind::TypeAlias(self.source_ast_id_map.ast_id(it)), it.name()) | ||
216 | } | ||
217 | ast::ModuleItemKind::ConstDef(it) => { | ||
218 | (DefKind::Const(self.source_ast_id_map.ast_id(it)), it.name()) | ||
219 | } | ||
220 | ast::ModuleItemKind::StaticDef(it) => { | ||
221 | (DefKind::Static(self.source_ast_id_map.ast_id(it)), it.name()) | ||
222 | } | ||
210 | }; | 223 | }; |
211 | if let Some(name) = name { | 224 | if let Some(name) = name { |
212 | let name = name.as_name(); | 225 | let name = name.as_name(); |
213 | let source_item_id = self.source_file_items.id_of_unchecked(item.syntax()); | 226 | let def = self.raw_items.defs.alloc(DefData { name, kind }); |
214 | let def = self.raw_items.defs.alloc(DefData { name, kind, source_item_id }); | ||
215 | self.push_item(current_module, RawItem::Def(def)) | 227 | self.push_item(current_module, RawItem::Def(def)) |
216 | } | 228 | } |
217 | } | 229 | } |
@@ -221,10 +233,9 @@ impl RawItemsCollector { | |||
221 | Some(it) => it.as_name(), | 233 | Some(it) => it.as_name(), |
222 | None => return, | 234 | None => return, |
223 | }; | 235 | }; |
224 | let source_item_id = self.source_file_items.id_of_unchecked(module.syntax()); | 236 | let ast_id = self.source_ast_id_map.ast_id(module); |
225 | if module.has_semi() { | 237 | if module.has_semi() { |
226 | let item = | 238 | let item = self.raw_items.modules.alloc(ModuleData::Declaration { name, ast_id }); |
227 | self.raw_items.modules.alloc(ModuleData::Declaration { name, source_item_id }); | ||
228 | self.push_item(current_module, RawItem::Module(item)); | 239 | self.push_item(current_module, RawItem::Module(item)); |
229 | return; | 240 | return; |
230 | } | 241 | } |
@@ -232,7 +243,7 @@ impl RawItemsCollector { | |||
232 | if let Some(item_list) = module.item_list() { | 243 | if let Some(item_list) = module.item_list() { |
233 | let item = self.raw_items.modules.alloc(ModuleData::Definition { | 244 | let item = self.raw_items.modules.alloc(ModuleData::Definition { |
234 | name, | 245 | name, |
235 | source_item_id, | 246 | ast_id, |
236 | items: Vec::new(), | 247 | items: Vec::new(), |
237 | }); | 248 | }); |
238 | self.process_module(Some(item), item_list); | 249 | self.process_module(Some(item), item_list); |
@@ -286,9 +297,9 @@ impl RawItemsCollector { | |||
286 | }; | 297 | }; |
287 | 298 | ||
288 | let name = m.name().map(|it| it.as_name()); | 299 | let name = m.name().map(|it| it.as_name()); |
289 | let source_item_id = self.source_file_items.id_of_unchecked(m.syntax()); | 300 | let ast_id = self.source_ast_id_map.ast_id(m); |
290 | let export = m.has_atom_attr("macro_export"); | 301 | let export = m.has_atom_attr("macro_export"); |
291 | let m = self.raw_items.macros.alloc(MacroData { source_item_id, path, name, export }); | 302 | let m = self.raw_items.macros.alloc(MacroData { ast_id, path, name, export }); |
292 | self.push_item(current_module, RawItem::Macro(m)); | 303 | self.push_item(current_module, RawItem::Macro(m)); |
293 | } | 304 | } |
294 | 305 | ||