Skip to content

Commit 584d1c9

Browse files
committed
Replace last uses of SubstsBuilder by TyBuilder
1 parent 505ca65 commit 584d1c9

File tree

4 files changed

+23
-64
lines changed

4 files changed

+23
-64
lines changed

crates/hir_ty/src/infer/expr.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,13 @@ impl<'a> InferenceContext<'a> {
7575
self.db.trait_data(fn_once_trait).associated_type_by_name(&name![Output])?;
7676

7777
let mut arg_tys = vec![];
78-
let parameters = Substitution::builder(num_args)
78+
let arg_ty = TyBuilder::tuple(num_args)
7979
.fill(repeat_with(|| {
8080
let arg = self.table.new_type_var();
8181
arg_tys.push(arg.clone());
8282
arg
8383
}))
8484
.build();
85-
let arg_ty = TyKind::Tuple(num_args, parameters).intern(&Interner);
8685

8786
let projection = {
8887
let b = TyBuilder::assoc_type_projection(self.db, output_assoc_type);

crates/hir_ty/src/infer/path.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,13 @@ impl<'a> InferenceContext<'a> {
9393
ValueNs::GenericParam(it) => return Some(self.db.const_param_ty(it)),
9494
};
9595

96-
let ty = self.db.value_ty(typable);
97-
// self_subst is just for the parent
9896
let parent_substs = self_subst.unwrap_or_else(|| Substitution::empty(&Interner));
9997
let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver);
10098
let substs = ctx.substs_from_path(path, typable, true);
101-
let full_substs = Substitution::builder(substs.len(&Interner))
99+
let ty = TyBuilder::value_ty(self.db, typable)
102100
.use_parent_substs(&parent_substs)
103101
.fill(substs.interned(&Interner)[parent_substs.len(&Interner)..].iter().cloned())
104102
.build();
105-
let ty = ty.subst(&full_substs);
106103
Some(ty)
107104
}
108105

crates/hir_ty/src/infer/unify.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,11 @@ pub(crate) fn unify(tys: &Canonical<(Ty, Ty)>) -> Option<Substitution> {
186186
);
187187
}
188188
}
189-
Some(
190-
Substitution::builder(tys.binders.len(&Interner))
191-
.fill(
192-
vars.iter(&Interner)
193-
.map(|v| table.resolve_ty_completely(v.assert_ty_ref(&Interner).clone())),
194-
)
195-
.build(),
196-
)
189+
Some(Substitution::from_iter(
190+
&Interner,
191+
vars.iter(&Interner)
192+
.map(|v| table.resolve_ty_completely(v.assert_ty_ref(&Interner).clone())),
193+
))
197194
}
198195

199196
#[derive(Clone, Debug)]

crates/hir_ty/src/lib.rs

Lines changed: 16 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -492,63 +492,13 @@ impl Substitution {
492492
.map(|(idx, _)| TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(&Interner)),
493493
)
494494
}
495-
496-
fn builder(param_count: usize) -> SubstsBuilder {
497-
SubstsBuilder { vec: Vec::with_capacity(param_count), param_count }
498-
}
499495
}
500496

501497
/// Return an index of a parameter in the generic type parameter list by it's id.
502498
pub fn param_idx(db: &dyn HirDatabase, id: TypeParamId) -> Option<usize> {
503499
generics(db.upcast(), id.parent).param_idx(id)
504500
}
505501

506-
#[derive(Debug, Clone)]
507-
pub struct SubstsBuilder {
508-
vec: Vec<GenericArg>,
509-
param_count: usize,
510-
}
511-
512-
impl SubstsBuilder {
513-
pub fn build(self) -> Substitution {
514-
assert_eq!(self.vec.len(), self.param_count);
515-
Substitution::from_iter(&Interner, self.vec)
516-
}
517-
518-
pub fn push(mut self, ty: impl CastTo<GenericArg>) -> Self {
519-
self.vec.push(ty.cast(&Interner));
520-
self
521-
}
522-
523-
fn remaining(&self) -> usize {
524-
self.param_count - self.vec.len()
525-
}
526-
527-
pub fn fill_with_bound_vars(self, debruijn: DebruijnIndex, starting_from: usize) -> Self {
528-
self.fill(
529-
(starting_from..)
530-
.map(|idx| TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(&Interner)),
531-
)
532-
}
533-
534-
pub fn fill_with_unknown(self) -> Self {
535-
self.fill(iter::repeat(TyKind::Unknown.intern(&Interner)))
536-
}
537-
538-
pub fn fill(mut self, filler: impl Iterator<Item = impl CastTo<GenericArg>>) -> Self {
539-
self.vec.extend(filler.take(self.remaining()).casted(&Interner));
540-
assert_eq!(self.remaining(), 0);
541-
self
542-
}
543-
544-
pub fn use_parent_substs(mut self, parent_substs: &Substitution) -> Self {
545-
assert!(self.vec.is_empty());
546-
assert!(parent_substs.len(&Interner) <= self.param_count);
547-
self.vec.extend(parent_substs.iter(&Interner).cloned());
548-
self
549-
}
550-
}
551-
552502
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
553503
pub struct Binders<T> {
554504
pub num_binders: usize,
@@ -921,6 +871,18 @@ impl TyBuilder<hir_def::AdtId> {
921871
}
922872
}
923873

874+
struct Tuple(usize);
875+
impl TyBuilder<Tuple> {
876+
pub fn tuple(size: usize) -> TyBuilder<Tuple> {
877+
TyBuilder::new(Tuple(size), size)
878+
}
879+
880+
pub fn build(self) -> Ty {
881+
let (Tuple(size), subst) = self.build_internal();
882+
TyKind::Tuple(size, subst).intern(&Interner)
883+
}
884+
}
885+
924886
impl TyBuilder<TraitId> {
925887
pub fn trait_ref(db: &dyn HirDatabase, trait_id: TraitId) -> TyBuilder<TraitId> {
926888
let generics = generics(db.upcast(), trait_id.into());
@@ -970,6 +932,10 @@ impl TyBuilder<Binders<Ty>> {
970932
pub fn impl_self_ty(db: &dyn HirDatabase, def: hir_def::ImplId) -> TyBuilder<Binders<Ty>> {
971933
TyBuilder::subst_binders(db.impl_self_ty(def))
972934
}
935+
936+
pub fn value_ty(db: &dyn HirDatabase, def: ValueTyDefId) -> TyBuilder<Binders<Ty>> {
937+
TyBuilder::subst_binders(db.value_ty(def))
938+
}
973939
}
974940

975941
impl Ty {

0 commit comments

Comments
 (0)