diff options
Diffstat (limited to 'crates/hir_ty/src/lower.rs')
-rw-r--r-- | crates/hir_ty/src/lower.rs | 49 |
1 files changed, 21 insertions, 28 deletions
diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index e60d7c730..214655807 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs | |||
@@ -36,7 +36,7 @@ use crate::{ | |||
36 | AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig, | 36 | AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig, |
37 | ImplTraitId, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause, QuantifiedWhereClauses, | 37 | ImplTraitId, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause, QuantifiedWhereClauses, |
38 | ReturnTypeImplTrait, ReturnTypeImplTraits, Substitution, TraitEnvironment, TraitRef, Ty, | 38 | ReturnTypeImplTrait, ReturnTypeImplTraits, Substitution, TraitEnvironment, TraitRef, Ty, |
39 | TyKind, TypeWalk, WhereClause, | 39 | TyBuilder, TyKind, TypeWalk, WhereClause, |
40 | }; | 40 | }; |
41 | 41 | ||
42 | #[derive(Debug)] | 42 | #[derive(Debug)] |
@@ -234,7 +234,7 @@ impl<'a> TyLoweringContext<'a> { | |||
234 | let impl_trait_id = ImplTraitId::ReturnTypeImplTrait(func, idx); | 234 | let impl_trait_id = ImplTraitId::ReturnTypeImplTrait(func, idx); |
235 | let opaque_ty_id = self.db.intern_impl_trait_id(impl_trait_id).into(); | 235 | let opaque_ty_id = self.db.intern_impl_trait_id(impl_trait_id).into(); |
236 | let generics = generics(self.db.upcast(), func.into()); | 236 | let generics = generics(self.db.upcast(), func.into()); |
237 | let parameters = Substitution::bound_vars(&generics, self.in_binders); | 237 | let parameters = generics.bound_vars_subst(self.in_binders); |
238 | TyKind::Alias(AliasTy::Opaque(OpaqueTy { | 238 | TyKind::Alias(AliasTy::Opaque(OpaqueTy { |
239 | opaque_ty_id, | 239 | opaque_ty_id, |
240 | substitution: parameters, | 240 | substitution: parameters, |
@@ -411,24 +411,16 @@ impl<'a> TyLoweringContext<'a> { | |||
411 | TypeNs::SelfType(impl_id) => { | 411 | TypeNs::SelfType(impl_id) => { |
412 | let generics = generics(self.db.upcast(), impl_id.into()); | 412 | let generics = generics(self.db.upcast(), impl_id.into()); |
413 | let substs = match self.type_param_mode { | 413 | let substs = match self.type_param_mode { |
414 | TypeParamLoweringMode::Placeholder => { | 414 | TypeParamLoweringMode::Placeholder => generics.type_params_subst(self.db), |
415 | Substitution::type_params_for_generics(self.db, &generics) | 415 | TypeParamLoweringMode::Variable => generics.bound_vars_subst(self.in_binders), |
416 | } | ||
417 | TypeParamLoweringMode::Variable => { | ||
418 | Substitution::bound_vars(&generics, self.in_binders) | ||
419 | } | ||
420 | }; | 416 | }; |
421 | self.db.impl_self_ty(impl_id).subst(&substs) | 417 | self.db.impl_self_ty(impl_id).subst(&substs) |
422 | } | 418 | } |
423 | TypeNs::AdtSelfType(adt) => { | 419 | TypeNs::AdtSelfType(adt) => { |
424 | let generics = generics(self.db.upcast(), adt.into()); | 420 | let generics = generics(self.db.upcast(), adt.into()); |
425 | let substs = match self.type_param_mode { | 421 | let substs = match self.type_param_mode { |
426 | TypeParamLoweringMode::Placeholder => { | 422 | TypeParamLoweringMode::Placeholder => generics.type_params_subst(self.db), |
427 | Substitution::type_params_for_generics(self.db, &generics) | 423 | TypeParamLoweringMode::Variable => generics.bound_vars_subst(self.in_binders), |
428 | } | ||
429 | TypeParamLoweringMode::Variable => { | ||
430 | Substitution::bound_vars(&generics, self.in_binders) | ||
431 | } | ||
432 | }; | 424 | }; |
433 | self.db.ty(adt.into()).subst(&substs) | 425 | self.db.ty(adt.into()).subst(&substs) |
434 | } | 426 | } |
@@ -478,12 +470,13 @@ impl<'a> TyLoweringContext<'a> { | |||
478 | TypeParamLoweringMode::Placeholder => { | 470 | TypeParamLoweringMode::Placeholder => { |
479 | // if we're lowering to placeholders, we have to put | 471 | // if we're lowering to placeholders, we have to put |
480 | // them in now | 472 | // them in now |
481 | let s = Substitution::type_params( | 473 | let generics = generics( |
482 | self.db, | 474 | self.db.upcast(), |
483 | self.resolver.generic_def().expect( | 475 | self.resolver.generic_def().expect( |
484 | "there should be generics if there's a generic param", | 476 | "there should be generics if there's a generic param", |
485 | ), | 477 | ), |
486 | ); | 478 | ); |
479 | let s = generics.type_params_subst(self.db); | ||
487 | t.substitution.clone().subst_bound_vars(&s) | 480 | t.substitution.clone().subst_bound_vars(&s) |
488 | } | 481 | } |
489 | TypeParamLoweringMode::Variable => t.substitution.clone(), | 482 | TypeParamLoweringMode::Variable => t.substitution.clone(), |
@@ -860,10 +853,9 @@ pub fn associated_type_shorthand_candidates<R>( | |||
860 | if generics.params.types[param_id.local_id].provenance | 853 | if generics.params.types[param_id.local_id].provenance |
861 | == TypeParamProvenance::TraitSelf | 854 | == TypeParamProvenance::TraitSelf |
862 | { | 855 | { |
863 | let trait_ref = TraitRef { | 856 | let trait_ref = TyBuilder::trait_ref(db, trait_id) |
864 | trait_id: to_chalk_trait_id(trait_id), | 857 | .fill_with_bound_vars(DebruijnIndex::INNERMOST, 0) |
865 | substitution: Substitution::bound_vars(&generics, DebruijnIndex::INNERMOST), | 858 | .build(); |
866 | }; | ||
867 | return search(trait_ref); | 859 | return search(trait_ref); |
868 | } | 860 | } |
869 | } | 861 | } |
@@ -972,7 +964,7 @@ pub(crate) fn trait_environment_query( | |||
972 | // function default implementations (and hypothetical code | 964 | // function default implementations (and hypothetical code |
973 | // inside consts or type aliases) | 965 | // inside consts or type aliases) |
974 | cov_mark::hit!(trait_self_implements_self); | 966 | cov_mark::hit!(trait_self_implements_self); |
975 | let substs = Substitution::type_params(db, trait_id); | 967 | let substs = TyBuilder::type_params_subst(db, trait_id); |
976 | let trait_ref = TraitRef { trait_id: to_chalk_trait_id(trait_id), substitution: substs }; | 968 | let trait_ref = TraitRef { trait_id: to_chalk_trait_id(trait_id), substitution: substs }; |
977 | let pred = WhereClause::Implemented(trait_ref); | 969 | let pred = WhereClause::Implemented(trait_ref); |
978 | let program_clause: chalk_ir::ProgramClause<Interner> = pred.to_chalk(db).cast(&Interner); | 970 | let program_clause: chalk_ir::ProgramClause<Interner> = pred.to_chalk(db).cast(&Interner); |
@@ -1061,7 +1053,7 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig { | |||
1061 | /// function body. | 1053 | /// function body. |
1062 | fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> { | 1054 | fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> { |
1063 | let generics = generics(db.upcast(), def.into()); | 1055 | let generics = generics(db.upcast(), def.into()); |
1064 | let substs = Substitution::bound_vars(&generics, DebruijnIndex::INNERMOST); | 1056 | let substs = generics.bound_vars_subst(DebruijnIndex::INNERMOST); |
1065 | Binders::new( | 1057 | Binders::new( |
1066 | substs.len(&Interner), | 1058 | substs.len(&Interner), |
1067 | TyKind::FnDef(CallableDefId::FunctionId(def).to_chalk(db), substs).intern(&Interner), | 1059 | TyKind::FnDef(CallableDefId::FunctionId(def).to_chalk(db), substs).intern(&Interner), |
@@ -1106,7 +1098,7 @@ fn type_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> Binders<T | |||
1106 | return type_for_adt(db, def.into()); | 1098 | return type_for_adt(db, def.into()); |
1107 | } | 1099 | } |
1108 | let generics = generics(db.upcast(), def.into()); | 1100 | let generics = generics(db.upcast(), def.into()); |
1109 | let substs = Substitution::bound_vars(&generics, DebruijnIndex::INNERMOST); | 1101 | let substs = generics.bound_vars_subst(DebruijnIndex::INNERMOST); |
1110 | Binders::new( | 1102 | Binders::new( |
1111 | substs.len(&Interner), | 1103 | substs.len(&Interner), |
1112 | TyKind::FnDef(CallableDefId::StructId(def).to_chalk(db), substs).intern(&Interner), | 1104 | TyKind::FnDef(CallableDefId::StructId(def).to_chalk(db), substs).intern(&Interner), |
@@ -1133,7 +1125,7 @@ fn type_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) - | |||
1133 | return type_for_adt(db, def.parent.into()); | 1125 | return type_for_adt(db, def.parent.into()); |
1134 | } | 1126 | } |
1135 | let generics = generics(db.upcast(), def.parent.into()); | 1127 | let generics = generics(db.upcast(), def.parent.into()); |
1136 | let substs = Substitution::bound_vars(&generics, DebruijnIndex::INNERMOST); | 1128 | let substs = generics.bound_vars_subst(DebruijnIndex::INNERMOST); |
1137 | Binders::new( | 1129 | Binders::new( |
1138 | substs.len(&Interner), | 1130 | substs.len(&Interner), |
1139 | TyKind::FnDef(CallableDefId::EnumVariantId(def).to_chalk(db), substs).intern(&Interner), | 1131 | TyKind::FnDef(CallableDefId::EnumVariantId(def).to_chalk(db), substs).intern(&Interner), |
@@ -1141,9 +1133,10 @@ fn type_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) - | |||
1141 | } | 1133 | } |
1142 | 1134 | ||
1143 | fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> { | 1135 | fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> { |
1144 | let generics = generics(db.upcast(), adt.into()); | 1136 | let b = TyBuilder::adt(db, adt); |
1145 | let substs = Substitution::bound_vars(&generics, DebruijnIndex::INNERMOST); | 1137 | let num_binders = b.remaining(); |
1146 | Binders::new(substs.len(&Interner), Ty::adt_ty(adt, substs)) | 1138 | let ty = b.fill_with_bound_vars(DebruijnIndex::INNERMOST, 0).build(); |
1139 | Binders::new(num_binders, ty) | ||
1147 | } | 1140 | } |
1148 | 1141 | ||
1149 | fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> { | 1142 | fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> { |
@@ -1215,7 +1208,7 @@ impl_from!(FunctionId, StructId, UnionId, EnumVariantId, ConstId, StaticId for V | |||
1215 | /// namespace. | 1208 | /// namespace. |
1216 | pub(crate) fn ty_query(db: &dyn HirDatabase, def: TyDefId) -> Binders<Ty> { | 1209 | pub(crate) fn ty_query(db: &dyn HirDatabase, def: TyDefId) -> Binders<Ty> { |
1217 | match def { | 1210 | match def { |
1218 | TyDefId::BuiltinType(it) => Binders::new(0, Ty::builtin(it)), | 1211 | TyDefId::BuiltinType(it) => Binders::new(0, TyBuilder::builtin(it)), |
1219 | TyDefId::AdtId(it) => type_for_adt(db, it), | 1212 | TyDefId::AdtId(it) => type_for_adt(db, it), |
1220 | TyDefId::TypeAliasId(it) => type_for_type_alias(db, it), | 1213 | TyDefId::TypeAliasId(it) => type_for_type_alias(db, it), |
1221 | } | 1214 | } |