Replace remaining uses of Substitution::build_for_def

This commit is contained in:
Florian Diebold 2021-04-03 22:18:02 +02:00
parent 77333a571f
commit 5d2b488aeb
6 changed files with 46 additions and 21 deletions

View File

@ -1702,10 +1702,9 @@ fn new(db: &dyn HirDatabase, krate: CrateId, lexical_env: impl HasResolver, ty:
fn from_def(
db: &dyn HirDatabase,
krate: CrateId,
def: impl HasResolver + Into<TyDefId> + Into<GenericDefId>,
def: impl HasResolver + Into<TyDefId>,
) -> 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)
}

View File

@ -514,10 +514,9 @@ fn resolve_variant(&mut self, path: Option<&Path>) -> (Ty, Option<VariantId>) {
}
}
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)
}

View File

@ -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};

View File

@ -243,7 +243,7 @@ fn resolve_ty_assoc_item(
};
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);

View File

@ -28,7 +28,10 @@ macro_rules! eprintln {
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 @@ pub(crate) fn bound_vars(generic_params: &Generics, debruijn: DebruijnIndex) ->
)
}
pub fn build_for_def(db: &dyn HirDatabase, def: impl Into<GenericDefId>) -> 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 @@ pub fn builtin(builtin: BuiltinType) -> Ty {
}
}
}
pub fn subst_for_def(db: &dyn HirDatabase, def: impl Into<GenericDefId>) -> 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<hir_def::AdtId> {
@ -956,6 +964,28 @@ pub fn build(self) -> ProjectionTy {
}
}
impl<T: TypeWalk + HasInterner<Interner = Interner>> TyBuilder<Binders<T>> {
fn subst_binders(b: Binders<T>) -> 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<Binders<Ty>> {
pub fn def_ty(db: &dyn HirDatabase, def: TyDefId) -> TyBuilder<Binders<Ty>> {
TyBuilder::subst_binders(db.ty(def.into()))
}
pub fn impl_self_ty(db: &dyn HirDatabase, def: hir_def::ImplId) -> TyBuilder<Binders<Ty>> {
TyBuilder::subst_binders(db.impl_self_ty(def))
}
}
impl Ty {
pub fn as_reference(&self) -> Option<(&Ty, Mutability)> {
match self.kind(&Interner) {

View File

@ -709,7 +709,7 @@ pub(crate) fn inherent_impl_substs(
) -> Option<Substitution> {
// 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<Ty>,
) -> Option<Ty> {
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()