aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/nameres.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/nameres.rs')
-rw-r--r--crates/ra_hir/src/nameres.rs58
1 files changed, 28 insertions, 30 deletions
diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs
index b5938fa03..d84134877 100644
--- a/crates/ra_hir/src/nameres.rs
+++ b/crates/ra_hir/src/nameres.rs
@@ -56,7 +56,6 @@ mod tests;
56use std::sync::Arc; 56use std::sync::Arc;
57 57
58use rustc_hash::{FxHashMap, FxHashSet}; 58use rustc_hash::{FxHashMap, FxHashSet};
59use either::Either;
60use ra_arena::{Arena, RawId, impl_arena_id}; 59use ra_arena::{Arena, RawId, impl_arena_id};
61use ra_db::{FileId, Edition}; 60use ra_db::{FileId, Edition};
62use test_utils::tested_by; 61use test_utils::tested_by;
@@ -70,6 +69,7 @@ use crate::{
70 ids::MacroDefId, 69 ids::MacroDefId,
71 diagnostics::DiagnosticSink, 70 diagnostics::DiagnosticSink,
72 nameres::diagnostics::DefDiagnostic, 71 nameres::diagnostics::DefDiagnostic,
72 either::Either,
73 AstId, 73 AstId,
74}; 74};
75 75
@@ -164,8 +164,8 @@ impl ModuleScope {
164 } 164 }
165 fn get_item_or_macro(&self, name: &Name) -> Option<ItemOrMacro> { 165 fn get_item_or_macro(&self, name: &Name) -> Option<ItemOrMacro> {
166 match (self.get(name), self.macros.get(name)) { 166 match (self.get(name), self.macros.get(name)) {
167 (Some(item), _) if !item.def.is_none() => Some(Either::Left(item.def)), 167 (Some(item), _) if !item.def.is_none() => Some(Either::A(item.def)),
168 (_, Some(macro_)) => Some(Either::Right(*macro_)), 168 (_, Some(macro_)) => Some(Either::B(*macro_)),
169 _ => None, 169 _ => None,
170 } 170 }
171 } 171 }
@@ -190,7 +190,7 @@ struct ResolvePathResult {
190 190
191impl ResolvePathResult { 191impl ResolvePathResult {
192 fn empty(reached_fixedpoint: ReachedFixedPoint) -> ResolvePathResult { 192 fn empty(reached_fixedpoint: ReachedFixedPoint) -> ResolvePathResult {
193 ResolvePathResult::with(Either::Left(PerNs::none()), reached_fixedpoint, None) 193 ResolvePathResult::with(Either::A(PerNs::none()), reached_fixedpoint, None)
194 } 194 }
195 195
196 fn with( 196 fn with(
@@ -217,13 +217,13 @@ enum ReachedFixedPoint {
217/// helper function for select item or macro to use 217/// helper function for select item or macro to use
218fn or(left: ItemOrMacro, right: ItemOrMacro) -> ItemOrMacro { 218fn or(left: ItemOrMacro, right: ItemOrMacro) -> ItemOrMacro {
219 match (left, right) { 219 match (left, right) {
220 (Either::Left(s), Either::Left(o)) => Either::Left(s.or(o)), 220 (Either::A(s), Either::A(o)) => Either::A(s.or(o)),
221 (Either::Right(s), _) => Either::Right(s), 221 (Either::B(s), _) => Either::B(s),
222 (Either::Left(s), Either::Right(o)) => { 222 (Either::A(s), Either::B(o)) => {
223 if !s.is_none() { 223 if !s.is_none() {
224 Either::Left(s) 224 Either::A(s)
225 } else { 225 } else {
226 Either::Right(o) 226 Either::B(o)
227 } 227 }
228 } 228 }
229 } 229 }
@@ -306,7 +306,7 @@ impl CrateDefMap {
306 path: &Path, 306 path: &Path,
307 ) -> (PerNs<ModuleDef>, Option<usize>) { 307 ) -> (PerNs<ModuleDef>, Option<usize>) {
308 let res = self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path); 308 let res = self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path);
309 (res.resolved_def.left().unwrap_or_else(PerNs::none), res.segment_index) 309 (res.resolved_def.a().unwrap_or_else(PerNs::none), res.segment_index)
310 } 310 }
311 311
312 pub(crate) fn resolve_path_with_macro( 312 pub(crate) fn resolve_path_with_macro(
@@ -330,10 +330,10 @@ impl CrateDefMap {
330 ) -> ResolvePathResult { 330 ) -> ResolvePathResult {
331 let mut segments = path.segments.iter().enumerate(); 331 let mut segments = path.segments.iter().enumerate();
332 let mut curr_per_ns: ItemOrMacro = match path.kind { 332 let mut curr_per_ns: ItemOrMacro = match path.kind {
333 PathKind::Crate => Either::Left(PerNs::types( 333 PathKind::Crate => {
334 Module { krate: self.krate, module_id: self.root }.into(), 334 Either::A(PerNs::types(Module { krate: self.krate, module_id: self.root }.into()))
335 )), 335 }
336 PathKind::Self_ => Either::Left(PerNs::types( 336 PathKind::Self_ => Either::A(PerNs::types(
337 Module { krate: self.krate, module_id: original_module }.into(), 337 Module { krate: self.krate, module_id: original_module }.into(),
338 )), 338 )),
339 // plain import or absolute path in 2015: crate-relative with 339 // plain import or absolute path in 2015: crate-relative with
@@ -361,7 +361,7 @@ impl CrateDefMap {
361 } 361 }
362 PathKind::Super => { 362 PathKind::Super => {
363 if let Some(p) = self.modules[original_module].parent { 363 if let Some(p) = self.modules[original_module].parent {
364 Either::Left(PerNs::types(Module { krate: self.krate, module_id: p }.into())) 364 Either::A(PerNs::types(Module { krate: self.krate, module_id: p }.into()))
365 } else { 365 } else {
366 log::debug!("super path in root module"); 366 log::debug!("super path in root module");
367 return ResolvePathResult::empty(ReachedFixedPoint::Yes); 367 return ResolvePathResult::empty(ReachedFixedPoint::Yes);
@@ -375,7 +375,7 @@ impl CrateDefMap {
375 }; 375 };
376 if let Some(def) = self.extern_prelude.get(&segment.name) { 376 if let Some(def) = self.extern_prelude.get(&segment.name) {
377 log::debug!("absolute path {:?} resolved to crate {:?}", path, def); 377 log::debug!("absolute path {:?} resolved to crate {:?}", path, def);
378 Either::Left(PerNs::types(*def)) 378 Either::A(PerNs::types(*def))
379 } else { 379 } else {
380 return ResolvePathResult::empty(ReachedFixedPoint::No); // extern crate declarations can add to the extern prelude 380 return ResolvePathResult::empty(ReachedFixedPoint::No); // extern crate declarations can add to the extern prelude
381 } 381 }
@@ -383,7 +383,7 @@ impl CrateDefMap {
383 }; 383 };
384 384
385 for (i, segment) in segments { 385 for (i, segment) in segments {
386 let curr = match curr_per_ns.as_ref().left().and_then(|m| m.as_ref().take_types()) { 386 let curr = match curr_per_ns.as_ref().a().and_then(|m| m.as_ref().take_types()) {
387 Some(r) => r, 387 Some(r) => r,
388 None => { 388 None => {
389 // we still have path segments left, but the path so far 389 // we still have path segments left, but the path so far
@@ -424,10 +424,10 @@ impl CrateDefMap {
424 // enum variant 424 // enum variant
425 tested_by!(can_import_enum_variant); 425 tested_by!(can_import_enum_variant);
426 match e.variant(db, &segment.name) { 426 match e.variant(db, &segment.name) {
427 Some(variant) => Either::Left(PerNs::both(variant.into(), variant.into())), 427 Some(variant) => Either::A(PerNs::both(variant.into(), variant.into())),
428 None => { 428 None => {
429 return ResolvePathResult::with( 429 return ResolvePathResult::with(
430 Either::Left(PerNs::types((*e).into())), 430 Either::A(PerNs::types((*e).into())),
431 ReachedFixedPoint::Yes, 431 ReachedFixedPoint::Yes,
432 Some(i), 432 Some(i),
433 ); 433 );
@@ -444,7 +444,7 @@ impl CrateDefMap {
444 ); 444 );
445 445
446 return ResolvePathResult::with( 446 return ResolvePathResult::with(
447 Either::Left(PerNs::types(*s)), 447 Either::A(PerNs::types(*s)),
448 ReachedFixedPoint::Yes, 448 ReachedFixedPoint::Yes,
449 Some(i), 449 Some(i),
450 ); 450 );
@@ -458,10 +458,10 @@ impl CrateDefMap {
458 let from_crate_root = self[self.root] 458 let from_crate_root = self[self.root]
459 .scope 459 .scope
460 .get_item_or_macro(name) 460 .get_item_or_macro(name)
461 .unwrap_or_else(|| Either::Left(PerNs::none())); 461 .unwrap_or_else(|| Either::A(PerNs::none()));
462 let from_extern_prelude = self.resolve_name_in_extern_prelude(name); 462 let from_extern_prelude = self.resolve_name_in_extern_prelude(name);
463 463
464 or(from_crate_root, Either::Left(from_extern_prelude)) 464 or(from_crate_root, Either::A(from_extern_prelude))
465 } 465 }
466 466
467 pub(crate) fn resolve_name_in_module( 467 pub(crate) fn resolve_name_in_module(
@@ -470,7 +470,7 @@ impl CrateDefMap {
470 module: CrateModuleId, 470 module: CrateModuleId,
471 name: &Name, 471 name: &Name,
472 ) -> PerNs<ModuleDef> { 472 ) -> PerNs<ModuleDef> {
473 self.resolve_name_in_module_with_macro(db, module, name).left().unwrap_or_else(PerNs::none) 473 self.resolve_name_in_module_with_macro(db, module, name).a().unwrap_or_else(PerNs::none)
474 } 474 }
475 475
476 fn resolve_name_in_module_with_macro( 476 fn resolve_name_in_module_with_macro(
@@ -483,15 +483,13 @@ impl CrateDefMap {
483 // - current module / scope 483 // - current module / scope
484 // - extern prelude 484 // - extern prelude
485 // - std prelude 485 // - std prelude
486 let from_scope = self[module] 486 let from_scope =
487 .scope 487 self[module].scope.get_item_or_macro(name).unwrap_or_else(|| Either::A(PerNs::none()));
488 .get_item_or_macro(name)
489 .unwrap_or_else(|| Either::Left(PerNs::none()));
490 let from_extern_prelude = 488 let from_extern_prelude =
491 self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it)); 489 self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it));
492 let from_prelude = self.resolve_in_prelude(db, name); 490 let from_prelude = self.resolve_in_prelude(db, name);
493 491
494 or(from_scope, or(Either::Left(from_extern_prelude), from_prelude)) 492 or(from_scope, or(Either::A(from_extern_prelude), from_prelude))
495 } 493 }
496 494
497 fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs<ModuleDef> { 495 fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs<ModuleDef> {
@@ -505,9 +503,9 @@ impl CrateDefMap {
505 } else { 503 } else {
506 db.crate_def_map(prelude.krate)[prelude.module_id].scope.get_item_or_macro(name) 504 db.crate_def_map(prelude.krate)[prelude.module_id].scope.get_item_or_macro(name)
507 }; 505 };
508 resolution.unwrap_or_else(|| Either::Left(PerNs::none())) 506 resolution.unwrap_or_else(|| Either::A(PerNs::none()))
509 } else { 507 } else {
510 Either::Left(PerNs::none()) 508 Either::A(PerNs::none())
511 } 509 }
512 } 510 }
513} 511}