Replace Substitution::type_params

This commit is contained in:
Florian Diebold 2021-04-04 13:16:16 +02:00
parent a4d7bdf1c8
commit ebdfc932e7
6 changed files with 15 additions and 16 deletions

View File

@ -13,7 +13,7 @@ use syntax::ast::{self, NameOwner};
use crate::{
Adt, Const, ConstParam, Enum, Field, Function, GenericParam, HasVisibility, LifetimeParam,
Module, Static, Struct, Substitution, Trait, Type, TypeAlias, TypeParam, Union, Variant,
Module, Static, Struct, Trait, TyBuilder, Type, TypeAlias, TypeParam, Union, Variant,
};
impl HirDisplay for Function {
@ -234,7 +234,7 @@ impl HirDisplay for TypeParam {
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
write!(f, "{}", self.name(f.db))?;
let bounds = f.db.generic_predicates_for_param(self.id);
let substs = Substitution::type_params(f.db, self.id.parent);
let substs = TyBuilder::type_params_subst(f.db, self.id.parent);
let predicates = bounds.iter().cloned().map(|b| b.subst(&substs)).collect::<Vec<_>>();
if !(predicates.is_empty() || f.omit_verbose_types()) {
write_bounds_like_dyn_trait_with_prefix(":", &predicates, f)?;

View File

@ -514,7 +514,7 @@ impl Field {
VariantDef::Union(it) => it.id.into(),
VariantDef::Variant(it) => it.parent.id.into(),
};
let substs = Substitution::type_params(db, generic_def_id);
let substs = TyBuilder::type_params_subst(db, generic_def_id);
let ty = db.field_types(var_id)[self.id].clone().subst(&substs);
Type::new(db, self.parent.module(db).id.krate(), var_id, ty)
}
@ -1501,7 +1501,7 @@ impl TypeParam {
let resolver = self.id.parent.resolver(db.upcast());
let krate = self.id.parent.module(db.upcast()).krate();
let ty = params.get(local_idx)?.clone();
let subst = Substitution::type_params(db, self.id.parent);
let subst = TyBuilder::type_params_subst(db, self.id.parent);
let ty = ty.subst(&subst.prefix(local_idx));
Some(Type::new_with_resolver_inner(db, krate, &resolver, ty))
}

View File

@ -99,6 +99,11 @@ impl TyBuilder<()> {
}
}
pub fn type_params_subst(db: &dyn HirDatabase, def: impl Into<GenericDefId>) -> Substitution {
let params = generics(db.upcast(), def.into());
params.type_params_subst(db)
}
pub fn subst_for_def(db: &dyn HirDatabase, def: impl Into<GenericDefId>) -> TyBuilder<()> {
let def = def.into();
let params = generics(db.upcast(), def);

View File

@ -19,8 +19,7 @@ use crate::{
db::HirDatabase, from_assoc_type_id, from_foreign_def_id, from_placeholder_idx, primitive,
to_assoc_type_id, traits::chalk::from_chalk, utils::generics, AdtId, AliasEq, AliasTy,
CallableDefId, CallableSig, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, OpaqueTy,
ProjectionTy, QuantifiedWhereClause, Scalar, TraitRef, Ty, TyExt, TyKind,
WhereClause,
ProjectionTy, QuantifiedWhereClause, Scalar, TraitRef, Ty, TyExt, TyKind, WhereClause,
};
pub struct HirFormatter<'a> {

View File

@ -462,12 +462,6 @@ impl Substitution {
) -> Self {
Substitution(elements.into_iter().casted(interner).collect())
}
/// Return Substs that replace each parameter by itself (i.e. `Ty::Param`).
pub fn type_params(db: &dyn HirDatabase, def: impl Into<GenericDefId>) -> Substitution {
let params = generics(db.upcast(), def.into());
params.type_params_subst(db)
}
}
/// Return an index of a parameter in the generic type parameter list by it's id.
@ -944,7 +938,7 @@ impl Ty {
let param_data = &generic_params.types[id.local_id];
match param_data.provenance {
hir_def::generics::TypeParamProvenance::ArgumentImplTrait => {
let substs = Substitution::type_params(db, id.parent);
let substs = TyBuilder::type_params_subst(db, id.parent);
let predicates = db
.generic_predicates(id.parent)
.into_iter()

View File

@ -470,12 +470,13 @@ impl<'a> TyLoweringContext<'a> {
TypeParamLoweringMode::Placeholder => {
// if we're lowering to placeholders, we have to put
// them in now
let s = Substitution::type_params(
self.db,
let generics = generics(
self.db.upcast(),
self.resolver.generic_def().expect(
"there should be generics if there's a generic param",
),
);
let s = generics.type_params_subst(self.db);
t.substitution.clone().subst_bound_vars(&s)
}
TypeParamLoweringMode::Variable => t.substitution.clone(),
@ -963,7 +964,7 @@ pub(crate) fn trait_environment_query(
// function default implementations (and hypothetical code
// inside consts or type aliases)
cov_mark::hit!(trait_self_implements_self);
let substs = Substitution::type_params(db, trait_id);
let substs = TyBuilder::type_params_subst(db, trait_id);
let trait_ref = TraitRef { trait_id: to_chalk_trait_id(trait_id), substitution: substs };
let pred = WhereClause::Implemented(trait_ref);
let program_clause: chalk_ir::ProgramClause<Interner> = pred.to_chalk(db).cast(&Interner);