diff options
author | Aleksey Kladov <[email protected]> | 2019-11-20 17:50:34 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-11-20 17:50:34 +0000 |
commit | 12ec946216a3637685f30ae359bc955313595a22 (patch) | |
tree | 62cbd8b20865648660be5a7b9dd7a65486e54535 /crates/ra_hir | |
parent | cb642fc578100c0945088accb85acb8f03d2e1fd (diff) |
Simplify generic params
Diffstat (limited to 'crates/ra_hir')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 23 | ||||
-rw-r--r-- | crates/ra_hir/src/impl_block.rs | 8 | ||||
-rw-r--r-- | crates/ra_hir/src/resolve.rs | 39 |
3 files changed, 33 insertions, 37 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 920899dce..e2638cf92 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -322,9 +322,7 @@ impl Struct { | |||
322 | // take the outer scope... | 322 | // take the outer scope... |
323 | let r = self.module(db).resolver(db); | 323 | let r = self.module(db).resolver(db); |
324 | // ...and add generic params, if present | 324 | // ...and add generic params, if present |
325 | let p = self.generic_params(db); | 325 | r.push_generic_params_scope(db, self.into()) |
326 | let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r }; | ||
327 | r | ||
328 | } | 326 | } |
329 | } | 327 | } |
330 | 328 | ||
@@ -352,9 +350,7 @@ impl Union { | |||
352 | // take the outer scope... | 350 | // take the outer scope... |
353 | let r = self.module(db).resolver(db); | 351 | let r = self.module(db).resolver(db); |
354 | // ...and add generic params, if present | 352 | // ...and add generic params, if present |
355 | let p = self.generic_params(db); | 353 | r.push_generic_params_scope(db, self.into()) |
356 | let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r }; | ||
357 | r | ||
358 | } | 354 | } |
359 | } | 355 | } |
360 | 356 | ||
@@ -402,8 +398,7 @@ impl Enum { | |||
402 | // take the outer scope... | 398 | // take the outer scope... |
403 | let r = self.module(db).resolver(db); | 399 | let r = self.module(db).resolver(db); |
404 | // ...and add generic params, if present | 400 | // ...and add generic params, if present |
405 | let p = self.generic_params(db); | 401 | let r = r.push_generic_params_scope(db, self.into()); |
406 | let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r }; | ||
407 | r.push_scope(Scope::AdtScope(self.into())) | 402 | r.push_scope(Scope::AdtScope(self.into())) |
408 | } | 403 | } |
409 | } | 404 | } |
@@ -709,9 +704,7 @@ impl Function { | |||
709 | // take the outer scope... | 704 | // take the outer scope... |
710 | let r = self.container(db).map_or_else(|| self.module(db).resolver(db), |c| c.resolver(db)); | 705 | let r = self.container(db).map_or_else(|| self.module(db).resolver(db), |c| c.resolver(db)); |
711 | // ...and add generic params, if present | 706 | // ...and add generic params, if present |
712 | let p = self.generic_params(db); | 707 | r.push_generic_params_scope(db, self.into()) |
713 | let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r }; | ||
714 | r | ||
715 | } | 708 | } |
716 | 709 | ||
717 | pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) { | 710 | pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) { |
@@ -946,9 +939,7 @@ impl Trait { | |||
946 | pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver { | 939 | pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver { |
947 | let r = self.module(db).resolver(db); | 940 | let r = self.module(db).resolver(db); |
948 | // add generic params, if present | 941 | // add generic params, if present |
949 | let p = self.generic_params(db); | 942 | r.push_generic_params_scope(db, self.into()) |
950 | let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r }; | ||
951 | r | ||
952 | } | 943 | } |
953 | } | 944 | } |
954 | 945 | ||
@@ -1010,9 +1001,7 @@ impl TypeAlias { | |||
1010 | .map(|ib| ib.resolver(db)) | 1001 | .map(|ib| ib.resolver(db)) |
1011 | .unwrap_or_else(|| self.module(db).resolver(db)); | 1002 | .unwrap_or_else(|| self.module(db).resolver(db)); |
1012 | // ...and add generic params, if present | 1003 | // ...and add generic params, if present |
1013 | let p = self.generic_params(db); | 1004 | r.push_generic_params_scope(db, self.into()) |
1014 | let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r }; | ||
1015 | r | ||
1016 | } | 1005 | } |
1017 | } | 1006 | } |
1018 | 1007 | ||
diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs index 0513f28a9..492d964a4 100644 --- a/crates/ra_hir/src/impl_block.rs +++ b/crates/ra_hir/src/impl_block.rs | |||
@@ -5,7 +5,6 @@ use ra_syntax::ast::{self}; | |||
5 | 5 | ||
6 | use crate::{ | 6 | use crate::{ |
7 | db::{AstDatabase, DefDatabase, HirDatabase}, | 7 | db::{AstDatabase, DefDatabase, HirDatabase}, |
8 | generics::HasGenericParams, | ||
9 | resolve::Resolver, | 8 | resolve::Resolver, |
10 | ty::Ty, | 9 | ty::Ty, |
11 | AssocItem, Crate, HasSource, ImplBlock, Module, Source, TraitRef, | 10 | AssocItem, Crate, HasSource, ImplBlock, Module, Source, TraitRef, |
@@ -52,12 +51,11 @@ impl ImplBlock { | |||
52 | Crate { crate_id: self.module(db).id.krate } | 51 | Crate { crate_id: self.module(db).id.krate } |
53 | } | 52 | } |
54 | 53 | ||
55 | pub(crate) fn resolver(&self, db: &impl DefDatabase) -> Resolver { | 54 | pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver { |
56 | let r = self.module(db).resolver(db); | 55 | let r = self.module(db).resolver(db); |
57 | // add generic params, if present | 56 | // add generic params, if present |
58 | let p = self.generic_params(db); | 57 | let r = r.push_generic_params_scope(db, self.into()); |
59 | let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r }; | 58 | let r = r.push_impl_block_scope(self); |
60 | let r = r.push_impl_block_scope(self.clone()); | ||
61 | r | 59 | r |
62 | } | 60 | } |
63 | } | 61 | } |
diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index a2fa0bb79..e5e768be9 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs | |||
@@ -14,9 +14,9 @@ use crate::{ | |||
14 | code_model::Crate, | 14 | code_model::Crate, |
15 | db::{DefDatabase, HirDatabase}, | 15 | db::{DefDatabase, HirDatabase}, |
16 | expr::{ExprScopes, PatId, ScopeId}, | 16 | expr::{ExprScopes, PatId, ScopeId}, |
17 | generics::GenericParams, | 17 | generics::{GenericParams, HasGenericParams}, |
18 | Adt, Const, DefWithBody, Enum, EnumVariant, Function, ImplBlock, Local, MacroDef, ModuleDef, | 18 | Adt, Const, DefWithBody, Enum, EnumVariant, Function, GenericDef, ImplBlock, Local, MacroDef, |
19 | PerNs, Static, Struct, Trait, TypeAlias, | 19 | ModuleDef, PerNs, Static, Struct, Trait, TypeAlias, |
20 | }; | 20 | }; |
21 | 21 | ||
22 | #[derive(Debug, Clone, Default)] | 22 | #[derive(Debug, Clone, Default)] |
@@ -43,7 +43,7 @@ pub(crate) enum Scope { | |||
43 | /// All the items and imported names of a module | 43 | /// All the items and imported names of a module |
44 | ModuleScope(ModuleItemMap), | 44 | ModuleScope(ModuleItemMap), |
45 | /// Brings the generic parameters of an item into scope | 45 | /// Brings the generic parameters of an item into scope |
46 | GenericParams(Arc<GenericParams>), | 46 | GenericParams { def: GenericDef, params: Arc<GenericParams> }, |
47 | /// Brings `Self` in `impl` block into scope | 47 | /// Brings `Self` in `impl` block into scope |
48 | ImplBlockScope(ImplBlock), | 48 | ImplBlockScope(ImplBlock), |
49 | /// Brings `Self` in enum, struct and union definitions into scope | 49 | /// Brings `Self` in enum, struct and union definitions into scope |
@@ -141,9 +141,9 @@ impl Resolver { | |||
141 | for scope in self.scopes.iter().rev() { | 141 | for scope in self.scopes.iter().rev() { |
142 | match scope { | 142 | match scope { |
143 | Scope::ExprScope(_) => continue, | 143 | Scope::ExprScope(_) => continue, |
144 | Scope::GenericParams(_) | Scope::ImplBlockScope(_) if skip_to_mod => continue, | 144 | Scope::GenericParams { .. } | Scope::ImplBlockScope(_) if skip_to_mod => continue, |
145 | 145 | ||
146 | Scope::GenericParams(params) => { | 146 | Scope::GenericParams { params, .. } => { |
147 | if let Some(param) = params.find_by_name(first_name) { | 147 | if let Some(param) = params.find_by_name(first_name) { |
148 | let idx = if path.segments.len() == 1 { None } else { Some(1) }; | 148 | let idx = if path.segments.len() == 1 { None } else { Some(1) }; |
149 | return Some((TypeNs::GenericParam(param.idx), idx)); | 149 | return Some((TypeNs::GenericParam(param.idx), idx)); |
@@ -212,7 +212,7 @@ impl Resolver { | |||
212 | match scope { | 212 | match scope { |
213 | Scope::AdtScope(_) | 213 | Scope::AdtScope(_) |
214 | | Scope::ExprScope(_) | 214 | | Scope::ExprScope(_) |
215 | | Scope::GenericParams(_) | 215 | | Scope::GenericParams { .. } |
216 | | Scope::ImplBlockScope(_) | 216 | | Scope::ImplBlockScope(_) |
217 | if skip_to_mod => | 217 | if skip_to_mod => |
218 | { | 218 | { |
@@ -232,13 +232,13 @@ impl Resolver { | |||
232 | } | 232 | } |
233 | Scope::ExprScope(_) => continue, | 233 | Scope::ExprScope(_) => continue, |
234 | 234 | ||
235 | Scope::GenericParams(params) if n_segments > 1 => { | 235 | Scope::GenericParams { params, .. } if n_segments > 1 => { |
236 | if let Some(param) = params.find_by_name(first_name) { | 236 | if let Some(param) = params.find_by_name(first_name) { |
237 | let ty = TypeNs::GenericParam(param.idx); | 237 | let ty = TypeNs::GenericParam(param.idx); |
238 | return Some(ResolveValueResult::Partial(ty, 1)); | 238 | return Some(ResolveValueResult::Partial(ty, 1)); |
239 | } | 239 | } |
240 | } | 240 | } |
241 | Scope::GenericParams(_) => continue, | 241 | Scope::GenericParams { .. } => continue, |
242 | 242 | ||
243 | Scope::ImplBlockScope(impl_) if n_segments > 1 => { | 243 | Scope::ImplBlockScope(impl_) if n_segments > 1 => { |
244 | if first_name == &name::SELF_TYPE { | 244 | if first_name == &name::SELF_TYPE { |
@@ -361,7 +361,7 @@ impl Resolver { | |||
361 | self.scopes | 361 | self.scopes |
362 | .iter() | 362 | .iter() |
363 | .filter_map(|scope| match scope { | 363 | .filter_map(|scope| match scope { |
364 | Scope::GenericParams(params) => Some(params), | 364 | Scope::GenericParams { params, .. } => Some(params), |
365 | _ => None, | 365 | _ => None, |
366 | }) | 366 | }) |
367 | .flat_map(|params| params.where_predicates.iter()) | 367 | .flat_map(|params| params.where_predicates.iter()) |
@@ -369,7 +369,7 @@ impl Resolver { | |||
369 | 369 | ||
370 | pub(crate) fn generic_def(&self) -> Option<crate::generics::GenericDef> { | 370 | pub(crate) fn generic_def(&self) -> Option<crate::generics::GenericDef> { |
371 | self.scopes.iter().find_map(|scope| match scope { | 371 | self.scopes.iter().find_map(|scope| match scope { |
372 | Scope::GenericParams(params) => Some(params.def.into()), | 372 | Scope::GenericParams { def, .. } => Some(*def), |
373 | _ => None, | 373 | _ => None, |
374 | }) | 374 | }) |
375 | } | 375 | } |
@@ -381,8 +381,17 @@ impl Resolver { | |||
381 | self | 381 | self |
382 | } | 382 | } |
383 | 383 | ||
384 | pub(crate) fn push_generic_params_scope(self, params: Arc<GenericParams>) -> Resolver { | 384 | pub(crate) fn push_generic_params_scope( |
385 | self.push_scope(Scope::GenericParams(params)) | 385 | self, |
386 | db: &impl DefDatabase, | ||
387 | def: GenericDef, | ||
388 | ) -> Resolver { | ||
389 | let params = def.generic_params(db); | ||
390 | if params.params.is_empty() { | ||
391 | self | ||
392 | } else { | ||
393 | self.push_scope(Scope::GenericParams { def, params }) | ||
394 | } | ||
386 | } | 395 | } |
387 | 396 | ||
388 | pub(crate) fn push_impl_block_scope(self, impl_block: ImplBlock) -> Resolver { | 397 | pub(crate) fn push_impl_block_scope(self, impl_block: ImplBlock) -> Resolver { |
@@ -457,8 +466,8 @@ impl Scope { | |||
457 | }); | 466 | }); |
458 | } | 467 | } |
459 | } | 468 | } |
460 | Scope::GenericParams(gp) => { | 469 | Scope::GenericParams { params, .. } => { |
461 | for param in &gp.params { | 470 | for param in params.params.iter() { |
462 | f(param.name.clone(), ScopeDef::GenericParam(param.idx)) | 471 | f(param.name.clone(), ScopeDef::GenericParam(param.idx)) |
463 | } | 472 | } |
464 | } | 473 | } |