diff options
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r-- | crates/ra_hir_def/src/db.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir_def/src/generics.rs | 29 |
2 files changed, 31 insertions, 4 deletions
diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs index c6cd4369b..844f8bbe8 100644 --- a/crates/ra_hir_def/src/db.rs +++ b/crates/ra_hir_def/src/db.rs | |||
@@ -8,13 +8,14 @@ use ra_syntax::ast; | |||
8 | use crate::{ | 8 | use crate::{ |
9 | adt::{EnumData, StructData}, | 9 | adt::{EnumData, StructData}, |
10 | body::{scope::ExprScopes, Body, BodySourceMap}, | 10 | body::{scope::ExprScopes, Body, BodySourceMap}, |
11 | generics::GenericParams, | ||
11 | impls::ImplData, | 12 | impls::ImplData, |
12 | nameres::{ | 13 | nameres::{ |
13 | raw::{ImportSourceMap, RawItems}, | 14 | raw::{ImportSourceMap, RawItems}, |
14 | CrateDefMap, | 15 | CrateDefMap, |
15 | }, | 16 | }, |
16 | traits::TraitData, | 17 | traits::TraitData, |
17 | DefWithBodyId, EnumId, ImplId, ItemLoc, StructOrUnionId, TraitId, | 18 | DefWithBodyId, EnumId, GenericDefId, ImplId, ItemLoc, StructOrUnionId, TraitId, |
18 | }; | 19 | }; |
19 | 20 | ||
20 | #[salsa::query_group(InternDatabaseStorage)] | 21 | #[salsa::query_group(InternDatabaseStorage)] |
@@ -71,4 +72,7 @@ pub trait DefDatabase2: InternDatabase + AstDatabase { | |||
71 | 72 | ||
72 | #[salsa::invoke(ExprScopes::expr_scopes_query)] | 73 | #[salsa::invoke(ExprScopes::expr_scopes_query)] |
73 | fn expr_scopes(&self, def: DefWithBodyId) -> Arc<ExprScopes>; | 74 | fn expr_scopes(&self, def: DefWithBodyId) -> Arc<ExprScopes>; |
75 | |||
76 | #[salsa::invoke(GenericParams::generic_params_query)] | ||
77 | fn generic_params(&self, def: GenericDefId) -> Arc<GenericParams>; | ||
74 | } | 78 | } |
diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs index 17a5d5f43..f794eefbc 100644 --- a/crates/ra_hir_def/src/generics.rs +++ b/crates/ra_hir_def/src/generics.rs | |||
@@ -5,13 +5,12 @@ | |||
5 | use std::sync::Arc; | 5 | use std::sync::Arc; |
6 | 6 | ||
7 | use hir_expand::name::{self, AsName, Name}; | 7 | use hir_expand::name::{self, AsName, Name}; |
8 | |||
9 | use ra_syntax::ast::{self, NameOwner, TypeBoundsOwner, TypeParamsOwner}; | 8 | use ra_syntax::ast::{self, NameOwner, TypeBoundsOwner, TypeParamsOwner}; |
10 | 9 | ||
11 | use crate::{ | 10 | use crate::{ |
12 | db::DefDatabase2, | 11 | db::DefDatabase2, |
13 | type_ref::{TypeBound, TypeRef}, | 12 | type_ref::{TypeBound, TypeRef}, |
14 | AdtId, AstItemDef, GenericDefId, HasSource, Lookup, | 13 | AdtId, AstItemDef, ContainerId, GenericDefId, HasSource, Lookup, |
15 | }; | 14 | }; |
16 | 15 | ||
17 | /// Data about a generic parameter (to a function, struct, impl, ...). | 16 | /// Data about a generic parameter (to a function, struct, impl, ...). |
@@ -43,7 +42,15 @@ pub struct WherePredicate { | |||
43 | } | 42 | } |
44 | 43 | ||
45 | impl GenericParams { | 44 | impl GenericParams { |
46 | pub fn new( | 45 | pub(crate) fn generic_params_query( |
46 | db: &impl DefDatabase2, | ||
47 | def: GenericDefId, | ||
48 | ) -> Arc<GenericParams> { | ||
49 | let parent_generics = parent_generic_def(db, def).map(|it| db.generic_params(it)); | ||
50 | Arc::new(GenericParams::new(db, def.into(), parent_generics)) | ||
51 | } | ||
52 | |||
53 | fn new( | ||
47 | db: &impl DefDatabase2, | 54 | db: &impl DefDatabase2, |
48 | def: GenericDefId, | 55 | def: GenericDefId, |
49 | parent_params: Option<Arc<GenericParams>>, | 56 | parent_params: Option<Arc<GenericParams>>, |
@@ -161,3 +168,19 @@ impl GenericParams { | |||
161 | vec | 168 | vec |
162 | } | 169 | } |
163 | } | 170 | } |
171 | |||
172 | fn parent_generic_def(db: &impl DefDatabase2, def: GenericDefId) -> Option<GenericDefId> { | ||
173 | let container = match def { | ||
174 | GenericDefId::FunctionId(it) => it.lookup(db).container, | ||
175 | GenericDefId::TypeAliasId(it) => it.lookup(db).container, | ||
176 | GenericDefId::ConstId(it) => it.lookup(db).container, | ||
177 | GenericDefId::EnumVariantId(it) => return Some(it.parent.into()), | ||
178 | GenericDefId::AdtId(_) | GenericDefId::TraitId(_) | GenericDefId::ImplId(_) => return None, | ||
179 | }; | ||
180 | |||
181 | match container { | ||
182 | ContainerId::ImplId(it) => Some(it.into()), | ||
183 | ContainerId::TraitId(it) => Some(it.into()), | ||
184 | ContainerId::ModuleId(_) => None, | ||
185 | } | ||
186 | } | ||