diff options
author | Aleksey Kladov <[email protected]> | 2019-01-23 20:14:13 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-01-24 10:29:19 +0000 |
commit | 3ab1519cb27b927074ed7fbbb18a856e6e7fabb8 (patch) | |
tree | 692c7a256604e188d38890966290bd1637d7dd60 /crates/ra_hir/src/nameres | |
parent | cfb085ded8d61d7b744d0a83ecbb3da254f6ab9f (diff) |
Change ids strategy
this is a part of larghish hir refactoring which aims to
* replace per-source-root module trees with per crate trees
* switch from a monotyped DedId to type-specific ids
Diffstat (limited to 'crates/ra_hir/src/nameres')
-rw-r--r-- | crates/ra_hir/src/nameres/lower.rs | 74 | ||||
-rw-r--r-- | crates/ra_hir/src/nameres/tests.rs | 35 |
2 files changed, 39 insertions, 70 deletions
diff --git a/crates/ra_hir/src/nameres/lower.rs b/crates/ra_hir/src/nameres/lower.rs index 4eea6ff1d..6f003bd66 100644 --- a/crates/ra_hir/src/nameres/lower.rs +++ b/crates/ra_hir/src/nameres/lower.rs | |||
@@ -4,14 +4,13 @@ use ra_syntax::{ | |||
4 | SyntaxKind, AstNode, SourceFile, TreeArc, AstPtr, | 4 | SyntaxKind, AstNode, SourceFile, TreeArc, AstPtr, |
5 | ast::{self, ModuleItemOwner, NameOwner}, | 5 | ast::{self, ModuleItemOwner, NameOwner}, |
6 | }; | 6 | }; |
7 | use ra_db::SourceRootId; | ||
8 | use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; | 7 | use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; |
9 | use rustc_hash::FxHashMap; | 8 | use rustc_hash::FxHashMap; |
10 | 9 | ||
11 | use crate::{ | 10 | use crate::{ |
12 | SourceItemId, Path, ModuleSource, HirDatabase, Name, SourceFileItems, | 11 | SourceItemId, Path, ModuleSource, HirDatabase, Name, SourceFileItems, |
13 | HirFileId, MacroCallLoc, AsName, PerNs, DefId, DefKind, DefLoc, | 12 | HirFileId, MacroCallLoc, AsName, PerNs, DefKind, DefLoc, |
14 | module_tree::ModuleId | 13 | ModuleDef, Module, |
15 | }; | 14 | }; |
16 | 15 | ||
17 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 16 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
@@ -32,7 +31,7 @@ pub(super) struct ImportData { | |||
32 | /// can avoid redoing name resolution. | 31 | /// can avoid redoing name resolution. |
33 | #[derive(Debug, Default, PartialEq, Eq)] | 32 | #[derive(Debug, Default, PartialEq, Eq)] |
34 | pub struct LoweredModule { | 33 | pub struct LoweredModule { |
35 | pub(crate) declarations: FxHashMap<Name, PerNs<DefId>>, | 34 | pub(crate) declarations: FxHashMap<Name, PerNs<ModuleDef>>, |
36 | pub(super) imports: Arena<ImportId, ImportData>, | 35 | pub(super) imports: Arena<ImportId, ImportData>, |
37 | } | 36 | } |
38 | 37 | ||
@@ -59,37 +58,31 @@ impl ImportSourceMap { | |||
59 | impl LoweredModule { | 58 | impl LoweredModule { |
60 | pub(crate) fn lower_module_module_query( | 59 | pub(crate) fn lower_module_module_query( |
61 | db: &impl HirDatabase, | 60 | db: &impl HirDatabase, |
62 | source_root_id: SourceRootId, | 61 | module: Module, |
63 | module_id: ModuleId, | ||
64 | ) -> Arc<LoweredModule> { | 62 | ) -> Arc<LoweredModule> { |
65 | db.lower_module(source_root_id, module_id).0 | 63 | db.lower_module(module).0 |
66 | } | 64 | } |
67 | 65 | ||
68 | pub(crate) fn lower_module_source_map_query( | 66 | pub(crate) fn lower_module_source_map_query( |
69 | db: &impl HirDatabase, | 67 | db: &impl HirDatabase, |
70 | source_root_id: SourceRootId, | 68 | module: Module, |
71 | module_id: ModuleId, | ||
72 | ) -> Arc<ImportSourceMap> { | 69 | ) -> Arc<ImportSourceMap> { |
73 | db.lower_module(source_root_id, module_id).1 | 70 | db.lower_module(module).1 |
74 | } | 71 | } |
75 | 72 | ||
76 | pub(crate) fn lower_module_query( | 73 | pub(crate) fn lower_module_query( |
77 | db: &impl HirDatabase, | 74 | db: &impl HirDatabase, |
78 | source_root_id: SourceRootId, | 75 | module: Module, |
79 | module_id: ModuleId, | ||
80 | ) -> (Arc<LoweredModule>, Arc<ImportSourceMap>) { | 76 | ) -> (Arc<LoweredModule>, Arc<ImportSourceMap>) { |
81 | let module_tree = db.module_tree(source_root_id); | 77 | let (file_id, source) = module.definition_source(db); |
82 | let source = module_id.source(&module_tree); | 78 | let file_id: HirFileId = file_id.into(); |
83 | let file_id = source.file_id; | ||
84 | let source = ModuleSource::from_source_item_id(db, source); | ||
85 | let mut source_map = ImportSourceMap::default(); | 79 | let mut source_map = ImportSourceMap::default(); |
86 | let mut res = LoweredModule::default(); | 80 | let mut res = LoweredModule::default(); |
87 | match source { | 81 | match source { |
88 | ModuleSource::SourceFile(it) => res.fill( | 82 | ModuleSource::SourceFile(it) => res.fill( |
89 | &mut source_map, | 83 | &mut source_map, |
90 | db, | 84 | db, |
91 | source_root_id, | 85 | module, |
92 | module_id, | ||
93 | file_id, | 86 | file_id, |
94 | &mut it.items_with_macros(), | 87 | &mut it.items_with_macros(), |
95 | ), | 88 | ), |
@@ -98,8 +91,7 @@ impl LoweredModule { | |||
98 | res.fill( | 91 | res.fill( |
99 | &mut source_map, | 92 | &mut source_map, |
100 | db, | 93 | db, |
101 | source_root_id, | 94 | module, |
102 | module_id, | ||
103 | file_id, | 95 | file_id, |
104 | &mut item_list.items_with_macros(), | 96 | &mut item_list.items_with_macros(), |
105 | ) | 97 | ) |
@@ -113,8 +105,7 @@ impl LoweredModule { | |||
113 | &mut self, | 105 | &mut self, |
114 | source_map: &mut ImportSourceMap, | 106 | source_map: &mut ImportSourceMap, |
115 | db: &impl HirDatabase, | 107 | db: &impl HirDatabase, |
116 | source_root_id: SourceRootId, | 108 | module: Module, |
117 | module_id: ModuleId, | ||
118 | file_id: HirFileId, | 109 | file_id: HirFileId, |
119 | items: &mut Iterator<Item = ast::ItemOrMacro>, | 110 | items: &mut Iterator<Item = ast::ItemOrMacro>, |
120 | ) { | 111 | ) { |
@@ -123,21 +114,12 @@ impl LoweredModule { | |||
123 | for item in items { | 114 | for item in items { |
124 | match item { | 115 | match item { |
125 | ast::ItemOrMacro::Item(it) => { | 116 | ast::ItemOrMacro::Item(it) => { |
126 | self.add_def_id( | 117 | self.add_def_id(source_map, db, module, file_id, &file_items, it); |
127 | source_map, | ||
128 | db, | ||
129 | source_root_id, | ||
130 | module_id, | ||
131 | file_id, | ||
132 | &file_items, | ||
133 | it, | ||
134 | ); | ||
135 | } | 118 | } |
136 | ast::ItemOrMacro::Macro(macro_call) => { | 119 | ast::ItemOrMacro::Macro(macro_call) => { |
137 | let item_id = file_items.id_of_unchecked(macro_call.syntax()); | 120 | let item_id = file_items.id_of_unchecked(macro_call.syntax()); |
138 | let loc = MacroCallLoc { | 121 | let loc = MacroCallLoc { |
139 | source_root_id, | 122 | module, |
140 | module_id, | ||
141 | source_item_id: SourceItemId { | 123 | source_item_id: SourceItemId { |
142 | file_id, | 124 | file_id, |
143 | item_id: Some(item_id), | 125 | item_id: Some(item_id), |
@@ -148,15 +130,7 @@ impl LoweredModule { | |||
148 | let file_items = db.file_items(file_id); | 130 | let file_items = db.file_items(file_id); |
149 | //FIXME: expand recursively | 131 | //FIXME: expand recursively |
150 | for item in db.hir_source_file(file_id).items() { | 132 | for item in db.hir_source_file(file_id).items() { |
151 | self.add_def_id( | 133 | self.add_def_id(source_map, db, module, file_id, &file_items, item); |
152 | source_map, | ||
153 | db, | ||
154 | source_root_id, | ||
155 | module_id, | ||
156 | file_id, | ||
157 | &file_items, | ||
158 | item, | ||
159 | ); | ||
160 | } | 134 | } |
161 | } | 135 | } |
162 | } | 136 | } |
@@ -167,8 +141,7 @@ impl LoweredModule { | |||
167 | &mut self, | 141 | &mut self, |
168 | source_map: &mut ImportSourceMap, | 142 | source_map: &mut ImportSourceMap, |
169 | db: &impl HirDatabase, | 143 | db: &impl HirDatabase, |
170 | source_root_id: SourceRootId, | 144 | module: Module, |
171 | module_id: ModuleId, | ||
172 | file_id: HirFileId, | 145 | file_id: HirFileId, |
173 | file_items: &SourceFileItems, | 146 | file_items: &SourceFileItems, |
174 | item: &ast::ModuleItem, | 147 | item: &ast::ModuleItem, |
@@ -199,7 +172,7 @@ impl LoweredModule { | |||
199 | } | 172 | } |
200 | }; | 173 | }; |
201 | if let Some(name) = name { | 174 | if let Some(name) = name { |
202 | let def_id = assign_def_id(db, source_root_id, module_id, file_id, file_items, item); | 175 | let def_id = assign_def_id(db, module, file_id, file_items, item); |
203 | self.declarations.insert(name.as_name(), def_id); | 176 | self.declarations.insert(name.as_name(), def_id); |
204 | } | 177 | } |
205 | } | 178 | } |
@@ -219,12 +192,11 @@ impl LoweredModule { | |||
219 | 192 | ||
220 | fn assign_def_id( | 193 | fn assign_def_id( |
221 | db: &impl HirDatabase, | 194 | db: &impl HirDatabase, |
222 | source_root_id: SourceRootId, | 195 | module: Module, |
223 | module_id: ModuleId, | ||
224 | file_id: HirFileId, | 196 | file_id: HirFileId, |
225 | file_items: &SourceFileItems, | 197 | file_items: &SourceFileItems, |
226 | item: &ast::ModuleItem, | 198 | item: &ast::ModuleItem, |
227 | ) -> PerNs<DefId> { | 199 | ) -> PerNs<ModuleDef> { |
228 | // depending on the item kind, the location can define something in | 200 | // depending on the item kind, the location can define something in |
229 | // the values namespace, the types namespace, or both | 201 | // the values namespace, the types namespace, or both |
230 | let kind = DefKind::for_syntax_kind(item.syntax().kind()); | 202 | let kind = DefKind::for_syntax_kind(item.syntax().kind()); |
@@ -232,14 +204,13 @@ fn assign_def_id( | |||
232 | let item_id = file_items.id_of_unchecked(item.syntax()); | 204 | let item_id = file_items.id_of_unchecked(item.syntax()); |
233 | let def_loc = DefLoc { | 205 | let def_loc = DefLoc { |
234 | kind: k, | 206 | kind: k, |
235 | source_root_id, | 207 | module, |
236 | module_id, | ||
237 | source_item_id: SourceItemId { | 208 | source_item_id: SourceItemId { |
238 | file_id, | 209 | file_id, |
239 | item_id: Some(item_id), | 210 | item_id: Some(item_id), |
240 | }, | 211 | }, |
241 | }; | 212 | }; |
242 | def_loc.id(db) | 213 | def_loc.id(db).into() |
243 | }); | 214 | }); |
244 | def_id | 215 | def_id |
245 | } | 216 | } |
@@ -248,7 +219,6 @@ impl DefKind { | |||
248 | fn for_syntax_kind(kind: SyntaxKind) -> PerNs<DefKind> { | 219 | fn for_syntax_kind(kind: SyntaxKind) -> PerNs<DefKind> { |
249 | match kind { | 220 | match kind { |
250 | SyntaxKind::FN_DEF => PerNs::values(DefKind::Function), | 221 | SyntaxKind::FN_DEF => PerNs::values(DefKind::Function), |
251 | SyntaxKind::MODULE => PerNs::types(DefKind::Module), | ||
252 | SyntaxKind::STRUCT_DEF => PerNs::both(DefKind::Struct, DefKind::StructCtor), | 222 | SyntaxKind::STRUCT_DEF => PerNs::both(DefKind::Struct, DefKind::StructCtor), |
253 | SyntaxKind::ENUM_DEF => PerNs::types(DefKind::Enum), | 223 | SyntaxKind::ENUM_DEF => PerNs::types(DefKind::Enum), |
254 | SyntaxKind::TRAIT_DEF => PerNs::types(DefKind::Trait), | 224 | SyntaxKind::TRAIT_DEF => PerNs::types(DefKind::Trait), |
diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index e92007453..9322bf08c 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use std::sync::Arc; | 1 | use std::sync::Arc; |
2 | 2 | ||
3 | use ra_db::{FilesDatabase, CrateGraph, SourceRootId, salsa::Database}; | 3 | use ra_db::{CrateGraph, SourceRootId, salsa::Database}; |
4 | use relative_path::RelativePath; | 4 | use relative_path::RelativePath; |
5 | use test_utils::{assert_eq_text, covers}; | 5 | use test_utils::{assert_eq_text, covers}; |
6 | 6 | ||
@@ -13,10 +13,10 @@ use crate::{ | |||
13 | 13 | ||
14 | fn item_map(fixture: &str) -> (Arc<ItemMap>, ModuleId) { | 14 | fn item_map(fixture: &str) -> (Arc<ItemMap>, ModuleId) { |
15 | let (db, pos) = MockDatabase::with_position(fixture); | 15 | let (db, pos) = MockDatabase::with_position(fixture); |
16 | let source_root = db.file_source_root(pos.file_id); | ||
17 | let module = crate::source_binder::module_from_position(&db, pos).unwrap(); | 16 | let module = crate::source_binder::module_from_position(&db, pos).unwrap(); |
18 | let module_id = module.def_id.loc(&db).module_id; | 17 | let krate = module.krate(&db).unwrap(); |
19 | (db.item_map(source_root), module_id) | 18 | let module_id = module.module_id; |
19 | (db.item_map(krate.crate_id), module_id) | ||
20 | } | 20 | } |
21 | 21 | ||
22 | fn check_module_item_map(map: &ItemMap, module_id: ModuleId, expected: &str) { | 22 | fn check_module_item_map(map: &ItemMap, module_id: ModuleId, expected: &str) { |
@@ -238,14 +238,13 @@ fn item_map_across_crates() { | |||
238 | 238 | ||
239 | db.set_crate_graph(crate_graph); | 239 | db.set_crate_graph(crate_graph); |
240 | 240 | ||
241 | let source_root = db.file_source_root(main_id); | ||
242 | let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); | 241 | let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); |
243 | let module_id = module.def_id.loc(&db).module_id; | 242 | let krate = module.krate(&db).unwrap(); |
244 | let item_map = db.item_map(source_root); | 243 | let item_map = db.item_map(krate.crate_id); |
245 | 244 | ||
246 | check_module_item_map( | 245 | check_module_item_map( |
247 | &item_map, | 246 | &item_map, |
248 | module_id, | 247 | module.module_id, |
249 | " | 248 | " |
250 | Baz: t v | 249 | Baz: t v |
251 | test_crate: t | 250 | test_crate: t |
@@ -292,12 +291,12 @@ fn import_across_source_roots() { | |||
292 | db.set_crate_graph(crate_graph); | 291 | db.set_crate_graph(crate_graph); |
293 | 292 | ||
294 | let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); | 293 | let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); |
295 | let module_id = module.def_id.loc(&db).module_id; | 294 | let krate = module.krate(&db).unwrap(); |
296 | let item_map = db.item_map(source_root); | 295 | let item_map = db.item_map(krate.crate_id); |
297 | 296 | ||
298 | check_module_item_map( | 297 | check_module_item_map( |
299 | &item_map, | 298 | &item_map, |
300 | module_id, | 299 | module.module_id, |
301 | " | 300 | " |
302 | C: t v | 301 | C: t v |
303 | test_crate: t | 302 | test_crate: t |
@@ -333,14 +332,13 @@ fn reexport_across_crates() { | |||
333 | 332 | ||
334 | db.set_crate_graph(crate_graph); | 333 | db.set_crate_graph(crate_graph); |
335 | 334 | ||
336 | let source_root = db.file_source_root(main_id); | ||
337 | let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); | 335 | let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); |
338 | let module_id = module.def_id.loc(&db).module_id; | 336 | let krate = module.krate(&db).unwrap(); |
339 | let item_map = db.item_map(source_root); | 337 | let item_map = db.item_map(krate.crate_id); |
340 | 338 | ||
341 | check_module_item_map( | 339 | check_module_item_map( |
342 | &item_map, | 340 | &item_map, |
343 | module_id, | 341 | module.module_id, |
344 | " | 342 | " |
345 | Baz: t v | 343 | Baz: t v |
346 | test_crate: t | 344 | test_crate: t |
@@ -350,10 +348,11 @@ fn reexport_across_crates() { | |||
350 | 348 | ||
351 | fn check_item_map_is_not_recomputed(initial: &str, file_change: &str) { | 349 | fn check_item_map_is_not_recomputed(initial: &str, file_change: &str) { |
352 | let (mut db, pos) = MockDatabase::with_position(initial); | 350 | let (mut db, pos) = MockDatabase::with_position(initial); |
353 | let source_root = db.file_source_root(pos.file_id); | 351 | let module = crate::source_binder::module_from_file_id(&db, pos.file_id).unwrap(); |
352 | let krate = module.krate(&db).unwrap(); | ||
354 | { | 353 | { |
355 | let events = db.log_executed(|| { | 354 | let events = db.log_executed(|| { |
356 | db.item_map(source_root); | 355 | db.item_map(krate.crate_id); |
357 | }); | 356 | }); |
358 | assert!(format!("{:?}", events).contains("item_map")) | 357 | assert!(format!("{:?}", events).contains("item_map")) |
359 | } | 358 | } |
@@ -362,7 +361,7 @@ fn check_item_map_is_not_recomputed(initial: &str, file_change: &str) { | |||
362 | 361 | ||
363 | { | 362 | { |
364 | let events = db.log_executed(|| { | 363 | let events = db.log_executed(|| { |
365 | db.item_map(source_root); | 364 | db.item_map(krate.crate_id); |
366 | }); | 365 | }); |
367 | assert!( | 366 | assert!( |
368 | !format!("{:?}", events).contains("item_map"), | 367 | !format!("{:?}", events).contains("item_map"), |