aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/nameres
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/nameres')
-rw-r--r--crates/ra_hir/src/nameres/collector.rs46
-rw-r--r--crates/ra_hir/src/nameres/tests.rs13
2 files changed, 25 insertions, 34 deletions
diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs
index 99110d58d..b74dc33b1 100644
--- a/crates/ra_hir/src/nameres/collector.rs
+++ b/crates/ra_hir/src/nameres/collector.rs
@@ -1,6 +1,5 @@
1use arrayvec::ArrayVec; 1use arrayvec::ArrayVec;
2use rustc_hash::FxHashMap; 2use rustc_hash::FxHashMap;
3use either::Either;
4use relative_path::RelativePathBuf; 3use relative_path::RelativePathBuf;
5use test_utils::tested_by; 4use test_utils::tested_by;
6use ra_db::FileId; 5use ra_db::FileId;
@@ -9,7 +8,7 @@ use ra_syntax::ast;
9use crate::{ 8use crate::{
10 Function, Module, Struct, Union, Enum, Const, Static, Trait, TypeAlias, MacroDef, 9 Function, Module, Struct, Union, Enum, Const, Static, Trait, TypeAlias, MacroDef,
11 DefDatabase, HirFileId, Name, Path, AstDatabase, 10 DefDatabase, HirFileId, Name, Path, AstDatabase,
12 KnownName, 11 KnownName, AstId,
13 nameres::{ 12 nameres::{
14 Resolution, PerNs, ModuleDef, ReachedFixedPoint, ResolveMode, 13 Resolution, PerNs, ModuleDef, ReachedFixedPoint, ResolveMode,
15 CrateDefMap, CrateModuleId, ModuleData, ItemOrMacro, 14 CrateDefMap, CrateModuleId, ModuleData, ItemOrMacro,
@@ -17,7 +16,7 @@ use crate::{
17 raw, 16 raw,
18 }, 17 },
19 ids::{AstItemDef, LocationCtx, MacroCallLoc, MacroCallId, MacroDefId, MacroFileKind}, 18 ids::{AstItemDef, LocationCtx, MacroCallLoc, MacroCallId, MacroDefId, MacroFileKind},
20 AstId, 19 either::Either,
21}; 20};
22 21
23pub(super) fn collect_defs( 22pub(super) fn collect_defs(
@@ -129,12 +128,7 @@ where
129 let unresolved_imports = std::mem::replace(&mut self.unresolved_imports, Vec::new()); 128 let unresolved_imports = std::mem::replace(&mut self.unresolved_imports, Vec::new());
130 // show unresolved imports in completion, etc 129 // show unresolved imports in completion, etc
131 for (module_id, import, import_data) in unresolved_imports { 130 for (module_id, import, import_data) in unresolved_imports {
132 self.record_resolved_import( 131 self.record_resolved_import(module_id, Either::A(PerNs::none()), import, &import_data)
133 module_id,
134 Either::Left(PerNs::none()),
135 import,
136 &import_data,
137 )
138 } 132 }
139 } 133 }
140 134
@@ -159,7 +153,7 @@ where
159 // What we should do is that, in CrateDefMap, we should maintain a 153 // What we should do is that, in CrateDefMap, we should maintain a
160 // separate tower of macro scopes, with ids. Then, for each item in the 154 // separate tower of macro scopes, with ids. Then, for each item in the
161 // module, we need to store it's macro scope. 155 // module, we need to store it's macro scope.
162 let def = Either::Right(MacroDef { id: macro_id }); 156 let def = Either::B(MacroDef { id: macro_id });
163 157
164 // In Rust, `#[macro_export]` macros are unconditionally visible at the 158 // In Rust, `#[macro_export]` macros are unconditionally visible at the
165 // crate root, even if the parent modules is **not** visible. 159 // crate root, even if the parent modules is **not** visible.
@@ -203,7 +197,7 @@ where
203 .as_ident() 197 .as_ident()
204 .expect("extern crate should have been desugared to one-element path"), 198 .expect("extern crate should have been desugared to one-element path"),
205 ); 199 );
206 (Either::Left(res), ReachedFixedPoint::Yes) 200 (Either::A(res), ReachedFixedPoint::Yes)
207 } else { 201 } else {
208 let res = self.def_map.resolve_path_fp_with_macro( 202 let res = self.def_map.resolve_path_fp_with_macro(
209 self.db, 203 self.db,
@@ -225,7 +219,7 @@ where
225 ) { 219 ) {
226 if import.is_glob { 220 if import.is_glob {
227 log::debug!("glob import: {:?}", import); 221 log::debug!("glob import: {:?}", import);
228 match def.left().and_then(|item| item.take_types()) { 222 match def.a().and_then(|item| item.take_types()) {
229 Some(ModuleDef::Module(m)) => { 223 Some(ModuleDef::Module(m)) => {
230 if import.is_prelude { 224 if import.is_prelude {
231 tested_by!(std_prelude); 225 tested_by!(std_prelude);
@@ -238,11 +232,11 @@ where
238 let items = scope 232 let items = scope
239 .items 233 .items
240 .iter() 234 .iter()
241 .map(|(name, res)| (name.clone(), Either::Left(res.clone()))); 235 .map(|(name, res)| (name.clone(), Either::A(res.clone())));
242 let macros = scope 236 let macros = scope
243 .macros 237 .macros
244 .iter() 238 .iter()
245 .map(|(name, res)| (name.clone(), Either::Right(res.clone()))); 239 .map(|(name, res)| (name.clone(), Either::B(res.clone())));
246 240
247 let all = items.chain(macros).collect::<Vec<_>>(); 241 let all = items.chain(macros).collect::<Vec<_>>();
248 self.update(module_id, Some(import_id), &all); 242 self.update(module_id, Some(import_id), &all);
@@ -254,11 +248,11 @@ where
254 let items = scope 248 let items = scope
255 .items 249 .items
256 .iter() 250 .iter()
257 .map(|(name, res)| (name.clone(), Either::Left(res.clone()))); 251 .map(|(name, res)| (name.clone(), Either::A(res.clone())));
258 let macros = scope 252 let macros = scope
259 .macros 253 .macros
260 .iter() 254 .iter()
261 .map(|(name, res)| (name.clone(), Either::Right(res.clone()))); 255 .map(|(name, res)| (name.clone(), Either::B(res.clone())));
262 256
263 let all = items.chain(macros).collect::<Vec<_>>(); 257 let all = items.chain(macros).collect::<Vec<_>>();
264 258
@@ -282,7 +276,7 @@ where
282 import: Some(import_id), 276 import: Some(import_id),
283 }; 277 };
284 let name = variant.name(self.db)?; 278 let name = variant.name(self.db)?;
285 Some((name, Either::Left(res))) 279 Some((name, Either::A(res)))
286 }) 280 })
287 .collect::<Vec<_>>(); 281 .collect::<Vec<_>>();
288 self.update(module_id, Some(import_id), &resolutions); 282 self.update(module_id, Some(import_id), &resolutions);
@@ -302,16 +296,16 @@ where
302 296
303 // extern crates in the crate root are special-cased to insert entries into the extern prelude: rust-lang/rust#54658 297 // extern crates in the crate root are special-cased to insert entries into the extern prelude: rust-lang/rust#54658
304 if import.is_extern_crate && module_id == self.def_map.root { 298 if import.is_extern_crate && module_id == self.def_map.root {
305 if let Some(def) = def.left().and_then(|item| item.take_types()) { 299 if let Some(def) = def.a().and_then(|item| item.take_types()) {
306 self.def_map.extern_prelude.insert(name.clone(), def); 300 self.def_map.extern_prelude.insert(name.clone(), def);
307 } 301 }
308 } 302 }
309 303
310 let resolution = match def { 304 let resolution = match def {
311 Either::Left(item) => { 305 Either::A(item) => {
312 Either::Left(Resolution { def: item, import: Some(import_id) }) 306 Either::A(Resolution { def: item, import: Some(import_id) })
313 } 307 }
314 Either::Right(macro_) => Either::Right(macro_), 308 Either::B(macro_) => Either::B(macro_),
315 }; 309 };
316 310
317 self.update(module_id, Some(import_id), &[(name, resolution)]); 311 self.update(module_id, Some(import_id), &[(name, resolution)]);
@@ -346,7 +340,7 @@ where
346 for (name, res) in resolutions { 340 for (name, res) in resolutions {
347 match res { 341 match res {
348 // item 342 // item
349 Either::Left(res) => { 343 Either::A(res) => {
350 let existing = module_items.items.entry(name.clone()).or_default(); 344 let existing = module_items.items.entry(name.clone()).or_default();
351 345
352 if existing.def.types.is_none() && res.def.types.is_some() { 346 if existing.def.types.is_none() && res.def.types.is_some() {
@@ -369,7 +363,7 @@ where
369 } 363 }
370 } 364 }
371 // macro 365 // macro
372 Either::Right(res) => { 366 Either::B(res) => {
373 // Always shadowing 367 // Always shadowing
374 module_items.macros.insert(name.clone(), *res); 368 module_items.macros.insert(name.clone(), *res);
375 } 369 }
@@ -404,7 +398,7 @@ where
404 path, 398 path,
405 ); 399 );
406 400
407 if let Some(def) = resolved_res.resolved_def.right() { 401 if let Some(def) = resolved_res.resolved_def.b() {
408 let call_id = MacroCallLoc { def: def.id, ast_id: *ast_id }.id(self.db); 402 let call_id = MacroCallLoc { def: def.id, ast_id: *ast_id }.id(self.db);
409 resolved.push((*module_id, call_id, def.id)); 403 resolved.push((*module_id, call_id, def.id));
410 res = ReachedFixedPoint::No; 404 res = ReachedFixedPoint::No;
@@ -570,7 +564,7 @@ where
570 ), 564 ),
571 import: None, 565 import: None,
572 }; 566 };
573 self.def_collector.update(self.module_id, None, &[(name, Either::Left(resolution))]); 567 self.def_collector.update(self.module_id, None, &[(name, Either::A(resolution))]);
574 res 568 res
575 } 569 }
576 570
@@ -601,7 +595,7 @@ where
601 raw::DefKind::TypeAlias(ast_id) => PerNs::types(def!(TypeAlias, ast_id)), 595 raw::DefKind::TypeAlias(ast_id) => PerNs::types(def!(TypeAlias, ast_id)),
602 }; 596 };
603 let resolution = Resolution { def, import: None }; 597 let resolution = Resolution { def, import: None };
604 self.def_collector.update(self.module_id, None, &[(name, Either::Left(resolution))]) 598 self.def_collector.update(self.module_id, None, &[(name, Either::A(resolution))])
605 } 599 }
606 600
607 fn collect_macro(&mut self, mac: &raw::MacroData) { 601 fn collect_macro(&mut self, mac: &raw::MacroData) {
diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs
index a15e62bbe..adac814d9 100644
--- a/crates/ra_hir/src/nameres/tests.rs
+++ b/crates/ra_hir/src/nameres/tests.rs
@@ -8,10 +8,9 @@ use std::sync::Arc;
8use ra_db::SourceDatabase; 8use ra_db::SourceDatabase;
9use test_utils::covers; 9use test_utils::covers;
10use insta::assert_snapshot_matches; 10use insta::assert_snapshot_matches;
11use either::Either;
12 11
13use crate::{ 12use crate::{
14 Crate, 13 Crate, Either,
15 mock::{MockDatabase, CrateGraphFixture}, 14 mock::{MockDatabase, CrateGraphFixture},
16 nameres::Resolution, 15 nameres::Resolution,
17}; 16};
@@ -37,19 +36,17 @@ fn render_crate_def_map(map: &CrateDefMap) -> String {
37 *buf += path; 36 *buf += path;
38 *buf += "\n"; 37 *buf += "\n";
39 38
40 let items = 39 let items = map.modules[module].scope.items.iter().map(|(name, it)| (name, Either::A(it)));
41 map.modules[module].scope.items.iter().map(|(name, it)| (name, Either::Left(it))); 40 let macros = map.modules[module].scope.macros.iter().map(|(name, m)| (name, Either::B(m)));
42 let macros =
43 map.modules[module].scope.macros.iter().map(|(name, m)| (name, Either::Right(m)));
44 let mut entries = items.chain(macros).collect::<Vec<_>>(); 41 let mut entries = items.chain(macros).collect::<Vec<_>>();
45 42
46 entries.sort_by_key(|(name, _)| *name); 43 entries.sort_by_key(|(name, _)| *name);
47 for (name, res) in entries { 44 for (name, res) in entries {
48 match res { 45 match res {
49 Either::Left(it) => { 46 Either::A(it) => {
50 *buf += &format!("{}: {}\n", name, dump_resolution(it)); 47 *buf += &format!("{}: {}\n", name, dump_resolution(it));
51 } 48 }
52 Either::Right(_) => { 49 Either::B(_) => {
53 *buf += &format!("{}: m\n", name); 50 *buf += &format!("{}: m\n", name);
54 } 51 }
55 } 52 }