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/utils.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'crates/hir_ty/src/utils.rs') diff --git a/crates/hir_ty/src/utils.rs b/crates/hir_ty/src/utils.rs index 42d7af146..b23e91b1b 100644 --- a/crates/hir_ty/src/utils.rs +++ b/crates/hir_ty/src/utils.rs @@ -2,7 +2,7 @@ //! query, but can't be computed directly from `*Data` (ie, which need a `db`). use std::sync::Arc; -use chalk_ir::DebruijnIndex; +use chalk_ir::{BoundVar, DebruijnIndex}; use hir_def::{ adt::VariantData, db::DefDatabase, @@ -16,7 +16,7 @@ use hir_def::{ }; use hir_expand::name::{name, Name}; -use crate::{db::HirDatabase, TraitRef, TypeWalk, WhereClause}; +use crate::{db::HirDatabase, Interner, Substitution, TraitRef, TyKind, TypeWalk, WhereClause}; fn direct_super_traits(db: &dyn DefDatabase, trait_: TraitId) -> 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