Skip to content

Commit 5d2b488

Browse files
committed
Replace remaining uses of Substitution::build_for_def
1 parent 77333a5 commit 5d2b488

File tree

6 files changed

+46
-21
lines changed

6 files changed

+46
-21
lines changed

crates/hir/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,10 +1702,9 @@ impl Type {
17021702
fn from_def(
17031703
db: &dyn HirDatabase,
17041704
krate: CrateId,
1705-
def: impl HasResolver + Into<TyDefId> + Into<GenericDefId>,
1705+
def: impl HasResolver + Into<TyDefId>,
17061706
) -> Type {
1707-
let substs = Substitution::build_for_def(db, def).fill_with_unknown().build();
1708-
let ty = db.ty(def.into()).subst(&substs);
1707+
let ty = TyBuilder::def_ty(db, def.into()).fill_with_unknown().build();
17091708
Type::new(db, krate, def, ty)
17101709
}
17111710

crates/hir_ty/src/infer.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,10 +514,9 @@ impl<'a> InferenceContext<'a> {
514514
}
515515
}
516516
TypeNs::TypeAliasId(it) => {
517-
let substs = Substitution::build_for_def(self.db, it)
517+
let ty = TyBuilder::def_ty(self.db, it.into())
518518
.fill(std::iter::repeat_with(|| self.table.new_type_var()))
519519
.build();
520-
let ty = self.db.ty(it.into()).subst(&substs);
521520
let variant = ty_variant(&ty);
522521
forbid_unresolved_segments((ty, variant), unresolved)
523522
}

crates/hir_ty/src/infer/coerce.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
use chalk_ir::{cast::Cast, Mutability, TyVariableKind};
88
use hir_def::lang_item::LangItemTarget;
99

10-
use crate::{
11-
autoderef, traits::Solution, Interner, Ty,
12-
TyBuilder, TyKind,
13-
};
10+
use crate::{autoderef, traits::Solution, Interner, Ty, TyBuilder, TyKind};
1411

1512
use super::{InEnvironment, InferenceContext};
1613

crates/hir_ty/src/infer/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ impl<'a> InferenceContext<'a> {
243243
};
244244
let substs = match container {
245245
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)
247247
.fill(iter::repeat_with(|| self.table.new_type_var()))
248248
.build();
249249
let impl_self_ty = self.db.impl_self_ty(impl_id).subst(&impl_substs);

crates/hir_ty/src/lib.rs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ mod chalk_ext;
2828
use std::{iter, mem, sync::Arc};
2929

3030
use base_db::salsa;
31-
use chalk_ir::cast::{CastTo, Caster};
31+
use chalk_ir::{
32+
cast::{CastTo, Caster},
33+
interner::HasInterner,
34+
};
3235
use hir_def::{
3336
builtin_type::BuiltinType, expr::ExprId, type_ref::Rawness, AssocContainerId, FunctionId,
3437
GenericDefId, HasModule, LifetimeParamId, Lookup, TraitId, TypeAliasId, TypeParamId,
@@ -490,13 +493,6 @@ impl Substitution {
490493
)
491494
}
492495

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-
500496
pub(crate) fn build_for_generics(generic_params: &Generics) -> SubstsBuilder {
501497
Substitution::builder(generic_params.len())
502498
}
@@ -894,6 +890,18 @@ impl TyBuilder<()> {
894890
}
895891
}
896892
}
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+
}
897905
}
898906

899907
impl TyBuilder<hir_def::AdtId> {
@@ -956,6 +964,28 @@ impl TyBuilder<TypeAliasId> {
956964
}
957965
}
958966

967+
impl<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+
979+
impl 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+
959989
impl Ty {
960990
pub fn as_reference(&self) -> Option<(&Ty, Mutability)> {
961991
match self.kind(&Interner) {

crates/hir_ty/src/method_resolution.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ pub(crate) fn inherent_impl_substs(
709709
) -> Option<Substitution> {
710710
// we create a var for each type parameter of the impl; we need to keep in
711711
// 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)
713713
.fill_with_bound_vars(DebruijnIndex::INNERMOST, self_ty.binders.len(&Interner))
714714
.build();
715715
let self_ty_with_vars = db.impl_self_ty(impl_id).subst(&vars);
@@ -760,13 +760,13 @@ fn transform_receiver_ty(
760760
self_ty: &Canonical<Ty>,
761761
) -> Option<Ty> {
762762
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)
764764
.push(self_ty.value.clone())
765765
.fill_with_unknown()
766766
.build(),
767767
AssocContainerId::ImplId(impl_id) => {
768768
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)
770770
.use_parent_substs(&impl_substs)
771771
.fill_with_unknown()
772772
.build()

0 commit comments

Comments
 (0)