aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2021-04-03 21:18:02 +0100
committerFlorian Diebold <[email protected]>2021-04-04 12:16:38 +0100
commit5d2b488aeb17410aec3b89eac69038c51f332448 (patch)
tree30ef50eb5dc4e8c572c6bec969ce96b3ed2f3e68
parent77333a571ffb401a6765bbc80dea082ad08238d9 (diff)
Replace remaining uses of Substitution::build_for_def
-rw-r--r--crates/hir/src/lib.rs5
-rw-r--r--crates/hir_ty/src/infer.rs3
-rw-r--r--crates/hir_ty/src/infer/coerce.rs5
-rw-r--r--crates/hir_ty/src/infer/path.rs2
-rw-r--r--crates/hir_ty/src/lib.rs46
-rw-r--r--crates/hir_ty/src/method_resolution.rs6
6 files changed, 46 insertions, 21 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 15f46f720..42ae53c66 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -1702,10 +1702,9 @@ impl Type {
1702 fn from_def( 1702 fn from_def(
1703 db: &dyn HirDatabase, 1703 db: &dyn HirDatabase,
1704 krate: CrateId, 1704 krate: CrateId,
1705 def: impl HasResolver + Into<TyDefId> + Into<GenericDefId>, 1705 def: impl HasResolver + Into<TyDefId>,
1706 ) -> Type { 1706 ) -> Type {
1707 let substs = Substitution::build_for_def(db, def).fill_with_unknown().build(); 1707 let ty = TyBuilder::def_ty(db, def.into()).fill_with_unknown().build();
1708 let ty = db.ty(def.into()).subst(&substs);
1709 Type::new(db, krate, def, ty) 1708 Type::new(db, krate, def, ty)
1710 } 1709 }
1711 1710
diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs
index b208d821d..caafeccb8 100644
--- a/crates/hir_ty/src/infer.rs
+++ b/crates/hir_ty/src/infer.rs
@@ -514,10 +514,9 @@ impl<'a> InferenceContext<'a> {
514 } 514 }
515 } 515 }
516 TypeNs::TypeAliasId(it) => { 516 TypeNs::TypeAliasId(it) => {
517 let substs = Substitution::build_for_def(self.db, it) 517 let ty = TyBuilder::def_ty(self.db, it.into())
518 .fill(std::iter::repeat_with(|| self.table.new_type_var())) 518 .fill(std::iter::repeat_with(|| self.table.new_type_var()))
519 .build(); 519 .build();
520 let ty = self.db.ty(it.into()).subst(&substs);
521 let variant = ty_variant(&ty); 520 let variant = ty_variant(&ty);
522 forbid_unresolved_segments((ty, variant), unresolved) 521 forbid_unresolved_segments((ty, variant), unresolved)
523 } 522 }
diff --git a/crates/hir_ty/src/infer/coerce.rs b/crates/hir_ty/src/infer/coerce.rs
index ed8c677a8..028a4d568 100644
--- a/crates/hir_ty/src/infer/coerce.rs
+++ b/crates/hir_ty/src/infer/coerce.rs
@@ -7,10 +7,7 @@
7use chalk_ir::{cast::Cast, Mutability, TyVariableKind}; 7use chalk_ir::{cast::Cast, Mutability, TyVariableKind};
8use hir_def::lang_item::LangItemTarget; 8use hir_def::lang_item::LangItemTarget;
9 9
10use crate::{ 10use crate::{autoderef, traits::Solution, Interner, Ty, TyBuilder, TyKind};
11 autoderef, traits::Solution, Interner, Ty,
12 TyBuilder, TyKind,
13};
14 11
15use super::{InEnvironment, InferenceContext}; 12use super::{InEnvironment, InferenceContext};
16 13
diff --git a/crates/hir_ty/src/infer/path.rs b/crates/hir_ty/src/infer/path.rs
index 282dd499f..c9219776b 100644
--- a/crates/hir_ty/src/infer/path.rs
+++ b/crates/hir_ty/src/infer/path.rs
@@ -243,7 +243,7 @@ impl<'a> InferenceContext<'a> {
243 }; 243 };
244 let substs = match container { 244 let substs = match container {
245 AssocContainerId::ImplId(impl_id) => { 245 AssocContainerId::ImplId(impl_id) => {
246 let impl_substs = Substitution::build_for_def(self.db, impl_id) 246 let impl_substs = TyBuilder::subst_for_def(self.db, impl_id)
247 .fill(iter::repeat_with(|| self.table.new_type_var())) 247 .fill(iter::repeat_with(|| self.table.new_type_var()))
248 .build(); 248 .build();
249 let impl_self_ty = self.db.impl_self_ty(impl_id).subst(&impl_substs); 249 let impl_self_ty = self.db.impl_self_ty(impl_id).subst(&impl_substs);
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs
index e586d73d8..75bf8bcd9 100644
--- a/crates/hir_ty/src/lib.rs
+++ b/crates/hir_ty/src/lib.rs
@@ -28,7 +28,10 @@ mod chalk_ext;
28use std::{iter, mem, sync::Arc}; 28use std::{iter, mem, sync::Arc};
29 29
30use base_db::salsa; 30use base_db::salsa;
31use chalk_ir::cast::{CastTo, Caster}; 31use chalk_ir::{
32 cast::{CastTo, Caster},
33 interner::HasInterner,
34};
32use hir_def::{ 35use hir_def::{
33 builtin_type::BuiltinType, expr::ExprId, type_ref::Rawness, AssocContainerId, FunctionId, 36 builtin_type::BuiltinType, expr::ExprId, type_ref::Rawness, AssocContainerId, FunctionId,
34 GenericDefId, HasModule, LifetimeParamId, Lookup, TraitId, TypeAliasId, TypeParamId, 37 GenericDefId, HasModule, LifetimeParamId, Lookup, TraitId, TypeAliasId, TypeParamId,
@@ -490,13 +493,6 @@ impl Substitution {
490 ) 493 )
491 } 494 }
492 495
493 pub fn build_for_def(db: &dyn HirDatabase, def: impl Into<GenericDefId>) -> SubstsBuilder {
494 let def = def.into();
495 let params = generics(db.upcast(), def);
496 let param_count = params.len();
497 Substitution::builder(param_count)
498 }
499
500 pub(crate) fn build_for_generics(generic_params: &Generics) -> SubstsBuilder { 496 pub(crate) fn build_for_generics(generic_params: &Generics) -> SubstsBuilder {
501 Substitution::builder(generic_params.len()) 497 Substitution::builder(generic_params.len())
502 } 498 }
@@ -894,6 +890,18 @@ impl TyBuilder<()> {
894 } 890 }
895 } 891 }
896 } 892 }
893
894 pub fn subst_for_def(db: &dyn HirDatabase, def: impl Into<GenericDefId>) -> TyBuilder<()> {
895 let def = def.into();
896 let params = generics(db.upcast(), def);
897 let param_count = params.len();
898 TyBuilder::new((), param_count)
899 }
900
901 pub fn build(self) -> Substitution {
902 let ((), subst) = self.build_internal();
903 subst
904 }
897} 905}
898 906
899impl TyBuilder<hir_def::AdtId> { 907impl TyBuilder<hir_def::AdtId> {
@@ -956,6 +964,28 @@ impl TyBuilder<TypeAliasId> {
956 } 964 }
957} 965}
958 966
967impl<T: TypeWalk + HasInterner<Interner = Interner>> TyBuilder<Binders<T>> {
968 fn subst_binders(b: Binders<T>) -> Self {
969 let param_count = b.num_binders;
970 TyBuilder::new(b, param_count)
971 }
972
973 pub fn build(self) -> T {
974 let (b, subst) = self.build_internal();
975 b.subst(&subst)
976 }
977}
978
979impl TyBuilder<Binders<Ty>> {
980 pub fn def_ty(db: &dyn HirDatabase, def: TyDefId) -> TyBuilder<Binders<Ty>> {
981 TyBuilder::subst_binders(db.ty(def.into()))
982 }
983
984 pub fn impl_self_ty(db: &dyn HirDatabase, def: hir_def::ImplId) -> TyBuilder<Binders<Ty>> {
985 TyBuilder::subst_binders(db.impl_self_ty(def))
986 }
987}
988
959impl Ty { 989impl Ty {
960 pub fn as_reference(&self) -> Option<(&Ty, Mutability)> { 990 pub fn as_reference(&self) -> Option<(&Ty, Mutability)> {
961 match self.kind(&Interner) { 991 match self.kind(&Interner) {
diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs
index b9cc081e8..a76586f0c 100644
--- a/crates/hir_ty/src/method_resolution.rs
+++ b/crates/hir_ty/src/method_resolution.rs
@@ -709,7 +709,7 @@ pub(crate) fn inherent_impl_substs(
709) -> Option<Substitution> { 709) -> Option<Substitution> {
710 // we create a var for each type parameter of the impl; we need to keep in 710 // we create a var for each type parameter of the impl; we need to keep in
711 // mind here that `self_ty` might have vars of its own 711 // mind here that `self_ty` might have vars of its own
712 let vars = Substitution::build_for_def(db, impl_id) 712 let vars = TyBuilder::subst_for_def(db, impl_id)
713 .fill_with_bound_vars(DebruijnIndex::INNERMOST, self_ty.binders.len(&Interner)) 713 .fill_with_bound_vars(DebruijnIndex::INNERMOST, self_ty.binders.len(&Interner))
714 .build(); 714 .build();
715 let self_ty_with_vars = db.impl_self_ty(impl_id).subst(&vars); 715 let self_ty_with_vars = db.impl_self_ty(impl_id).subst(&vars);
@@ -760,13 +760,13 @@ fn transform_receiver_ty(
760 self_ty: &Canonical<Ty>, 760 self_ty: &Canonical<Ty>,
761) -> Option<Ty> { 761) -> Option<Ty> {
762 let substs = match function_id.lookup(db.upcast()).container { 762 let substs = match function_id.lookup(db.upcast()).container {
763 AssocContainerId::TraitId(_) => Substitution::build_for_def(db, function_id) 763 AssocContainerId::TraitId(_) => TyBuilder::subst_for_def(db, function_id)
764 .push(self_ty.value.clone()) 764 .push(self_ty.value.clone())
765 .fill_with_unknown() 765 .fill_with_unknown()
766 .build(), 766 .build(),
767 AssocContainerId::ImplId(impl_id) => { 767 AssocContainerId::ImplId(impl_id) => {
768 let impl_substs = inherent_impl_substs(db, impl_id, &self_ty)?; 768 let impl_substs = inherent_impl_substs(db, impl_id, &self_ty)?;
769 Substitution::build_for_def(db, function_id) 769 TyBuilder::subst_for_def(db, function_id)
770 .use_parent_substs(&impl_substs) 770 .use_parent_substs(&impl_substs)
771 .fill_with_unknown() 771 .fill_with_unknown()
772 .build() 772 .build()