From ebdfc932e74ff11c8c14c513614212b7c07bf400 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 4 Apr 2021 13:16:16 +0200 Subject: Replace Substitution::type_params --- crates/hir/src/display.rs | 4 ++-- crates/hir/src/lib.rs | 4 ++-- crates/hir_ty/src/builder.rs | 5 +++++ crates/hir_ty/src/display.rs | 3 +-- crates/hir_ty/src/lib.rs | 8 +------- crates/hir_ty/src/lower.rs | 7 ++++--- 6 files changed, 15 insertions(+), 16 deletions(-) (limited to 'crates') diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs index 559ea31a0..993772aac 100644 --- a/crates/hir/src/display.rs +++ b/crates/hir/src/display.rs @@ -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::>(); if !(predicates.is_empty() || f.omit_verbose_types()) { write_bounds_like_dyn_trait_with_prefix(":", &predicates, f)?; diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 42ae53c66..19901ed33 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -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)) } diff --git a/crates/hir_ty/src/builder.rs b/crates/hir_ty/src/builder.rs index ba158a749..4d3b4eade 100644 --- a/crates/hir_ty/src/builder.rs +++ b/crates/hir_ty/src/builder.rs @@ -99,6 +99,11 @@ impl TyBuilder<()> { } } + pub fn type_params_subst(db: &dyn HirDatabase, def: impl Into) -> Substitution { + let params = generics(db.upcast(), def.into()); + params.type_params_subst(db) + } + pub fn subst_for_def(db: &dyn HirDatabase, def: impl Into) -> TyBuilder<()> { let def = def.into(); let params = generics(db.upcast(), def); diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 39e0b328d..385bd9405 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -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> { diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 6d6443ca3..a8c87eadf 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -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) -> 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() diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index f9a721fdf..214655807 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -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 = pred.to_chalk(db).cast(&Interner); -- cgit v1.2.3