aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir/src/code_model.rs46
-rw-r--r--crates/ra_hir/src/db.rs5
-rw-r--r--crates/ra_hir/src/from_id.rs17
-rw-r--r--crates/ra_hir/src/generics.rs54
-rw-r--r--crates/ra_hir/src/lib.rs8
-rw-r--r--crates/ra_hir/src/ty.rs22
-rw-r--r--crates/ra_hir/src/ty/autoderef.rs4
-rw-r--r--crates/ra_hir/src/ty/infer/expr.rs8
-rw-r--r--crates/ra_hir/src/ty/infer/path.rs3
-rw-r--r--crates/ra_hir/src/ty/lower.rs33
-rw-r--r--crates/ra_hir/src/ty/traits/chalk.rs13
-rw-r--r--crates/ra_hir_def/src/resolver.rs107
12 files changed, 139 insertions, 181 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 92860fb59..5690040a7 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -27,7 +27,6 @@ use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner};
27use crate::{ 27use crate::{
28 db::{AstDatabase, DefDatabase, HirDatabase}, 28 db::{AstDatabase, DefDatabase, HirDatabase},
29 expr::{BindingAnnotation, Body, BodySourceMap, ExprValidator, Pat, PatId}, 29 expr::{BindingAnnotation, Body, BodySourceMap, ExprValidator, Pat, PatId},
30 generics::{GenericDef, HasGenericParams},
31 ids::{ 30 ids::{
32 AstItemDef, ConstId, EnumId, FunctionId, MacroDefId, StaticId, StructId, TraitId, 31 AstItemDef, ConstId, EnumId, FunctionId, MacroDefId, StaticId, StructId, TraitId,
33 TypeAliasId, 32 TypeAliasId,
@@ -835,7 +834,7 @@ impl Trait {
835 // lifetime problems, but since there usually shouldn't be more than a 834 // lifetime problems, but since there usually shouldn't be more than a
836 // few direct traits this should be fine (we could even use some kind of 835 // few direct traits this should be fine (we could even use some kind of
837 // SmallVec if performance is a concern) 836 // SmallVec if performance is a concern)
838 self.generic_params(db) 837 db.generic_params(self.id.into())
839 .where_predicates 838 .where_predicates
840 .iter() 839 .iter()
841 .filter_map(|pred| match &pred.type_ref { 840 .filter_map(|pred| match &pred.type_ref {
@@ -975,16 +974,6 @@ pub enum AssocItem {
975// casting them, and somehow making the constructors private, which would be annoying. 974// casting them, and somehow making the constructors private, which would be annoying.
976impl_froms!(AssocItem: Function, Const, TypeAlias); 975impl_froms!(AssocItem: Function, Const, TypeAlias);
977 976
978impl From<AssocItem> for crate::generics::GenericDef {
979 fn from(item: AssocItem) -> Self {
980 match item {
981 AssocItem::Function(f) => f.into(),
982 AssocItem::Const(c) => c.into(),
983 AssocItem::TypeAlias(t) => t.into(),
984 }
985 }
986}
987
988impl AssocItem { 977impl AssocItem {
989 pub fn module(self, db: &impl DefDatabase) -> Module { 978 pub fn module(self, db: &impl DefDatabase) -> Module {
990 match self { 979 match self {
@@ -1004,6 +993,39 @@ impl AssocItem {
1004 } 993 }
1005} 994}
1006 995
996#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
997pub enum GenericDef {
998 Function(Function),
999 Adt(Adt),
1000 Trait(Trait),
1001 TypeAlias(TypeAlias),
1002 ImplBlock(ImplBlock),
1003 // enum variants cannot have generics themselves, but their parent enums
1004 // can, and this makes some code easier to write
1005 EnumVariant(EnumVariant),
1006 // consts can have type parameters from their parents (i.e. associated consts of traits)
1007 Const(Const),
1008}
1009impl_froms!(
1010 GenericDef: Function,
1011 Adt(Struct, Enum, Union),
1012 Trait,
1013 TypeAlias,
1014 ImplBlock,
1015 EnumVariant,
1016 Const
1017);
1018
1019impl From<AssocItem> for GenericDef {
1020 fn from(item: AssocItem) -> Self {
1021 match item {
1022 AssocItem::Function(f) => f.into(),
1023 AssocItem::Const(c) => c.into(),
1024 AssocItem::TypeAlias(t) => t.into(),
1025 }
1026 }
1027}
1028
1007#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] 1029#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
1008pub struct Local { 1030pub struct Local {
1009 pub(crate) parent: DefWithBody, 1031 pub(crate) parent: DefWithBody,
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs
index a9982a70f..ed0d68001 100644
--- a/crates/ra_hir/src/db.rs
+++ b/crates/ra_hir/src/db.rs
@@ -8,7 +8,6 @@ use ra_syntax::SmolStr;
8 8
9use crate::{ 9use crate::{
10 debug::HirDebugDatabase, 10 debug::HirDebugDatabase,
11 generics::GenericDef,
12 ids, 11 ids,
13 lang_item::{LangItemTarget, LangItems}, 12 lang_item::{LangItemTarget, LangItems},
14 ty::{ 13 ty::{
@@ -18,8 +17,8 @@ use crate::{
18 TypeCtor, 17 TypeCtor,
19 }, 18 },
20 type_alias::TypeAliasData, 19 type_alias::TypeAliasData,
21 Const, ConstData, Crate, DefWithBody, FnData, Function, ImplBlock, Module, Static, StructField, 20 Const, ConstData, Crate, DefWithBody, FnData, Function, GenericDef, ImplBlock, Module, Static,
22 Trait, TypeAlias, 21 StructField, Trait, TypeAlias,
23}; 22};
24 23
25pub use hir_def::db::{ 24pub use hir_def::db::{
diff --git a/crates/ra_hir/src/from_id.rs b/crates/ra_hir/src/from_id.rs
index a3e9d8525..e8ed04056 100644
--- a/crates/ra_hir/src/from_id.rs
+++ b/crates/ra_hir/src/from_id.rs
@@ -9,8 +9,9 @@ use hir_def::{
9}; 9};
10 10
11use crate::{ 11use crate::{
12 ty::TypableDef, Adt, AssocItem, Const, Crate, DefWithBody, EnumVariant, Function, GenericDef, 12 ty::{CallableDef, TypableDef},
13 ModuleDef, Static, TypeAlias, 13 Adt, AssocItem, Const, Crate, DefWithBody, EnumVariant, Function, GenericDef, ModuleDef,
14 Static, TypeAlias,
14}; 15};
15 16
16impl From<ra_db::CrateId> for Crate { 17impl From<ra_db::CrateId> for Crate {
@@ -206,3 +207,15 @@ impl From<Adt> for GenericDefId {
206 } 207 }
207 } 208 }
208} 209}
210
211impl From<CallableDef> for GenericDefId {
212 fn from(def: CallableDef) -> Self {
213 match def {
214 CallableDef::Function(it) => it.id.into(),
215 CallableDef::Struct(it) => it.id.into(),
216 CallableDef::EnumVariant(it) => {
217 EnumVariantId { parent: it.parent.id, local_id: it.id }.into()
218 }
219 }
220 }
221}
diff --git a/crates/ra_hir/src/generics.rs b/crates/ra_hir/src/generics.rs
deleted file mode 100644
index f1bf2ee9d..000000000
--- a/crates/ra_hir/src/generics.rs
+++ /dev/null
@@ -1,54 +0,0 @@
1//! Temp module to wrap hir_def::generics
2use std::sync::Arc;
3
4use crate::{
5 db::DefDatabase, Adt, Const, Container, Enum, EnumVariant, Function, ImplBlock, Struct, Trait,
6 TypeAlias, Union,
7};
8
9pub use hir_def::generics::{GenericParam, GenericParams, WherePredicate};
10
11#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
12pub enum GenericDef {
13 Function(Function),
14 Adt(Adt),
15 Trait(Trait),
16 TypeAlias(TypeAlias),
17 ImplBlock(ImplBlock),
18 // enum variants cannot have generics themselves, but their parent enums
19 // can, and this makes some code easier to write
20 EnumVariant(EnumVariant),
21 // consts can have type parameters from their parents (i.e. associated consts of traits)
22 Const(Const),
23}
24impl_froms!(
25 GenericDef: Function,
26 Adt(Struct, Enum, Union),
27 Trait,
28 TypeAlias,
29 ImplBlock,
30 EnumVariant,
31 Const
32);
33
34impl From<Container> for GenericDef {
35 fn from(c: Container) -> Self {
36 match c {
37 Container::Trait(trait_) => trait_.into(),
38 Container::ImplBlock(impl_block) => impl_block.into(),
39 }
40 }
41}
42
43pub trait HasGenericParams: Copy {
44 fn generic_params(self, db: &impl DefDatabase) -> Arc<GenericParams>;
45}
46
47impl<T> HasGenericParams for T
48where
49 T: Into<GenericDef> + Copy,
50{
51 fn generic_params(self, db: &impl DefDatabase) -> Arc<GenericParams> {
52 db.generic_params(self.into().into())
53 }
54}
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index 76c96bdcf..8c6834392 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -37,7 +37,6 @@ mod ty;
37mod impl_block; 37mod impl_block;
38mod expr; 38mod expr;
39mod lang_item; 39mod lang_item;
40pub mod generics;
41pub mod diagnostics; 40pub mod diagnostics;
42mod util; 41mod util;
43 42
@@ -57,13 +56,12 @@ pub use crate::{
57 docs::{DocDef, Docs, Documentation}, 56 docs::{DocDef, Docs, Documentation},
58 src::{HasBodySource, HasSource}, 57 src::{HasBodySource, HasSource},
59 Adt, AssocItem, Const, ConstData, Container, Crate, CrateDependency, DefWithBody, Enum, 58 Adt, AssocItem, Const, ConstData, Container, Crate, CrateDependency, DefWithBody, Enum,
60 EnumVariant, FieldSource, FnData, Function, GenericParam, HasBody, ImplBlock, Local, 59 EnumVariant, FieldSource, FnData, Function, GenericDef, GenericParam, HasBody, ImplBlock,
61 MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct, StructField, Trait, 60 Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct, StructField,
62 TypeAlias, Union, VariantDef, 61 Trait, TypeAlias, Union, VariantDef,
63 }, 62 },
64 expr::ExprScopes, 63 expr::ExprScopes,
65 from_source::FromSource, 64 from_source::FromSource,
66 generics::GenericDef,
67 ids::{HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFile}, 65 ids::{HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFile},
68 source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer}, 66 source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer},
69 ty::{ 67 ty::{
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs
index 36ece723f..95b8df181 100644
--- a/crates/ra_hir/src/ty.rs
+++ b/crates/ra_hir/src/ty.rs
@@ -17,12 +17,11 @@ use std::ops::Deref;
17use std::sync::Arc; 17use std::sync::Arc;
18use std::{fmt, iter, mem}; 18use std::{fmt, iter, mem};
19 19
20use hir_def::{generics::GenericParams, AdtId};
21
20use crate::{ 22use crate::{
21 db::HirDatabase, 23 db::HirDatabase, expr::ExprId, util::make_mut_slice, Adt, Crate, DefWithBody, FloatTy,
22 expr::ExprId, 24 GenericDef, IntTy, Mutability, Name, Trait, TypeAlias, Uncertain,
23 generics::{GenericParams, HasGenericParams},
24 util::make_mut_slice,
25 Adt, Crate, DefWithBody, FloatTy, IntTy, Mutability, Name, Trait, TypeAlias, Uncertain,
26}; 25};
27use display::{HirDisplay, HirFormatter}; 26use display::{HirDisplay, HirFormatter};
28 27
@@ -131,15 +130,15 @@ impl TypeCtor {
131 | TypeCtor::Closure { .. } // 1 param representing the signature of the closure 130 | TypeCtor::Closure { .. } // 1 param representing the signature of the closure
132 => 1, 131 => 1,
133 TypeCtor::Adt(adt) => { 132 TypeCtor::Adt(adt) => {
134 let generic_params = adt.generic_params(db); 133 let generic_params = db.generic_params(AdtId::from(adt).into());
135 generic_params.count_params_including_parent() 134 generic_params.count_params_including_parent()
136 } 135 }
137 TypeCtor::FnDef(callable) => { 136 TypeCtor::FnDef(callable) => {
138 let generic_params = callable.generic_params(db); 137 let generic_params = db.generic_params(callable.into());
139 generic_params.count_params_including_parent() 138 generic_params.count_params_including_parent()
140 } 139 }
141 TypeCtor::AssociatedType(type_alias) => { 140 TypeCtor::AssociatedType(type_alias) => {
142 let generic_params = type_alias.generic_params(db); 141 let generic_params = db.generic_params(type_alias.id.into());
143 generic_params.count_params_including_parent() 142 generic_params.count_params_including_parent()
144 } 143 }
145 TypeCtor::FnPtr { num_args } => num_args as usize + 1, 144 TypeCtor::FnPtr { num_args } => num_args as usize + 1,
@@ -168,7 +167,7 @@ impl TypeCtor {
168 } 167 }
169 } 168 }
170 169
171 pub fn as_generic_def(self) -> Option<crate::generics::GenericDef> { 170 pub fn as_generic_def(self) -> Option<crate::GenericDef> {
172 match self { 171 match self {
173 TypeCtor::Bool 172 TypeCtor::Bool
174 | TypeCtor::Char 173 | TypeCtor::Char
@@ -348,8 +347,9 @@ impl Substs {
348 ) 347 )
349 } 348 }
350 349
351 pub fn build_for_def(db: &impl HirDatabase, def: impl HasGenericParams) -> SubstsBuilder { 350 pub fn build_for_def(db: &impl HirDatabase, def: impl Into<GenericDef>) -> SubstsBuilder {
352 let params = def.generic_params(db); 351 let def = def.into();
352 let params = db.generic_params(def.into());
353 let param_count = params.count_params_including_parent(); 353 let param_count = params.count_params_including_parent();
354 Substs::builder(param_count) 354 Substs::builder(param_count)
355 } 355 }
diff --git a/crates/ra_hir/src/ty/autoderef.rs b/crates/ra_hir/src/ty/autoderef.rs
index 5d8518041..b60e4bb31 100644
--- a/crates/ra_hir/src/ty/autoderef.rs
+++ b/crates/ra_hir/src/ty/autoderef.rs
@@ -10,7 +10,7 @@ use hir_expand::name;
10use log::{info, warn}; 10use log::{info, warn};
11 11
12use super::{traits::Solution, Canonical, Substs, Ty, TypeWalk}; 12use super::{traits::Solution, Canonical, Substs, Ty, TypeWalk};
13use crate::{db::HirDatabase, generics::HasGenericParams}; 13use crate::db::HirDatabase;
14 14
15const AUTODEREF_RECURSION_LIMIT: usize = 10; 15const AUTODEREF_RECURSION_LIMIT: usize = 10;
16 16
@@ -46,7 +46,7 @@ fn deref_by_trait(
46 }; 46 };
47 let target = deref_trait.associated_type_by_name(db, &name::TARGET_TYPE)?; 47 let target = deref_trait.associated_type_by_name(db, &name::TARGET_TYPE)?;
48 48
49 let generic_params = target.generic_params(db); 49 let generic_params = db.generic_params(target.id.into());
50 if generic_params.count_params_including_parent() != 1 { 50 if generic_params.count_params_including_parent() != 1 {
51 // the Target type + Deref trait should only have one generic parameter, 51 // the Target type + Deref trait should only have one generic parameter,
52 // namely Deref's Self type 52 // namely Deref's Self type
diff --git a/crates/ra_hir/src/ty/infer/expr.rs b/crates/ra_hir/src/ty/infer/expr.rs
index ac570075f..20a7e9352 100644
--- a/crates/ra_hir/src/ty/infer/expr.rs
+++ b/crates/ra_hir/src/ty/infer/expr.rs
@@ -5,6 +5,7 @@ use std::sync::Arc;
5 5
6use hir_def::{ 6use hir_def::{
7 builtin_type::Signedness, 7 builtin_type::Signedness,
8 generics::GenericParams,
8 path::{GenericArg, GenericArgs}, 9 path::{GenericArg, GenericArgs},
9 resolver::resolver_for_expr, 10 resolver::resolver_for_expr,
10}; 11};
@@ -13,7 +14,6 @@ use hir_expand::name;
13use crate::{ 14use crate::{
14 db::HirDatabase, 15 db::HirDatabase,
15 expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, 16 expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
16 generics::{GenericParams, HasGenericParams},
17 ty::{ 17 ty::{
18 autoderef, method_resolution, op, CallableDef, InferTy, IntTy, Mutability, Namespace, 18 autoderef, method_resolution, op, CallableDef, InferTy, IntTy, Mutability, Namespace,
19 Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk, 19 Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk,
@@ -534,7 +534,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
534 ( 534 (
535 ty, 535 ty,
536 self.db.type_for_def(func.into(), Namespace::Values), 536 self.db.type_for_def(func.into(), Namespace::Values),
537 Some(func.generic_params(self.db)), 537 Some(self.db.generic_params(func.id.into())),
538 ) 538 )
539 } 539 }
540 None => (receiver_ty, Ty::Unknown, None), 540 None => (receiver_ty, Ty::Unknown, None),
@@ -645,7 +645,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
645 if let Some(trait_) = f.parent_trait(self.db) { 645 if let Some(trait_) = f.parent_trait(self.db) {
646 // construct a TraitDef 646 // construct a TraitDef
647 let substs = a_ty.parameters.prefix( 647 let substs = a_ty.parameters.prefix(
648 trait_.generic_params(self.db).count_params_including_parent(), 648 self.db
649 .generic_params(trait_.id.into())
650 .count_params_including_parent(),
649 ); 651 );
650 self.obligations.push(Obligation::Trait(TraitRef { trait_, substs })); 652 self.obligations.push(Obligation::Trait(TraitRef { trait_, substs }));
651 } 653 }
diff --git a/crates/ra_hir/src/ty/infer/path.rs b/crates/ra_hir/src/ty/infer/path.rs
index 70136e514..ee54d8217 100644
--- a/crates/ra_hir/src/ty/infer/path.rs
+++ b/crates/ra_hir/src/ty/infer/path.rs
@@ -7,7 +7,6 @@ use hir_def::{
7 7
8use crate::{ 8use crate::{
9 db::HirDatabase, 9 db::HirDatabase,
10 generics::HasGenericParams,
11 ty::{method_resolution, Namespace, Substs, Ty, TypableDef, TypeWalk}, 10 ty::{method_resolution, Namespace, Substs, Ty, TypableDef, TypeWalk},
12 AssocItem, Container, Function, Name, Path, 11 AssocItem, Container, Function, Name, Path,
13}; 12};
@@ -230,7 +229,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
230 if let ValueNs::FunctionId(func) = def { 229 if let ValueNs::FunctionId(func) = def {
231 let func = Function::from(*func); 230 let func = Function::from(*func);
232 // We only do the infer if parent has generic params 231 // We only do the infer if parent has generic params
233 let gen = func.generic_params(self.db); 232 let gen = self.db.generic_params(func.id.into());
234 if gen.count_parent_params() == 0 { 233 if gen.count_parent_params() == 0 {
235 return None; 234 return None;
236 } 235 }
diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs
index c6ad0811b..75c552569 100644
--- a/crates/ra_hir/src/ty/lower.rs
+++ b/crates/ra_hir/src/ty/lower.rs
@@ -10,10 +10,11 @@ use std::sync::Arc;
10 10
11use hir_def::{ 11use hir_def::{
12 builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType}, 12 builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType},
13 generics::WherePredicate,
13 path::{GenericArg, PathSegment}, 14 path::{GenericArg, PathSegment},
14 resolver::{HasResolver, Resolver, TypeNs}, 15 resolver::{HasResolver, Resolver, TypeNs},
15 type_ref::{TypeBound, TypeRef}, 16 type_ref::{TypeBound, TypeRef},
16 GenericDefId, 17 AdtId, GenericDefId,
17}; 18};
18 19
19use super::{ 20use super::{
@@ -22,15 +23,13 @@ use super::{
22}; 23};
23use crate::{ 24use crate::{
24 db::HirDatabase, 25 db::HirDatabase,
25 generics::HasGenericParams,
26 generics::{GenericDef, WherePredicate},
27 ty::{ 26 ty::{
28 primitive::{FloatTy, IntTy, Uncertain}, 27 primitive::{FloatTy, IntTy, Uncertain},
29 Adt, 28 Adt,
30 }, 29 },
31 util::make_mut_slice, 30 util::make_mut_slice,
32 Const, Enum, EnumVariant, Function, ImplBlock, ModuleDef, Path, Static, Struct, StructField, 31 Const, Enum, EnumVariant, Function, GenericDef, ImplBlock, ModuleDef, Path, Static, Struct,
33 Trait, TypeAlias, Union, VariantDef, 32 StructField, Trait, TypeAlias, Union, VariantDef,
34}; 33};
35 34
36// FIXME: this is only really used in `type_for_def`, which contains a bunch of 35// FIXME: this is only really used in `type_for_def`, which contains a bunch of
@@ -342,7 +341,7 @@ pub(super) fn substs_from_path_segment(
342 add_self_param: bool, 341 add_self_param: bool,
343) -> Substs { 342) -> Substs {
344 let mut substs = Vec::new(); 343 let mut substs = Vec::new();
345 let def_generics = def_generic.map(|def| def.generic_params(db)); 344 let def_generics = def_generic.map(|def| db.generic_params(def.into()));
346 345
347 let (parent_param_count, param_count) = 346 let (parent_param_count, param_count) =
348 def_generics.map_or((0, 0), |g| (g.count_parent_params(), g.params.len())); 347 def_generics.map_or((0, 0), |g| (g.count_parent_params(), g.params.len()));
@@ -443,7 +442,7 @@ impl TraitRef {
443 } 442 }
444 443
445 pub(crate) fn for_trait(db: &impl HirDatabase, trait_: Trait) -> TraitRef { 444 pub(crate) fn for_trait(db: &impl HirDatabase, trait_: Trait) -> TraitRef {
446 let substs = Substs::identity(&trait_.generic_params(db)); 445 let substs = Substs::identity(&db.generic_params(trait_.id.into()));
447 TraitRef { trait_, substs } 446 TraitRef { trait_, substs }
448 } 447 }
449 448
@@ -611,7 +610,7 @@ pub(crate) fn generic_predicates_query(
611/// Resolve the default type params from generics 610/// Resolve the default type params from generics
612pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDef) -> Substs { 611pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDef) -> Substs {
613 let resolver = GenericDefId::from(def).resolver(db); 612 let resolver = GenericDefId::from(def).resolver(db);
614 let generic_params = def.generic_params(db); 613 let generic_params = db.generic_params(def.into());
615 614
616 let defaults = generic_params 615 let defaults = generic_params
617 .params_including_parent() 616 .params_including_parent()
@@ -633,7 +632,7 @@ fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig {
633/// Build the declared type of a function. This should not need to look at the 632/// Build the declared type of a function. This should not need to look at the
634/// function body. 633/// function body.
635fn type_for_fn(db: &impl HirDatabase, def: Function) -> Ty { 634fn type_for_fn(db: &impl HirDatabase, def: Function) -> Ty {
636 let generics = def.generic_params(db); 635 let generics = db.generic_params(def.id.into());
637 let substs = Substs::identity(&generics); 636 let substs = Substs::identity(&generics);
638 Ty::apply(TypeCtor::FnDef(def.into()), substs) 637 Ty::apply(TypeCtor::FnDef(def.into()), substs)
639} 638}
@@ -716,7 +715,7 @@ fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty {
716 if struct_data.variant_data.fields().is_none() { 715 if struct_data.variant_data.fields().is_none() {
717 return type_for_adt(db, def); // Unit struct 716 return type_for_adt(db, def); // Unit struct
718 } 717 }
719 let generics = def.generic_params(db); 718 let generics = db.generic_params(def.id.into());
720 let substs = Substs::identity(&generics); 719 let substs = Substs::identity(&generics);
721 Ty::apply(TypeCtor::FnDef(def.into()), substs) 720 Ty::apply(TypeCtor::FnDef(def.into()), substs)
722} 721}
@@ -732,7 +731,7 @@ fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant)
732 .iter() 731 .iter()
733 .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref)) 732 .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref))
734 .collect::<Vec<_>>(); 733 .collect::<Vec<_>>();
735 let generics = def.parent_enum(db).generic_params(db); 734 let generics = db.generic_params(def.parent_enum(db).id.into());
736 let substs = Substs::identity(&generics); 735 let substs = Substs::identity(&generics);
737 let ret = type_for_adt(db, def.parent_enum(db)).subst(&substs); 736 let ret = type_for_adt(db, def.parent_enum(db)).subst(&substs);
738 FnSig::from_params_and_return(params, ret) 737 FnSig::from_params_and_return(params, ret)
@@ -744,18 +743,20 @@ fn type_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) ->
744 if var_data.fields().is_none() { 743 if var_data.fields().is_none() {
745 return type_for_adt(db, def.parent_enum(db)); // Unit variant 744 return type_for_adt(db, def.parent_enum(db)); // Unit variant
746 } 745 }
747 let generics = def.parent_enum(db).generic_params(db); 746 let generics = db.generic_params(def.parent_enum(db).id.into());
748 let substs = Substs::identity(&generics); 747 let substs = Substs::identity(&generics);
749 Ty::apply(TypeCtor::FnDef(def.into()), substs) 748 Ty::apply(TypeCtor::FnDef(def.into()), substs)
750} 749}
751 750
752fn type_for_adt(db: &impl HirDatabase, adt: impl Into<Adt> + HasGenericParams) -> Ty { 751fn type_for_adt(db: &impl HirDatabase, adt: impl Into<Adt>) -> Ty {
753 let generics = adt.generic_params(db); 752 let adt = adt.into();
754 Ty::apply(TypeCtor::Adt(adt.into()), Substs::identity(&generics)) 753 let adt_id: AdtId = adt.into();
754 let generics = db.generic_params(adt_id.into());
755 Ty::apply(TypeCtor::Adt(adt), Substs::identity(&generics))
755} 756}
756 757
757fn type_for_type_alias(db: &impl HirDatabase, t: TypeAlias) -> Ty { 758fn type_for_type_alias(db: &impl HirDatabase, t: TypeAlias) -> Ty {
758 let generics = t.generic_params(db); 759 let generics = db.generic_params(t.id.into());
759 let resolver = t.id.resolver(db); 760 let resolver = t.id.resolver(db);
760 let type_ref = t.type_ref(db); 761 let type_ref = t.type_ref(db);
761 let substs = Substs::identity(&generics); 762 let substs = Substs::identity(&generics);
diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs
index 9bf93981a..88785f305 100644
--- a/crates/ra_hir/src/ty/traits/chalk.rs
+++ b/crates/ra_hir/src/ty/traits/chalk.rs
@@ -16,10 +16,9 @@ use ra_db::salsa::{InternId, InternKey};
16use super::{AssocTyValue, Canonical, ChalkContext, Impl, Obligation}; 16use super::{AssocTyValue, Canonical, ChalkContext, Impl, Obligation};
17use crate::{ 17use crate::{
18 db::HirDatabase, 18 db::HirDatabase,
19 generics::{GenericDef, HasGenericParams},
20 ty::display::HirDisplay, 19 ty::display::HirDisplay,
21 ty::{ApplicationTy, GenericPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk}, 20 ty::{ApplicationTy, GenericPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk},
22 Crate, HasBody, ImplBlock, Trait, TypeAlias, 21 Crate, GenericDef, HasBody, ImplBlock, Trait, TypeAlias,
23}; 22};
24 23
25/// This represents a trait whose name we could not resolve. 24/// This represents a trait whose name we could not resolve.
@@ -509,7 +508,7 @@ pub(crate) fn associated_ty_data_query(
509 Some(crate::Container::Trait(t)) => t, 508 Some(crate::Container::Trait(t)) => t,
510 _ => panic!("associated type not in trait"), 509 _ => panic!("associated type not in trait"),
511 }; 510 };
512 let generic_params = type_alias.generic_params(db); 511 let generic_params = db.generic_params(type_alias.id.into());
513 let bound_data = chalk_rust_ir::AssociatedTyDatumBound { 512 let bound_data = chalk_rust_ir::AssociatedTyDatumBound {
514 // FIXME add bounds and where clauses 513 // FIXME add bounds and where clauses
515 bounds: vec![], 514 bounds: vec![],
@@ -550,7 +549,7 @@ pub(crate) fn trait_datum_query(
550 } 549 }
551 let trait_: Trait = from_chalk(db, trait_id); 550 let trait_: Trait = from_chalk(db, trait_id);
552 debug!("trait {:?} = {:?}", trait_id, trait_.name(db)); 551 debug!("trait {:?} = {:?}", trait_id, trait_.name(db));
553 let generic_params = trait_.generic_params(db); 552 let generic_params = db.generic_params(trait_.id.into());
554 let bound_vars = Substs::bound_vars(&generic_params); 553 let bound_vars = Substs::bound_vars(&generic_params);
555 let flags = chalk_rust_ir::TraitFlags { 554 let flags = chalk_rust_ir::TraitFlags {
556 auto: trait_.is_auto(db), 555 auto: trait_.is_auto(db),
@@ -594,7 +593,7 @@ pub(crate) fn struct_datum_query(
594 let where_clauses = type_ctor 593 let where_clauses = type_ctor
595 .as_generic_def() 594 .as_generic_def()
596 .map(|generic_def| { 595 .map(|generic_def| {
597 let generic_params = generic_def.generic_params(db); 596 let generic_params = db.generic_params(generic_def.into());
598 let bound_vars = Substs::bound_vars(&generic_params); 597 let bound_vars = Substs::bound_vars(&generic_params);
599 convert_where_clauses(db, generic_def, &bound_vars) 598 convert_where_clauses(db, generic_def, &bound_vars)
600 }) 599 })
@@ -634,7 +633,7 @@ fn impl_block_datum(
634 impl_id: ImplId, 633 impl_id: ImplId,
635 impl_block: ImplBlock, 634 impl_block: ImplBlock,
636) -> Option<Arc<ImplDatum<ChalkIr>>> { 635) -> Option<Arc<ImplDatum<ChalkIr>>> {
637 let generic_params = impl_block.generic_params(db); 636 let generic_params = db.generic_params(impl_block.id.into());
638 let bound_vars = Substs::bound_vars(&generic_params); 637 let bound_vars = Substs::bound_vars(&generic_params);
639 let trait_ref = impl_block.target_trait_ref(db)?.subst(&bound_vars); 638 let trait_ref = impl_block.target_trait_ref(db)?.subst(&bound_vars);
640 let trait_ = trait_ref.trait_; 639 let trait_ = trait_ref.trait_;
@@ -786,7 +785,7 @@ fn type_alias_associated_ty_value(
786 let assoc_ty = trait_ 785 let assoc_ty = trait_
787 .associated_type_by_name(db, &type_alias.name(db)) 786 .associated_type_by_name(db, &type_alias.name(db))
788 .expect("assoc ty value should not exist"); // validated when building the impl data as well 787 .expect("assoc ty value should not exist"); // validated when building the impl data as well
789 let generic_params = impl_block.generic_params(db); 788 let generic_params = db.generic_params(impl_block.id.into());
790 let bound_vars = Substs::bound_vars(&generic_params); 789 let bound_vars = Substs::bound_vars(&generic_params);
791 let ty = db.type_for_def(type_alias.into(), crate::ty::Namespace::Types).subst(&bound_vars); 790 let ty = db.type_for_def(type_alias.into(), crate::ty::Namespace::Types).subst(&bound_vars);
792 let value_bound = chalk_rust_ir::AssociatedTyValueBound { ty: ty.to_chalk(db) }; 791 let value_bound = chalk_rust_ir::AssociatedTyValueBound { ty: ty.to_chalk(db) };
diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs
index 840785baa..7b5c3ec06 100644
--- a/crates/ra_hir_def/src/resolver.rs
+++ b/crates/ra_hir_def/src/resolver.rs
@@ -18,7 +18,7 @@ use crate::{
18 path::{Path, PathKind}, 18 path::{Path, PathKind},
19 AdtId, AstItemDef, ConstId, ContainerId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, 19 AdtId, AstItemDef, ConstId, ContainerId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId,
20 FunctionId, GenericDefId, ImplId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId, 20 FunctionId, GenericDefId, ImplId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId,
21 TypeAliasId, UnionId, 21 TypeAliasId,
22}; 22};
23 23
24#[derive(Debug, Clone, Default)] 24#[derive(Debug, Clone, Default)]
@@ -369,47 +369,6 @@ impl Resolver {
369 } 369 }
370} 370}
371 371
372impl Resolver {
373 pub(crate) fn push_scope(mut self, scope: Scope) -> Resolver {
374 self.scopes.push(scope);
375 self
376 }
377
378 pub(crate) fn push_generic_params_scope(
379 self,
380 db: &impl DefDatabase2,
381 def: GenericDefId,
382 ) -> Resolver {
383 let params = db.generic_params(def);
384 if params.params.is_empty() {
385 self
386 } else {
387 self.push_scope(Scope::GenericParams { def, params })
388 }
389 }
390
391 pub(crate) fn push_impl_block_scope(self, impl_block: ImplId) -> Resolver {
392 self.push_scope(Scope::ImplBlockScope(impl_block))
393 }
394
395 pub(crate) fn push_module_scope(
396 self,
397 crate_def_map: Arc<CrateDefMap>,
398 module_id: CrateModuleId,
399 ) -> Resolver {
400 self.push_scope(Scope::ModuleScope(ModuleItemMap { crate_def_map, module_id }))
401 }
402
403 pub(crate) fn push_expr_scope(
404 self,
405 owner: DefWithBodyId,
406 expr_scopes: Arc<ExprScopes>,
407 scope_id: ScopeId,
408 ) -> Resolver {
409 self.push_scope(Scope::ExprScope(ExprScope { owner, expr_scopes, scope_id }))
410 }
411}
412
413pub enum ScopeDef { 372pub enum ScopeDef {
414 PerNs(PerNs), 373 PerNs(PerNs),
415 ImplSelfType(ImplId), 374 ImplSelfType(ImplId),
@@ -489,6 +448,43 @@ pub fn resolver_for_scope(
489 r 448 r
490} 449}
491 450
451impl Resolver {
452 fn push_scope(mut self, scope: Scope) -> Resolver {
453 self.scopes.push(scope);
454 self
455 }
456
457 fn push_generic_params_scope(self, db: &impl DefDatabase2, def: GenericDefId) -> Resolver {
458 let params = db.generic_params(def);
459 if params.params.is_empty() {
460 self
461 } else {
462 self.push_scope(Scope::GenericParams { def, params })
463 }
464 }
465
466 fn push_impl_block_scope(self, impl_block: ImplId) -> Resolver {
467 self.push_scope(Scope::ImplBlockScope(impl_block))
468 }
469
470 fn push_module_scope(
471 self,
472 crate_def_map: Arc<CrateDefMap>,
473 module_id: CrateModuleId,
474 ) -> Resolver {
475 self.push_scope(Scope::ModuleScope(ModuleItemMap { crate_def_map, module_id }))
476 }
477
478 fn push_expr_scope(
479 self,
480 owner: DefWithBodyId,
481 expr_scopes: Arc<ExprScopes>,
482 scope_id: ScopeId,
483 ) -> Resolver {
484 self.push_scope(Scope::ExprScope(ExprScope { owner, expr_scopes, scope_id }))
485 }
486}
487
492pub trait HasResolver { 488pub trait HasResolver {
493 /// Builds a resolver for type references inside this def. 489 /// Builds a resolver for type references inside this def.
494 fn resolver(self, db: &impl DefDatabase2) -> Resolver; 490 fn resolver(self, db: &impl DefDatabase2) -> Resolver;
@@ -507,9 +503,10 @@ impl HasResolver for TraitId {
507 } 503 }
508} 504}
509 505
510impl HasResolver for AdtId { 506impl<T: Into<AdtId>> HasResolver for T {
511 fn resolver(self, db: &impl DefDatabase2) -> Resolver { 507 fn resolver(self, db: &impl DefDatabase2) -> Resolver {
512 let module = match self { 508 let def = self.into();
509 let module = match def {
513 AdtId::StructId(it) => it.0.module(db), 510 AdtId::StructId(it) => it.0.module(db),
514 AdtId::UnionId(it) => it.0.module(db), 511 AdtId::UnionId(it) => it.0.module(db),
515 AdtId::EnumId(it) => it.module(db), 512 AdtId::EnumId(it) => it.module(db),
@@ -517,26 +514,8 @@ impl HasResolver for AdtId {
517 514
518 module 515 module
519 .resolver(db) 516 .resolver(db)
520 .push_generic_params_scope(db, self.into()) 517 .push_generic_params_scope(db, def.into())
521 .push_scope(Scope::AdtScope(self.into())) 518 .push_scope(Scope::AdtScope(def))
522 }
523}
524
525impl HasResolver for StructId {
526 fn resolver(self, db: &impl DefDatabase2) -> Resolver {
527 AdtId::from(self).resolver(db)
528 }
529}
530
531impl HasResolver for UnionId {
532 fn resolver(self, db: &impl DefDatabase2) -> Resolver {
533 AdtId::from(self).resolver(db)
534 }
535}
536
537impl HasResolver for EnumId {
538 fn resolver(self, db: &impl DefDatabase2) -> Resolver {
539 AdtId::from(self).resolver(db)
540 } 519 }
541} 520}
542 521