From a4d7bdf1c884a9f3dd415a882fa56422adae89bf Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 4 Apr 2021 13:07:06 +0200 Subject: Replace Substitution::bound_vars and ::type_params_for_generics --- crates/hir_ty/src/display.rs | 4 ++-- crates/hir_ty/src/infer.rs | 4 ++-- crates/hir_ty/src/infer/path.rs | 2 +- crates/hir_ty/src/lib.rs | 28 ++-------------------------- crates/hir_ty/src/lower.rs | 24 ++++++++---------------- crates/hir_ty/src/traits/chalk.rs | 10 +++++----- crates/hir_ty/src/utils.rs | 24 ++++++++++++++++++++++-- 7 files changed, 42 insertions(+), 54 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index eb82e1a11..39e0b328d 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -19,7 +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, Substitution, TraitRef, Ty, TyExt, TyKind, + ProjectionTy, QuantifiedWhereClause, Scalar, TraitRef, Ty, TyExt, TyKind, WhereClause, }; @@ -592,7 +592,7 @@ impl HirDisplay for Ty { write!(f, "{}", param_data.name.clone().unwrap_or_else(Name::missing))? } TypeParamProvenance::ArgumentImplTrait => { - let substs = Substitution::type_params_for_generics(f.db, &generics); + let substs = generics.type_params_subst(f.db); let bounds = f .db .generic_predicates(id.parent) diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs index caafeccb8..1b1d4458c 100644 --- a/crates/hir_ty/src/infer.rs +++ b/crates/hir_ty/src/infer.rs @@ -38,7 +38,7 @@ use syntax::SmolStr; use super::{ traits::{DomainGoal, Guidance, Solution}, - InEnvironment, ProjectionTy, Substitution, TraitEnvironment, TraitRef, Ty, TypeWalk, + InEnvironment, ProjectionTy, TraitEnvironment, TraitRef, Ty, TypeWalk, }; use crate::{ db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode, @@ -487,7 +487,7 @@ impl<'a> InferenceContext<'a> { } TypeNs::SelfType(impl_id) => { let generics = crate::utils::generics(self.db.upcast(), impl_id.into()); - let substs = Substitution::type_params_for_generics(self.db, &generics); + let substs = generics.type_params_subst(self.db); let ty = self.db.impl_self_ty(impl_id).subst(&substs); match unresolved { None => { diff --git a/crates/hir_ty/src/infer/path.rs b/crates/hir_ty/src/infer/path.rs index d55ae4900..671ea355f 100644 --- a/crates/hir_ty/src/infer/path.rs +++ b/crates/hir_ty/src/infer/path.rs @@ -80,7 +80,7 @@ impl<'a> InferenceContext<'a> { } ValueNs::ImplSelf(impl_id) => { let generics = crate::utils::generics(self.db.upcast(), impl_id.into()); - let substs = Substitution::type_params_for_generics(self.db, &generics); + let substs = generics.type_params_subst(self.db); let ty = self.db.impl_self_ty(impl_id).subst(&substs); if let Some((AdtId::StructId(struct_id), substs)) = ty.as_adt() { let ty = self.db.value_ty(struct_id.into()).subst(&substs); diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index ebdcc4804..6d6443ca3 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -41,7 +41,7 @@ use hir_def::{ use crate::{ db::HirDatabase, display::HirDisplay, - utils::{generics, make_mut_slice, Generics}, + utils::{generics, make_mut_slice}, }; pub use autoderef::autoderef; @@ -463,34 +463,10 @@ impl Substitution { Substitution(elements.into_iter().casted(interner).collect()) } - /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`). - pub(crate) fn type_params_for_generics( - db: &dyn HirDatabase, - generic_params: &Generics, - ) -> Substitution { - Substitution::from_iter( - &Interner, - generic_params - .iter() - .map(|(id, _)| TyKind::Placeholder(to_placeholder_idx(db, id)).intern(&Interner)), - ) - } - /// 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()); - Substitution::type_params_for_generics(db, ¶ms) - } - - /// Return Substs that replace each parameter by a bound variable. - pub(crate) fn bound_vars(generic_params: &Generics, debruijn: DebruijnIndex) -> Substitution { - Substitution::from_iter( - &Interner, - generic_params - .iter() - .enumerate() - .map(|(idx, _)| TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(&Interner)), - ) + params.type_params_subst(db) } } diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index 644a2eed5..f9a721fdf 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -234,7 +234,7 @@ impl<'a> TyLoweringContext<'a> { let impl_trait_id = ImplTraitId::ReturnTypeImplTrait(func, idx); let opaque_ty_id = self.db.intern_impl_trait_id(impl_trait_id).into(); let generics = generics(self.db.upcast(), func.into()); - let parameters = Substitution::bound_vars(&generics, self.in_binders); + let parameters = generics.bound_vars_subst(self.in_binders); TyKind::Alias(AliasTy::Opaque(OpaqueTy { opaque_ty_id, substitution: parameters, @@ -411,24 +411,16 @@ impl<'a> TyLoweringContext<'a> { TypeNs::SelfType(impl_id) => { let generics = generics(self.db.upcast(), impl_id.into()); let substs = match self.type_param_mode { - TypeParamLoweringMode::Placeholder => { - Substitution::type_params_for_generics(self.db, &generics) - } - TypeParamLoweringMode::Variable => { - Substitution::bound_vars(&generics, self.in_binders) - } + TypeParamLoweringMode::Placeholder => generics.type_params_subst(self.db), + TypeParamLoweringMode::Variable => generics.bound_vars_subst(self.in_binders), }; self.db.impl_self_ty(impl_id).subst(&substs) } TypeNs::AdtSelfType(adt) => { let generics = generics(self.db.upcast(), adt.into()); let substs = match self.type_param_mode { - TypeParamLoweringMode::Placeholder => { - Substitution::type_params_for_generics(self.db, &generics) - } - TypeParamLoweringMode::Variable => { - Substitution::bound_vars(&generics, self.in_binders) - } + TypeParamLoweringMode::Placeholder => generics.type_params_subst(self.db), + TypeParamLoweringMode::Variable => generics.bound_vars_subst(self.in_binders), }; self.db.ty(adt.into()).subst(&substs) } @@ -1060,7 +1052,7 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig { /// function body. fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders { let generics = generics(db.upcast(), def.into()); - let substs = Substitution::bound_vars(&generics, DebruijnIndex::INNERMOST); + let substs = generics.bound_vars_subst(DebruijnIndex::INNERMOST); Binders::new( substs.len(&Interner), TyKind::FnDef(CallableDefId::FunctionId(def).to_chalk(db), substs).intern(&Interner), @@ -1105,7 +1097,7 @@ fn type_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> Binders Vec { let resolver = trait_.resolver(db); @@ -249,6 +249,26 @@ impl Generics { self.parent_generics.as_ref().and_then(|g| g.find_param(param)) } } + + /// Returns a Substitution that replaces each parameter by a bound variable. + pub(crate) fn bound_vars_subst(&self, debruijn: DebruijnIndex) -> Substitution { + Substitution::from_iter( + &Interner, + self.iter() + .enumerate() + .map(|(idx, _)| TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(&Interner)), + ) + } + + /// Returns a Substitution that replaces each parameter by itself (i.e. `Ty::Param`). + pub(crate) fn type_params_subst(&self, db: &dyn HirDatabase) -> Substitution { + Substitution::from_iter( + &Interner, + self.iter().map(|(id, _)| { + TyKind::Placeholder(crate::to_placeholder_idx(db, id)).intern(&Interner) + }), + ) + } } fn parent_generic_def(db: &dyn DefDatabase, def: GenericDefId) -> Option { -- cgit v1.2.3