From 5d2b488aeb17410aec3b89eac69038c51f332448 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 3 Apr 2021 22:18:02 +0200 Subject: Replace remaining uses of Substitution::build_for_def --- crates/hir/src/lib.rs | 5 ++-- crates/hir_ty/src/infer.rs | 3 +-- crates/hir_ty/src/infer/coerce.rs | 5 +--- crates/hir_ty/src/infer/path.rs | 2 +- crates/hir_ty/src/lib.rs | 46 ++++++++++++++++++++++++++++------ crates/hir_ty/src/method_resolution.rs | 6 ++--- 6 files changed, 46 insertions(+), 21 deletions(-) (limited to 'crates') 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 { fn from_def( db: &dyn HirDatabase, krate: CrateId, - def: impl HasResolver + Into + Into, + def: impl HasResolver + Into, ) -> Type { - let substs = Substitution::build_for_def(db, def).fill_with_unknown().build(); - let ty = db.ty(def.into()).subst(&substs); + let ty = TyBuilder::def_ty(db, def.into()).fill_with_unknown().build(); Type::new(db, krate, def, ty) } 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> { } } TypeNs::TypeAliasId(it) => { - let substs = Substitution::build_for_def(self.db, it) + let ty = TyBuilder::def_ty(self.db, it.into()) .fill(std::iter::repeat_with(|| self.table.new_type_var())) .build(); - let ty = self.db.ty(it.into()).subst(&substs); let variant = ty_variant(&ty); forbid_unresolved_segments((ty, variant), unresolved) } 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 @@ use chalk_ir::{cast::Cast, Mutability, TyVariableKind}; use hir_def::lang_item::LangItemTarget; -use crate::{ - autoderef, traits::Solution, Interner, Ty, - TyBuilder, TyKind, -}; +use crate::{autoderef, traits::Solution, Interner, Ty, TyBuilder, TyKind}; use super::{InEnvironment, InferenceContext}; 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> { }; let substs = match container { AssocContainerId::ImplId(impl_id) => { - let impl_substs = Substitution::build_for_def(self.db, impl_id) + let impl_substs = TyBuilder::subst_for_def(self.db, impl_id) .fill(iter::repeat_with(|| self.table.new_type_var())) .build(); 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; use std::{iter, mem, sync::Arc}; use base_db::salsa; -use chalk_ir::cast::{CastTo, Caster}; +use chalk_ir::{ + cast::{CastTo, Caster}, + interner::HasInterner, +}; use hir_def::{ builtin_type::BuiltinType, expr::ExprId, type_ref::Rawness, AssocContainerId, FunctionId, GenericDefId, HasModule, LifetimeParamId, Lookup, TraitId, TypeAliasId, TypeParamId, @@ -490,13 +493,6 @@ impl Substitution { ) } - pub fn build_for_def(db: &dyn HirDatabase, def: impl Into) -> SubstsBuilder { - let def = def.into(); - let params = generics(db.upcast(), def); - let param_count = params.len(); - Substitution::builder(param_count) - } - pub(crate) fn build_for_generics(generic_params: &Generics) -> SubstsBuilder { Substitution::builder(generic_params.len()) } @@ -894,6 +890,18 @@ impl TyBuilder<()> { } } } + + pub fn subst_for_def(db: &dyn HirDatabase, def: impl Into) -> TyBuilder<()> { + let def = def.into(); + let params = generics(db.upcast(), def); + let param_count = params.len(); + TyBuilder::new((), param_count) + } + + pub fn build(self) -> Substitution { + let ((), subst) = self.build_internal(); + subst + } } impl TyBuilder { @@ -956,6 +964,28 @@ impl TyBuilder { } } +impl> TyBuilder> { + fn subst_binders(b: Binders) -> Self { + let param_count = b.num_binders; + TyBuilder::new(b, param_count) + } + + pub fn build(self) -> T { + let (b, subst) = self.build_internal(); + b.subst(&subst) + } +} + +impl TyBuilder> { + pub fn def_ty(db: &dyn HirDatabase, def: TyDefId) -> TyBuilder> { + TyBuilder::subst_binders(db.ty(def.into())) + } + + pub fn impl_self_ty(db: &dyn HirDatabase, def: hir_def::ImplId) -> TyBuilder> { + TyBuilder::subst_binders(db.impl_self_ty(def)) + } +} + impl Ty { pub fn as_reference(&self) -> Option<(&Ty, Mutability)> { 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( ) -> Option { // we create a var for each type parameter of the impl; we need to keep in // mind here that `self_ty` might have vars of its own - let vars = Substitution::build_for_def(db, impl_id) + let vars = TyBuilder::subst_for_def(db, impl_id) .fill_with_bound_vars(DebruijnIndex::INNERMOST, self_ty.binders.len(&Interner)) .build(); let self_ty_with_vars = db.impl_self_ty(impl_id).subst(&vars); @@ -760,13 +760,13 @@ fn transform_receiver_ty( self_ty: &Canonical, ) -> Option { let substs = match function_id.lookup(db.upcast()).container { - AssocContainerId::TraitId(_) => Substitution::build_for_def(db, function_id) + AssocContainerId::TraitId(_) => TyBuilder::subst_for_def(db, function_id) .push(self_ty.value.clone()) .fill_with_unknown() .build(), AssocContainerId::ImplId(impl_id) => { let impl_substs = inherent_impl_substs(db, impl_id, &self_ty)?; - Substitution::build_for_def(db, function_id) + TyBuilder::subst_for_def(db, function_id) .use_parent_substs(&impl_substs) .fill_with_unknown() .build() -- cgit v1.2.3