diff options
author | Aleksey Kladov <[email protected]> | 2019-06-10 23:06:11 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-06-10 23:26:20 +0100 |
commit | 156b7ee84210583fa2fdc7fb8ae1dccafdf80830 (patch) | |
tree | 5e0d18e023a095c64a02f1c44c4a82245c89beeb | |
parent | 75e6c03883c4533b1134c806d166b72200b4837d (diff) |
use single version of either in hir
-rw-r--r-- | Cargo.lock | 1 | ||||
-rw-r--r-- | crates/ra_hir/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/ra_hir/src/either.rs | 18 | ||||
-rw-r--r-- | crates/ra_hir/src/nameres.rs | 58 | ||||
-rw-r--r-- | crates/ra_hir/src/nameres/collector.rs | 46 | ||||
-rw-r--r-- | crates/ra_hir/src/nameres/tests.rs | 13 | ||||
-rw-r--r-- | crates/ra_hir/src/resolve.rs | 4 |
7 files changed, 73 insertions, 68 deletions
diff --git a/Cargo.lock b/Cargo.lock index 4fccddc5a..202c8838c 100644 --- a/Cargo.lock +++ b/Cargo.lock | |||
@@ -1079,7 +1079,6 @@ dependencies = [ | |||
1079 | "chalk-ir 0.1.0 (git+https://github.com/flodiebold/chalk.git?branch=fuel)", | 1079 | "chalk-ir 0.1.0 (git+https://github.com/flodiebold/chalk.git?branch=fuel)", |
1080 | "chalk-rust-ir 0.1.0 (git+https://github.com/flodiebold/chalk.git?branch=fuel)", | 1080 | "chalk-rust-ir 0.1.0 (git+https://github.com/flodiebold/chalk.git?branch=fuel)", |
1081 | "chalk-solve 0.1.0 (git+https://github.com/flodiebold/chalk.git?branch=fuel)", | 1081 | "chalk-solve 0.1.0 (git+https://github.com/flodiebold/chalk.git?branch=fuel)", |
1082 | "either 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)", | ||
1083 | "ena 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", | 1082 | "ena 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", |
1084 | "flexi_logger 0.11.5 (registry+https://github.com/rust-lang/crates.io-index)", | 1083 | "flexi_logger 0.11.5 (registry+https://github.com/rust-lang/crates.io-index)", |
1085 | "insta 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", | 1084 | "insta 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", |
diff --git a/crates/ra_hir/Cargo.toml b/crates/ra_hir/Cargo.toml index 78808e72f..12d849f37 100644 --- a/crates/ra_hir/Cargo.toml +++ b/crates/ra_hir/Cargo.toml | |||
@@ -12,7 +12,6 @@ rustc-hash = "1.0" | |||
12 | parking_lot = "0.8.0" | 12 | parking_lot = "0.8.0" |
13 | ena = "0.11" | 13 | ena = "0.11" |
14 | join_to_string = "0.1.3" | 14 | join_to_string = "0.1.3" |
15 | either = "1.5.2" | ||
16 | once_cell = "0.2" | 15 | once_cell = "0.2" |
17 | 16 | ||
18 | ra_syntax = { path = "../ra_syntax" } | 17 | ra_syntax = { path = "../ra_syntax" } |
diff --git a/crates/ra_hir/src/either.rs b/crates/ra_hir/src/either.rs index 4073cc82e..71c53ebc0 100644 --- a/crates/ra_hir/src/either.rs +++ b/crates/ra_hir/src/either.rs | |||
@@ -25,4 +25,22 @@ impl<A, B> Either<A, B> { | |||
25 | Either::B(b) => Either::B(f2(b)), | 25 | Either::B(b) => Either::B(f2(b)), |
26 | } | 26 | } |
27 | } | 27 | } |
28 | pub fn a(self) -> Option<A> { | ||
29 | match self { | ||
30 | Either::A(it) => Some(it), | ||
31 | Either::B(_) => None, | ||
32 | } | ||
33 | } | ||
34 | pub fn b(self) -> Option<B> { | ||
35 | match self { | ||
36 | Either::A(_) => None, | ||
37 | Either::B(it) => Some(it), | ||
38 | } | ||
39 | } | ||
40 | pub fn as_ref(&self) -> Either<&A, &B> { | ||
41 | match self { | ||
42 | Either::A(it) => Either::A(it), | ||
43 | Either::B(it) => Either::B(it), | ||
44 | } | ||
45 | } | ||
28 | } | 46 | } |
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; | |||
56 | use std::sync::Arc; | 56 | use std::sync::Arc; |
57 | 57 | ||
58 | use rustc_hash::{FxHashMap, FxHashSet}; | 58 | use rustc_hash::{FxHashMap, FxHashSet}; |
59 | use either::Either; | ||
60 | use ra_arena::{Arena, RawId, impl_arena_id}; | 59 | use ra_arena::{Arena, RawId, impl_arena_id}; |
61 | use ra_db::{FileId, Edition}; | 60 | use ra_db::{FileId, Edition}; |
62 | use test_utils::tested_by; | 61 | use 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 | ||
191 | impl ResolvePathResult { | 191 | impl 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 |
218 | fn or(left: ItemOrMacro, right: ItemOrMacro) -> ItemOrMacro { | 218 | fn 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 | } |
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 @@ | |||
1 | use arrayvec::ArrayVec; | 1 | use arrayvec::ArrayVec; |
2 | use rustc_hash::FxHashMap; | 2 | use rustc_hash::FxHashMap; |
3 | use either::Either; | ||
4 | use relative_path::RelativePathBuf; | 3 | use relative_path::RelativePathBuf; |
5 | use test_utils::tested_by; | 4 | use test_utils::tested_by; |
6 | use ra_db::FileId; | 5 | use ra_db::FileId; |
@@ -9,7 +8,7 @@ use ra_syntax::ast; | |||
9 | use crate::{ | 8 | use 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 | ||
23 | pub(super) fn collect_defs( | 22 | pub(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; | |||
8 | use ra_db::SourceDatabase; | 8 | use ra_db::SourceDatabase; |
9 | use test_utils::covers; | 9 | use test_utils::covers; |
10 | use insta::assert_snapshot_matches; | 10 | use insta::assert_snapshot_matches; |
11 | use either::Either; | ||
12 | 11 | ||
13 | use crate::{ | 12 | use 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 | } |
diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index 1b987c1b6..7f8b3812c 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs | |||
@@ -2,7 +2,6 @@ | |||
2 | use std::sync::Arc; | 2 | use std::sync::Arc; |
3 | 3 | ||
4 | use rustc_hash::{FxHashMap, FxHashSet}; | 4 | use rustc_hash::{FxHashMap, FxHashSet}; |
5 | use either::Either; | ||
6 | 5 | ||
7 | use crate::{ | 6 | use crate::{ |
8 | ModuleDef, Trait, MacroDef, | 7 | ModuleDef, Trait, MacroDef, |
@@ -14,6 +13,7 @@ use crate::{ | |||
14 | expr::{scope::{ExprScopes, ScopeId}, PatId}, | 13 | expr::{scope::{ExprScopes, ScopeId}, PatId}, |
15 | impl_block::ImplBlock, | 14 | impl_block::ImplBlock, |
16 | path::Path, | 15 | path::Path, |
16 | either::Either, | ||
17 | }; | 17 | }; |
18 | 18 | ||
19 | #[derive(Debug, Clone, Default)] | 19 | #[derive(Debug, Clone, Default)] |
@@ -137,7 +137,7 @@ impl Resolver { | |||
137 | ) -> Option<MacroDef> { | 137 | ) -> Option<MacroDef> { |
138 | let (item_map, module) = self.module()?; | 138 | let (item_map, module) = self.module()?; |
139 | match item_map.resolve_path_with_macro(db, module, path) { | 139 | match item_map.resolve_path_with_macro(db, module, path) { |
140 | (Either::Right(macro_def), None) => Some(macro_def), | 140 | (Either::B(macro_def), None) => Some(macro_def), |
141 | _ => None, | 141 | _ => None, |
142 | } | 142 | } |
143 | } | 143 | } |