diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-06-10 23:26:50 +0100 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-06-10 23:26:50 +0100 |
commit | 1c867b4e67126350579d6d598efeb6c03b503ddc (patch) | |
tree | 5e0d18e023a095c64a02f1c44c4a82245c89beeb /crates/ra_hir/src/nameres/collector.rs | |
parent | 75e6c03883c4533b1134c806d166b72200b4837d (diff) | |
parent | 156b7ee84210583fa2fdc7fb8ae1dccafdf80830 (diff) |
Merge #1390
1390: use single version of either in hir r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/nameres/collector.rs')
-rw-r--r-- | crates/ra_hir/src/nameres/collector.rs | 46 |
1 files changed, 20 insertions, 26 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 @@ | |||
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) { |