aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir/src/code_model.rs25
-rw-r--r--crates/ra_hir_ty/src/lib.rs10
-rw-r--r--crates/ra_hir_ty/src/lower.rs4
3 files changed, 25 insertions, 14 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 817c27410..63c85ca34 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -13,6 +13,7 @@ use hir_def::{
13 AdtId, ConstId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId, LocalEnumVariantId, 13 AdtId, ConstId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId, LocalEnumVariantId,
14 LocalModuleId, LocalStructFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, 14 LocalModuleId, LocalStructFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
15 TypeParamId, UnionId, 15 TypeParamId, UnionId,
16 GenericDefId
16}; 17};
17use hir_expand::{ 18use hir_expand::{
18 diagnostics::DiagnosticSink, 19 diagnostics::DiagnosticSink,
@@ -21,7 +22,8 @@ use hir_expand::{
21}; 22};
22use hir_ty::{ 23use hir_ty::{
23 autoderef, display::HirFormatter, expr::ExprValidator, method_resolution, ApplicationTy, 24 autoderef, display::HirFormatter, expr::ExprValidator, method_resolution, ApplicationTy,
24 Canonical, InEnvironment, TraitEnvironment, Ty, TyDefId, TypeCtor, TypeWalk, 25 Canonical, InEnvironment, TraitEnvironment, Ty, TyDefId, TypeCtor,
26 Substs
25}; 27};
26use ra_db::{CrateId, Edition, FileId}; 28use ra_db::{CrateId, Edition, FileId};
27use ra_prof::profile; 29use ra_prof::profile;
@@ -270,7 +272,13 @@ impl StructField {
270 272
271 pub fn ty(&self, db: &impl HirDatabase) -> Type { 273 pub fn ty(&self, db: &impl HirDatabase) -> Type {
272 let var_id = self.parent.into(); 274 let var_id = self.parent.into();
273 let ty = db.field_types(var_id)[self.id].clone(); 275 let generic_def_id: GenericDefId = match self.parent {
276 VariantDef::Struct(it) => it.id.into(),
277 VariantDef::Union(it) => it.id.into(),
278 VariantDef::EnumVariant(it) => it.parent.id.into(),
279 };
280 let substs = Substs::type_params(db, generic_def_id);
281 let ty = db.field_types(var_id)[self.id].clone().subst(&substs);
274 Type::new(db, self.parent.module(db).id.krate.into(), var_id, ty) 282 Type::new(db, self.parent.module(db).id.krate.into(), var_id, ty)
275 } 283 }
276 284
@@ -789,11 +797,7 @@ impl ImplBlock {
789 pub fn target_ty(&self, db: &impl HirDatabase) -> Type { 797 pub fn target_ty(&self, db: &impl HirDatabase) -> Type {
790 let impl_data = db.impl_data(self.id); 798 let impl_data = db.impl_data(self.id);
791 let resolver = self.id.resolver(db); 799 let resolver = self.id.resolver(db);
792 let ctx = hir_ty::TyLoweringContext { 800 let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
793 db,
794 resolver: &resolver,
795 impl_trait_mode: hir_ty::ImplTraitLoweringMode::Disallowed,
796 };
797 let environment = TraitEnvironment::lower(db, &resolver); 801 let environment = TraitEnvironment::lower(db, &resolver);
798 let ty = Ty::from_hir(&ctx, &impl_data.target_type); 802 let ty = Ty::from_hir(&ctx, &impl_data.target_type);
799 Type { 803 Type {
@@ -856,9 +860,10 @@ impl Type {
856 fn from_def( 860 fn from_def(
857 db: &impl HirDatabase, 861 db: &impl HirDatabase,
858 krate: CrateId, 862 krate: CrateId,
859 def: impl HasResolver + Into<TyDefId>, 863 def: impl HasResolver + Into<TyDefId> + Into<GenericDefId>,
860 ) -> Type { 864 ) -> Type {
861 let ty = db.ty(def.into()); 865 let substs = Substs::type_params(db, def);
866 let ty = db.ty(def.into()).subst(&substs);
862 Type::new(db, krate, def, ty) 867 Type::new(db, krate, def, ty)
863 } 868 }
864 869
@@ -955,7 +960,7 @@ impl Type {
955 match a_ty.ctor { 960 match a_ty.ctor {
956 TypeCtor::Tuple { .. } => { 961 TypeCtor::Tuple { .. } => {
957 for ty in a_ty.parameters.iter() { 962 for ty in a_ty.parameters.iter() {
958 let ty = ty.clone().subst(&a_ty.parameters); 963 let ty = ty.clone();
959 res.push(self.derived(ty)); 964 res.push(self.derived(ty));
960 } 965 }
961 } 966 }
diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs
index a685e70c2..314be17b8 100644
--- a/crates/ra_hir_ty/src/lib.rs
+++ b/crates/ra_hir_ty/src/lib.rs
@@ -361,10 +361,16 @@ impl Substs {
361 } 361 }
362 362
363 /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`). 363 /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`).
364 pub(crate) fn type_params(generic_params: &Generics) -> Substs { 364 pub(crate) fn type_params_for_generics(generic_params: &Generics) -> Substs {
365 Substs(generic_params.iter().map(|(id, _)| Ty::Param(id)).collect()) 365 Substs(generic_params.iter().map(|(id, _)| Ty::Param(id)).collect())
366 } 366 }
367 367
368 /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`).
369 pub fn type_params(db: &impl HirDatabase, def: impl Into<GenericDefId>) -> Substs {
370 let params = generics(db, def.into());
371 Substs::type_params_for_generics(&params)
372 }
373
368 /// Return Substs that replace each parameter by a bound variable. 374 /// Return Substs that replace each parameter by a bound variable.
369 pub(crate) fn bound_vars(generic_params: &Generics) -> Substs { 375 pub(crate) fn bound_vars(generic_params: &Generics) -> Substs {
370 Substs(generic_params.iter().enumerate().map(|(idx, _)| Ty::Bound(idx as u32)).collect()) 376 Substs(generic_params.iter().enumerate().map(|(idx, _)| Ty::Bound(idx as u32)).collect())
@@ -1026,7 +1032,7 @@ impl HirDisplay for Ty {
1026 TypeParamProvenance::ArgumentImplTrait => { 1032 TypeParamProvenance::ArgumentImplTrait => {
1027 write!(f, "impl ")?; 1033 write!(f, "impl ")?;
1028 let bounds = f.db.generic_predicates_for_param(*id); 1034 let bounds = f.db.generic_predicates_for_param(*id);
1029 let substs = Substs::type_params(&generics); 1035 let substs = Substs::type_params_for_generics(&generics);
1030 write_bounds_like_dyn_trait(&bounds.iter().map(|b| b.clone().subst(&substs)).collect::<Vec<_>>(), f)?; 1036 write_bounds_like_dyn_trait(&bounds.iter().map(|b| b.clone().subst(&substs)).collect::<Vec<_>>(), f)?;
1031 } 1037 }
1032 } 1038 }
diff --git a/crates/ra_hir_ty/src/lower.rs b/crates/ra_hir_ty/src/lower.rs
index 847111748..0d4c075af 100644
--- a/crates/ra_hir_ty/src/lower.rs
+++ b/crates/ra_hir_ty/src/lower.rs
@@ -276,7 +276,7 @@ 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(&generics), 279 TypeParamLoweringMode::Placeholder => Substs::type_params_for_generics(&generics),
280 TypeParamLoweringMode::Variable => Substs::bound_vars(&generics), 280 TypeParamLoweringMode::Variable => Substs::bound_vars(&generics),
281 }; 281 };
282 ctx.db.impl_self_ty(impl_id).subst(&substs) 282 ctx.db.impl_self_ty(impl_id).subst(&substs)
@@ -284,7 +284,7 @@ impl Ty {
284 TypeNs::AdtSelfType(adt) => { 284 TypeNs::AdtSelfType(adt) => {
285 let generics = generics(ctx.db, adt.into()); 285 let generics = generics(ctx.db, adt.into());
286 let substs = match ctx.type_param_mode { 286 let substs = match ctx.type_param_mode {
287 TypeParamLoweringMode::Placeholder => Substs::type_params(&generics), 287 TypeParamLoweringMode::Placeholder => Substs::type_params_for_generics(&generics),
288 TypeParamLoweringMode::Variable => Substs::bound_vars(&generics), 288 TypeParamLoweringMode::Variable => Substs::bound_vars(&generics),
289 }; 289 };
290 ctx.db.ty(adt.into()).subst(&substs) 290 ctx.db.ty(adt.into()).subst(&substs)