diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-12-07 12:33:08 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-12-07 12:33:08 +0000 |
commit | 7aac5f2b427247120025f63c4864a1b937ed20c7 (patch) | |
tree | 51a841eaac9c7416e379b23887ee97ddc541ab5c /crates/ra_hir_def/src/resolver.rs | |
parent | 35fc983dd9e904ad4961b9c10be3397bad33da0c (diff) | |
parent | 29b5e1ec2a4bc25daddfe5137503b156b3cd283f (diff) |
Merge #2492
2492: Refactor generic parameteres lowering r=flodiebold a=matklad
indices and parent params seem to be concerns, specific to `hir_ty`, so move them there.
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir_def/src/resolver.rs')
-rw-r--r-- | crates/ra_hir_def/src/resolver.rs | 38 |
1 files changed, 23 insertions, 15 deletions
diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs index 7d4df222e..e00bd03d5 100644 --- a/crates/ra_hir_def/src/resolver.rs +++ b/crates/ra_hir_def/src/resolver.rs | |||
@@ -18,12 +18,13 @@ use crate::{ | |||
18 | path::{Path, PathKind}, | 18 | path::{Path, PathKind}, |
19 | per_ns::PerNs, | 19 | per_ns::PerNs, |
20 | AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, | 20 | AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, |
21 | GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, | 21 | GenericDefId, GenericParamId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, |
22 | StructId, TraitId, TypeAliasId, | 22 | StaticId, StructId, TraitId, TypeAliasId, |
23 | }; | 23 | }; |
24 | 24 | ||
25 | #[derive(Debug, Clone, Default)] | 25 | #[derive(Debug, Clone, Default)] |
26 | pub struct Resolver { | 26 | pub struct Resolver { |
27 | // FIXME: all usages generally call `.rev`, so maybe reverse once in consturciton? | ||
27 | scopes: Vec<Scope>, | 28 | scopes: Vec<Scope>, |
28 | } | 29 | } |
29 | 30 | ||
@@ -58,7 +59,7 @@ enum Scope { | |||
58 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 59 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
59 | pub enum TypeNs { | 60 | pub enum TypeNs { |
60 | SelfType(ImplId), | 61 | SelfType(ImplId), |
61 | GenericParam(u32), | 62 | GenericParam(GenericParamId), |
62 | AdtId(AdtId), | 63 | AdtId(AdtId), |
63 | AdtSelfType(AdtId), | 64 | AdtSelfType(AdtId), |
64 | // Yup, enum variants are added to the types ns, but any usage of variant as | 65 | // Yup, enum variants are added to the types ns, but any usage of variant as |
@@ -152,10 +153,13 @@ impl Resolver { | |||
152 | Scope::ExprScope(_) => continue, | 153 | Scope::ExprScope(_) => continue, |
153 | Scope::GenericParams { .. } | Scope::ImplBlockScope(_) if skip_to_mod => continue, | 154 | Scope::GenericParams { .. } | Scope::ImplBlockScope(_) if skip_to_mod => continue, |
154 | 155 | ||
155 | Scope::GenericParams { params, .. } => { | 156 | Scope::GenericParams { params, def } => { |
156 | if let Some(param) = params.find_by_name(first_name) { | 157 | if let Some(local_id) = params.find_by_name(first_name) { |
157 | let idx = if path.segments.len() == 1 { None } else { Some(1) }; | 158 | let idx = if path.segments.len() == 1 { None } else { Some(1) }; |
158 | return Some((TypeNs::GenericParam(param.idx), idx)); | 159 | return Some(( |
160 | TypeNs::GenericParam(GenericParamId { local_id, parent: *def }), | ||
161 | idx, | ||
162 | )); | ||
159 | } | 163 | } |
160 | } | 164 | } |
161 | Scope::ImplBlockScope(impl_) => { | 165 | Scope::ImplBlockScope(impl_) => { |
@@ -246,9 +250,9 @@ impl Resolver { | |||
246 | } | 250 | } |
247 | Scope::ExprScope(_) => continue, | 251 | Scope::ExprScope(_) => continue, |
248 | 252 | ||
249 | Scope::GenericParams { params, .. } if n_segments > 1 => { | 253 | Scope::GenericParams { params, def } if n_segments > 1 => { |
250 | if let Some(param) = params.find_by_name(first_name) { | 254 | if let Some(local_id) = params.find_by_name(first_name) { |
251 | let ty = TypeNs::GenericParam(param.idx); | 255 | let ty = TypeNs::GenericParam(GenericParamId { local_id, parent: *def }); |
252 | return Some(ResolveValueResult::Partial(ty, 1)); | 256 | return Some(ResolveValueResult::Partial(ty, 1)); |
253 | } | 257 | } |
254 | } | 258 | } |
@@ -368,6 +372,7 @@ impl Resolver { | |||
368 | ) -> impl Iterator<Item = &'a crate::generics::WherePredicate> + 'a { | 372 | ) -> impl Iterator<Item = &'a crate::generics::WherePredicate> + 'a { |
369 | self.scopes | 373 | self.scopes |
370 | .iter() | 374 | .iter() |
375 | .rev() | ||
371 | .filter_map(|scope| match scope { | 376 | .filter_map(|scope| match scope { |
372 | Scope::GenericParams { params, .. } => Some(params), | 377 | Scope::GenericParams { params, .. } => Some(params), |
373 | _ => None, | 378 | _ => None, |
@@ -376,14 +381,14 @@ impl Resolver { | |||
376 | } | 381 | } |
377 | 382 | ||
378 | pub fn generic_def(&self) -> Option<GenericDefId> { | 383 | pub fn generic_def(&self) -> Option<GenericDefId> { |
379 | self.scopes.iter().find_map(|scope| match scope { | 384 | self.scopes.iter().rev().find_map(|scope| match scope { |
380 | Scope::GenericParams { def, .. } => Some(*def), | 385 | Scope::GenericParams { def, .. } => Some(*def), |
381 | _ => None, | 386 | _ => None, |
382 | }) | 387 | }) |
383 | } | 388 | } |
384 | 389 | ||
385 | pub fn body_owner(&self) -> Option<DefWithBodyId> { | 390 | pub fn body_owner(&self) -> Option<DefWithBodyId> { |
386 | self.scopes.iter().find_map(|scope| match scope { | 391 | self.scopes.iter().rev().find_map(|scope| match scope { |
387 | Scope::ExprScope(it) => Some(it.owner), | 392 | Scope::ExprScope(it) => Some(it.owner), |
388 | _ => None, | 393 | _ => None, |
389 | }) | 394 | }) |
@@ -394,7 +399,7 @@ pub enum ScopeDef { | |||
394 | PerNs(PerNs), | 399 | PerNs(PerNs), |
395 | ImplSelfType(ImplId), | 400 | ImplSelfType(ImplId), |
396 | AdtSelfType(AdtId), | 401 | AdtSelfType(AdtId), |
397 | GenericParam(u32), | 402 | GenericParam(GenericParamId), |
398 | Local(PatId), | 403 | Local(PatId), |
399 | } | 404 | } |
400 | 405 | ||
@@ -425,9 +430,12 @@ impl Scope { | |||
425 | }); | 430 | }); |
426 | } | 431 | } |
427 | } | 432 | } |
428 | Scope::GenericParams { params, .. } => { | 433 | Scope::GenericParams { params, def } => { |
429 | for param in params.params.iter() { | 434 | for (local_id, param) in params.params.iter() { |
430 | f(param.name.clone(), ScopeDef::GenericParam(param.idx)) | 435 | f( |
436 | param.name.clone(), | ||
437 | ScopeDef::GenericParam(GenericParamId { local_id, parent: *def }), | ||
438 | ) | ||
431 | } | 439 | } |
432 | } | 440 | } |
433 | Scope::ImplBlockScope(i) => { | 441 | Scope::ImplBlockScope(i) => { |