aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_ty')
-rw-r--r--crates/ra_hir_ty/src/db.rs7
-rw-r--r--crates/ra_hir_ty/src/infer.rs9
-rw-r--r--crates/ra_hir_ty/src/infer/coerce.rs8
-rw-r--r--crates/ra_hir_ty/src/infer/expr.rs4
-rw-r--r--crates/ra_hir_ty/src/infer/path.rs5
-rw-r--r--crates/ra_hir_ty/src/lib.rs5
-rw-r--r--crates/ra_hir_ty/src/lower.rs8
-rw-r--r--crates/ra_hir_ty/src/traits/chalk.rs11
-rw-r--r--crates/ra_hir_ty/src/utils.rs63
9 files changed, 82 insertions, 38 deletions
diff --git a/crates/ra_hir_ty/src/db.rs b/crates/ra_hir_ty/src/db.rs
index 21ab22fa9..e9bfcfa17 100644
--- a/crates/ra_hir_ty/src/db.rs
+++ b/crates/ra_hir_ty/src/db.rs
@@ -3,7 +3,8 @@
3use std::sync::Arc; 3use std::sync::Arc;
4 4
5use hir_def::{ 5use hir_def::{
6 db::DefDatabase, DefWithBodyId, GenericDefId, ImplId, LocalStructFieldId, TraitId, VariantId, TypeParamId, 6 db::DefDatabase, DefWithBodyId, GenericDefId, ImplId, LocalStructFieldId, TraitId, TypeParamId,
7 VariantId,
7}; 8};
8use ra_arena::map::ArenaMap; 9use ra_arena::map::ArenaMap;
9use ra_db::{impl_intern_key, salsa, CrateId}; 10use ra_db::{impl_intern_key, salsa, CrateId};
@@ -12,8 +13,8 @@ use ra_prof::profile;
12use crate::{ 13use crate::{
13 method_resolution::CrateImplBlocks, 14 method_resolution::CrateImplBlocks,
14 traits::{chalk, AssocTyValue, Impl}, 15 traits::{chalk, AssocTyValue, Impl},
15 CallableDef, PolyFnSig, GenericPredicate, InferenceResult, Substs, TraitRef, Ty, TyDefId, TypeCtor, 16 Binders, CallableDef, GenericPredicate, InferenceResult, PolyFnSig, Substs, TraitRef, Ty,
16 ValueTyDefId, Binders, 17 TyDefId, TypeCtor, ValueTyDefId,
17}; 18};
18 19
19#[salsa::query_group(HirDatabaseStorage)] 20#[salsa::query_group(HirDatabaseStorage)]
diff --git a/crates/ra_hir_ty/src/infer.rs b/crates/ra_hir_ty/src/infer.rs
index dec6bd84c..a9d958c8b 100644
--- a/crates/ra_hir_ty/src/infer.rs
+++ b/crates/ra_hir_ty/src/infer.rs
@@ -278,7 +278,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
278 impl_trait_mode: ImplTraitLoweringMode, 278 impl_trait_mode: ImplTraitLoweringMode,
279 ) -> Ty { 279 ) -> Ty {
280 // FIXME use right resolver for block 280 // FIXME use right resolver for block
281 let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver).with_impl_trait_mode(impl_trait_mode); 281 let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver)
282 .with_impl_trait_mode(impl_trait_mode);
282 let ty = Ty::from_hir(&ctx, type_ref); 283 let ty = Ty::from_hir(&ctx, type_ref);
283 let ty = self.insert_type_vars(ty); 284 let ty = self.insert_type_vars(ty);
284 self.normalize_associated_types_in(ty) 285 self.normalize_associated_types_in(ty)
@@ -455,8 +456,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
455 456
456 fn collect_fn(&mut self, data: &FunctionData) { 457 fn collect_fn(&mut self, data: &FunctionData) {
457 let body = Arc::clone(&self.body); // avoid borrow checker problem 458 let body = Arc::clone(&self.body); // avoid borrow checker problem
458 let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver).with_impl_trait_mode(ImplTraitLoweringMode::Param); 459 let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver)
459 let param_tys = data.params.iter().map(|type_ref| Ty::from_hir(&ctx, type_ref)).collect::<Vec<_>>(); 460 .with_impl_trait_mode(ImplTraitLoweringMode::Param);
461 let param_tys =
462 data.params.iter().map(|type_ref| Ty::from_hir(&ctx, type_ref)).collect::<Vec<_>>();
460 for (ty, pat) in param_tys.into_iter().zip(body.params.iter()) { 463 for (ty, pat) in param_tys.into_iter().zip(body.params.iter()) {
461 let ty = self.insert_type_vars(ty); 464 let ty = self.insert_type_vars(ty);
462 let ty = self.normalize_associated_types_in(ty); 465 let ty = self.normalize_associated_types_in(ty);
diff --git a/crates/ra_hir_ty/src/infer/coerce.rs b/crates/ra_hir_ty/src/infer/coerce.rs
index 2a9567898..f68a1439f 100644
--- a/crates/ra_hir_ty/src/infer/coerce.rs
+++ b/crates/ra_hir_ty/src/infer/coerce.rs
@@ -66,9 +66,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
66 // This works for smart-pointer-like coercion, which covers all impls from std. 66 // This works for smart-pointer-like coercion, which covers all impls from std.
67 st1.iter().zip(st2.iter()).enumerate().find_map(|(i, (ty1, ty2))| { 67 st1.iter().zip(st2.iter()).enumerate().find_map(|(i, (ty1, ty2))| {
68 match (ty1, ty2) { 68 match (ty1, ty2) {
69 (Ty::Bound(idx1), Ty::Bound(idx2)) 69 (Ty::Bound(idx1), Ty::Bound(idx2)) if idx1 != idx2 => {
70 if idx1 != idx2 =>
71 {
72 Some(((*ctor1, *ctor2), i)) 70 Some(((*ctor1, *ctor2), i))
73 } 71 }
74 _ => None, 72 _ => None,
@@ -277,9 +275,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
277 let mut multiple_used = false; 275 let mut multiple_used = false;
278 fields.for_each(|(field_id, _data)| { 276 fields.for_each(|(field_id, _data)| {
279 field_tys[field_id].value.walk(&mut |ty| match ty { 277 field_tys[field_id].value.walk(&mut |ty| match ty {
280 &Ty::Bound(idx) if idx == unsize_generic_index => { 278 &Ty::Bound(idx) if idx == unsize_generic_index => multiple_used = true,
281 multiple_used = true
282 }
283 _ => {} 279 _ => {}
284 }) 280 })
285 }); 281 });
diff --git a/crates/ra_hir_ty/src/infer/expr.rs b/crates/ra_hir_ty/src/infer/expr.rs
index 6222bd90e..8c360bcad 100644
--- a/crates/ra_hir_ty/src/infer/expr.rs
+++ b/crates/ra_hir_ty/src/infer/expr.rs
@@ -19,8 +19,8 @@ use crate::{
19 method_resolution, op, 19 method_resolution, op,
20 traits::InEnvironment, 20 traits::InEnvironment,
21 utils::{generics, variant_data, Generics}, 21 utils::{generics, variant_data, Generics},
22 ApplicationTy, CallableDef, InferTy, IntTy, Mutability, Obligation, Substs, TraitRef, Ty, 22 ApplicationTy, Binders, CallableDef, InferTy, IntTy, Mutability, Obligation, Substs, TraitRef,
23 TypeCtor, Uncertain, Binders, 23 Ty, TypeCtor, Uncertain,
24}; 24};
25 25
26use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch}; 26use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch};
diff --git a/crates/ra_hir_ty/src/infer/path.rs b/crates/ra_hir_ty/src/infer/path.rs
index fcf13b0b3..686ce7a21 100644
--- a/crates/ra_hir_ty/src/infer/path.rs
+++ b/crates/ra_hir_ty/src/infer/path.rs
@@ -9,10 +9,7 @@ use hir_def::{
9}; 9};
10use hir_expand::name::Name; 10use hir_expand::name::Name;
11 11
12use crate::{ 12use crate::{db::HirDatabase, method_resolution, Substs, Ty, ValueTyDefId};
13 db::HirDatabase, method_resolution, Substs, Ty,
14 ValueTyDefId
15};
16 13
17use super::{ExprOrPatId, InferenceContext, TraitRef}; 14use super::{ExprOrPatId, InferenceContext, TraitRef};
18 15
diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs
index 314be17b8..60c7fd0e5 100644
--- a/crates/ra_hir_ty/src/lib.rs
+++ b/crates/ra_hir_ty/src/lib.rs
@@ -1033,7 +1033,10 @@ impl HirDisplay for Ty {
1033 write!(f, "impl ")?; 1033 write!(f, "impl ")?;
1034 let bounds = f.db.generic_predicates_for_param(*id); 1034 let bounds = f.db.generic_predicates_for_param(*id);
1035 let substs = Substs::type_params_for_generics(&generics); 1035 let substs = Substs::type_params_for_generics(&generics);
1036 write_bounds_like_dyn_trait(&bounds.iter().map(|b| b.clone().subst(&substs)).collect::<Vec<_>>(), f)?; 1036 write_bounds_like_dyn_trait(
1037 &bounds.iter().map(|b| b.clone().subst(&substs)).collect::<Vec<_>>(),
1038 f,
1039 )?;
1037 } 1040 }
1038 } 1041 }
1039 } 1042 }
diff --git a/crates/ra_hir_ty/src/lower.rs b/crates/ra_hir_ty/src/lower.rs
index 0d4c075af..4168e7509 100644
--- a/crates/ra_hir_ty/src/lower.rs
+++ b/crates/ra_hir_ty/src/lower.rs
@@ -276,7 +276,9 @@ impl Ty {
276 TypeNs::SelfType(impl_id) => { 276 TypeNs::SelfType(impl_id) => {
277 let generics = generics(ctx.db, impl_id.into()); 277 let generics = generics(ctx.db, impl_id.into());
278 let substs = match ctx.type_param_mode { 278 let substs = match ctx.type_param_mode {
279 TypeParamLoweringMode::Placeholder => Substs::type_params_for_generics(&generics), 279 TypeParamLoweringMode::Placeholder => {
280 Substs::type_params_for_generics(&generics)
281 }
280 TypeParamLoweringMode::Variable => Substs::bound_vars(&generics), 282 TypeParamLoweringMode::Variable => Substs::bound_vars(&generics),
281 }; 283 };
282 ctx.db.impl_self_ty(impl_id).subst(&substs) 284 ctx.db.impl_self_ty(impl_id).subst(&substs)
@@ -284,7 +286,9 @@ impl Ty {
284 TypeNs::AdtSelfType(adt) => { 286 TypeNs::AdtSelfType(adt) => {
285 let generics = generics(ctx.db, adt.into()); 287 let generics = generics(ctx.db, adt.into());
286 let substs = match ctx.type_param_mode { 288 let substs = match ctx.type_param_mode {
287 TypeParamLoweringMode::Placeholder => Substs::type_params_for_generics(&generics), 289 TypeParamLoweringMode::Placeholder => {
290 Substs::type_params_for_generics(&generics)
291 }
288 TypeParamLoweringMode::Variable => Substs::bound_vars(&generics), 292 TypeParamLoweringMode::Variable => Substs::bound_vars(&generics),
289 }; 293 };
290 ctx.db.ty(adt.into()).subst(&substs) 294 ctx.db.ty(adt.into()).subst(&substs)
diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs
index 8260bd157..4974c565b 100644
--- a/crates/ra_hir_ty/src/traits/chalk.rs
+++ b/crates/ra_hir_ty/src/traits/chalk.rs
@@ -144,8 +144,11 @@ impl ToChalk for Ty {
144 } 144 }
145 Ty::Param(id) => { 145 Ty::Param(id) => {
146 let interned_id = db.intern_type_param_id(id); 146 let interned_id = db.intern_type_param_id(id);
147 PlaceholderIndex { ui: UniverseIndex::ROOT, idx: interned_id.as_intern_id().as_usize() } 147 PlaceholderIndex {
148 .to_ty::<TypeFamily>() 148 ui: UniverseIndex::ROOT,
149 idx: interned_id.as_intern_id().as_usize(),
150 }
151 .to_ty::<TypeFamily>()
149 } 152 }
150 Ty::Bound(idx) => chalk_ir::TyData::BoundVar(idx as usize).intern(), 153 Ty::Bound(idx) => chalk_ir::TyData::BoundVar(idx as usize).intern(),
151 Ty::Infer(_infer_ty) => panic!("uncanonicalized infer ty"), 154 Ty::Infer(_infer_ty) => panic!("uncanonicalized infer ty"),
@@ -178,7 +181,9 @@ impl ToChalk for Ty {
178 }, 181 },
179 chalk_ir::TyData::Placeholder(idx) => { 182 chalk_ir::TyData::Placeholder(idx) => {
180 assert_eq!(idx.ui, UniverseIndex::ROOT); 183 assert_eq!(idx.ui, UniverseIndex::ROOT);
181 let interned_id = crate::db::GlobalTypeParamId::from_intern_id(crate::salsa::InternId::from(idx.idx)); 184 let interned_id = crate::db::GlobalTypeParamId::from_intern_id(
185 crate::salsa::InternId::from(idx.idx),
186 );
182 Ty::Param(db.lookup_intern_type_param_id(interned_id)) 187 Ty::Param(db.lookup_intern_type_param_id(interned_id))
183 } 188 }
184 chalk_ir::TyData::Alias(proj) => { 189 chalk_ir::TyData::Alias(proj) => {
diff --git a/crates/ra_hir_ty/src/utils.rs b/crates/ra_hir_ty/src/utils.rs
index 8fa1838bd..e307d958d 100644
--- a/crates/ra_hir_ty/src/utils.rs
+++ b/crates/ra_hir_ty/src/utils.rs
@@ -2,6 +2,7 @@
2//! query, but can't be computed directly from `*Data` (ie, which need a `db`). 2//! query, but can't be computed directly from `*Data` (ie, which need a `db`).
3use std::sync::Arc; 3use std::sync::Arc;
4 4
5use hir_def::generics::WherePredicateTarget;
5use hir_def::{ 6use hir_def::{
6 adt::VariantData, 7 adt::VariantData,
7 db::DefDatabase, 8 db::DefDatabase,
@@ -12,7 +13,6 @@ use hir_def::{
12 AssocContainerId, GenericDefId, Lookup, TraitId, TypeAliasId, TypeParamId, VariantId, 13 AssocContainerId, GenericDefId, Lookup, TraitId, TypeAliasId, TypeParamId, VariantId,
13}; 14};
14use hir_expand::name::{name, Name}; 15use hir_expand::name::{name, Name};
15use hir_def::generics::WherePredicateTarget;
16 16
17fn direct_super_traits(db: &impl DefDatabase, trait_: TraitId) -> Vec<TraitId> { 17fn direct_super_traits(db: &impl DefDatabase, trait_: TraitId) -> Vec<TraitId> {
18 let resolver = trait_.resolver(db); 18 let resolver = trait_.resolver(db);
@@ -26,8 +26,12 @@ fn direct_super_traits(db: &impl DefDatabase, trait_: TraitId) -> Vec<TraitId> {
26 .where_predicates 26 .where_predicates
27 .iter() 27 .iter()
28 .filter_map(|pred| match &pred.target { 28 .filter_map(|pred| match &pred.target {
29 WherePredicateTarget::TypeRef(TypeRef::Path(p)) if p == &Path::from(name![Self]) => pred.bound.as_path(), 29 WherePredicateTarget::TypeRef(TypeRef::Path(p)) if p == &Path::from(name![Self]) => {
30 WherePredicateTarget::TypeParam(local_id) if Some(*local_id) == trait_self => pred.bound.as_path(), 30 pred.bound.as_path()
31 }
32 WherePredicateTarget::TypeParam(local_id) if Some(*local_id) == trait_self => {
33 pred.bound.as_path()
34 }
31 _ => None, 35 _ => None,
32 }) 36 })
33 .filter_map(|path| match resolver.resolve_path_in_type_ns_fully(db, path.mod_path()) { 37 .filter_map(|path| match resolver.resolve_path_in_type_ns_fully(db, path.mod_path()) {
@@ -99,19 +103,35 @@ pub(crate) struct Generics {
99} 103}
100 104
101impl Generics { 105impl Generics {
102 pub(crate) fn iter<'a>(&'a self) -> impl Iterator<Item = (TypeParamId, &'a TypeParamData)> + 'a { 106 pub(crate) fn iter<'a>(
107 &'a self,
108 ) -> impl Iterator<Item = (TypeParamId, &'a TypeParamData)> + 'a {
103 self.parent_generics 109 self.parent_generics
104 .as_ref() 110 .as_ref()
105 .into_iter() 111 .into_iter()
106 .flat_map(|it| it.params.types.iter().map(move |(local_id, p)| (TypeParamId { parent: it.def, local_id }, p))) 112 .flat_map(|it| {
107 .chain(self.params.types.iter().map(move |(local_id, p)| (TypeParamId { parent: self.def, local_id }, p))) 113 it.params
114 .types
115 .iter()
116 .map(move |(local_id, p)| (TypeParamId { parent: it.def, local_id }, p))
117 })
118 .chain(
119 self.params
120 .types
121 .iter()
122 .map(move |(local_id, p)| (TypeParamId { parent: self.def, local_id }, p)),
123 )
108 } 124 }
109 125
110 pub(crate) fn iter_parent<'a>(&'a self) -> impl Iterator<Item = (TypeParamId, &'a TypeParamData)> + 'a { 126 pub(crate) fn iter_parent<'a>(
111 self.parent_generics 127 &'a self,
112 .as_ref() 128 ) -> impl Iterator<Item = (TypeParamId, &'a TypeParamData)> + 'a {
113 .into_iter() 129 self.parent_generics.as_ref().into_iter().flat_map(|it| {
114 .flat_map(|it| it.params.types.iter().map(move |(local_id, p)| (TypeParamId { parent: it.def, local_id }, p))) 130 it.params
131 .types
132 .iter()
133 .map(move |(local_id, p)| (TypeParamId { parent: it.def, local_id }, p))
134 })
115 } 135 }
116 136
117 pub(crate) fn len(&self) -> usize { 137 pub(crate) fn len(&self) -> usize {
@@ -127,9 +147,24 @@ impl Generics {
127 147
128 /// (self, type param list, impl trait) 148 /// (self, type param list, impl trait)
129 pub(crate) fn provenance_split(&self) -> (usize, usize, usize) { 149 pub(crate) fn provenance_split(&self) -> (usize, usize, usize) {
130 let self_params = self.params.types.iter().filter(|(_, p)| p.provenance == TypeParamProvenance::TraitSelf).count(); 150 let self_params = self
131 let list_params = self.params.types.iter().filter(|(_, p)| p.provenance == TypeParamProvenance::TypeParamList).count(); 151 .params
132 let impl_trait_params = self.params.types.iter().filter(|(_, p)| p.provenance == TypeParamProvenance::ArgumentImplTrait).count(); 152 .types
153 .iter()
154 .filter(|(_, p)| p.provenance == TypeParamProvenance::TraitSelf)
155 .count();
156 let list_params = self
157 .params
158 .types
159 .iter()
160 .filter(|(_, p)| p.provenance == TypeParamProvenance::TypeParamList)
161 .count();
162 let impl_trait_params = self
163 .params
164 .types
165 .iter()
166 .filter(|(_, p)| p.provenance == TypeParamProvenance::ArgumentImplTrait)
167 .count();
133 (self_params, list_params, impl_trait_params) 168 (self_params, list_params, impl_trait_params)
134 } 169 }
135 170