mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 14:21:44 +00:00
Replace last uses of SubstsBuilder by TyBuilder
This commit is contained in:
parent
505ca65216
commit
584d1c9e5b
4 changed files with 23 additions and 64 deletions
|
@ -75,14 +75,13 @@ impl<'a> InferenceContext<'a> {
|
||||||
self.db.trait_data(fn_once_trait).associated_type_by_name(&name![Output])?;
|
self.db.trait_data(fn_once_trait).associated_type_by_name(&name![Output])?;
|
||||||
|
|
||||||
let mut arg_tys = vec![];
|
let mut arg_tys = vec![];
|
||||||
let parameters = Substitution::builder(num_args)
|
let arg_ty = TyBuilder::tuple(num_args)
|
||||||
.fill(repeat_with(|| {
|
.fill(repeat_with(|| {
|
||||||
let arg = self.table.new_type_var();
|
let arg = self.table.new_type_var();
|
||||||
arg_tys.push(arg.clone());
|
arg_tys.push(arg.clone());
|
||||||
arg
|
arg
|
||||||
}))
|
}))
|
||||||
.build();
|
.build();
|
||||||
let arg_ty = TyKind::Tuple(num_args, parameters).intern(&Interner);
|
|
||||||
|
|
||||||
let projection = {
|
let projection = {
|
||||||
let b = TyBuilder::assoc_type_projection(self.db, output_assoc_type);
|
let b = TyBuilder::assoc_type_projection(self.db, output_assoc_type);
|
||||||
|
|
|
@ -93,16 +93,13 @@ impl<'a> InferenceContext<'a> {
|
||||||
ValueNs::GenericParam(it) => return Some(self.db.const_param_ty(it)),
|
ValueNs::GenericParam(it) => return Some(self.db.const_param_ty(it)),
|
||||||
};
|
};
|
||||||
|
|
||||||
let ty = self.db.value_ty(typable);
|
|
||||||
// self_subst is just for the parent
|
|
||||||
let parent_substs = self_subst.unwrap_or_else(|| Substitution::empty(&Interner));
|
let parent_substs = self_subst.unwrap_or_else(|| Substitution::empty(&Interner));
|
||||||
let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver);
|
let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver);
|
||||||
let substs = ctx.substs_from_path(path, typable, true);
|
let substs = ctx.substs_from_path(path, typable, true);
|
||||||
let full_substs = Substitution::builder(substs.len(&Interner))
|
let ty = TyBuilder::value_ty(self.db, typable)
|
||||||
.use_parent_substs(&parent_substs)
|
.use_parent_substs(&parent_substs)
|
||||||
.fill(substs.interned(&Interner)[parent_substs.len(&Interner)..].iter().cloned())
|
.fill(substs.interned(&Interner)[parent_substs.len(&Interner)..].iter().cloned())
|
||||||
.build();
|
.build();
|
||||||
let ty = ty.subst(&full_substs);
|
|
||||||
Some(ty)
|
Some(ty)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -186,14 +186,11 @@ pub(crate) fn unify(tys: &Canonical<(Ty, Ty)>) -> Option<Substitution> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(
|
Some(Substitution::from_iter(
|
||||||
Substitution::builder(tys.binders.len(&Interner))
|
&Interner,
|
||||||
.fill(
|
|
||||||
vars.iter(&Interner)
|
vars.iter(&Interner)
|
||||||
.map(|v| table.resolve_ty_completely(v.assert_ty_ref(&Interner).clone())),
|
.map(|v| table.resolve_ty_completely(v.assert_ty_ref(&Interner).clone())),
|
||||||
)
|
))
|
||||||
.build(),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
|
|
|
@ -492,10 +492,6 @@ impl Substitution {
|
||||||
.map(|(idx, _)| TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(&Interner)),
|
.map(|(idx, _)| TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(&Interner)),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn builder(param_count: usize) -> SubstsBuilder {
|
|
||||||
SubstsBuilder { vec: Vec::with_capacity(param_count), param_count }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return an index of a parameter in the generic type parameter list by it's id.
|
/// Return an index of a parameter in the generic type parameter list by it's id.
|
||||||
|
@ -503,52 +499,6 @@ pub fn param_idx(db: &dyn HirDatabase, id: TypeParamId) -> Option<usize> {
|
||||||
generics(db.upcast(), id.parent).param_idx(id)
|
generics(db.upcast(), id.parent).param_idx(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct SubstsBuilder {
|
|
||||||
vec: Vec<GenericArg>,
|
|
||||||
param_count: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SubstsBuilder {
|
|
||||||
pub fn build(self) -> Substitution {
|
|
||||||
assert_eq!(self.vec.len(), self.param_count);
|
|
||||||
Substitution::from_iter(&Interner, self.vec)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn push(mut self, ty: impl CastTo<GenericArg>) -> Self {
|
|
||||||
self.vec.push(ty.cast(&Interner));
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
fn remaining(&self) -> usize {
|
|
||||||
self.param_count - self.vec.len()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn fill_with_bound_vars(self, debruijn: DebruijnIndex, starting_from: usize) -> Self {
|
|
||||||
self.fill(
|
|
||||||
(starting_from..)
|
|
||||||
.map(|idx| TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(&Interner)),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn fill_with_unknown(self) -> Self {
|
|
||||||
self.fill(iter::repeat(TyKind::Unknown.intern(&Interner)))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn fill(mut self, filler: impl Iterator<Item = impl CastTo<GenericArg>>) -> Self {
|
|
||||||
self.vec.extend(filler.take(self.remaining()).casted(&Interner));
|
|
||||||
assert_eq!(self.remaining(), 0);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn use_parent_substs(mut self, parent_substs: &Substitution) -> Self {
|
|
||||||
assert!(self.vec.is_empty());
|
|
||||||
assert!(parent_substs.len(&Interner) <= self.param_count);
|
|
||||||
self.vec.extend(parent_substs.iter(&Interner).cloned());
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
|
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
|
||||||
pub struct Binders<T> {
|
pub struct Binders<T> {
|
||||||
pub num_binders: usize,
|
pub num_binders: usize,
|
||||||
|
@ -921,6 +871,18 @@ impl TyBuilder<hir_def::AdtId> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Tuple(usize);
|
||||||
|
impl TyBuilder<Tuple> {
|
||||||
|
pub fn tuple(size: usize) -> TyBuilder<Tuple> {
|
||||||
|
TyBuilder::new(Tuple(size), size)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn build(self) -> Ty {
|
||||||
|
let (Tuple(size), subst) = self.build_internal();
|
||||||
|
TyKind::Tuple(size, subst).intern(&Interner)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl TyBuilder<TraitId> {
|
impl TyBuilder<TraitId> {
|
||||||
pub fn trait_ref(db: &dyn HirDatabase, trait_id: TraitId) -> TyBuilder<TraitId> {
|
pub fn trait_ref(db: &dyn HirDatabase, trait_id: TraitId) -> TyBuilder<TraitId> {
|
||||||
let generics = generics(db.upcast(), trait_id.into());
|
let generics = generics(db.upcast(), trait_id.into());
|
||||||
|
@ -970,6 +932,10 @@ impl TyBuilder<Binders<Ty>> {
|
||||||
pub fn impl_self_ty(db: &dyn HirDatabase, def: hir_def::ImplId) -> TyBuilder<Binders<Ty>> {
|
pub fn impl_self_ty(db: &dyn HirDatabase, def: hir_def::ImplId) -> TyBuilder<Binders<Ty>> {
|
||||||
TyBuilder::subst_binders(db.impl_self_ty(def))
|
TyBuilder::subst_binders(db.impl_self_ty(def))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn value_ty(db: &dyn HirDatabase, def: ValueTyDefId) -> TyBuilder<Binders<Ty>> {
|
||||||
|
TyBuilder::subst_binders(db.value_ty(def))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Ty {
|
impl Ty {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue