aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src/lib.rs')
-rw-r--r--crates/hir_ty/src/lib.rs46
1 files changed, 38 insertions, 8 deletions
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs
index e586d73d8..75bf8bcd9 100644
--- a/crates/hir_ty/src/lib.rs
+++ b/crates/hir_ty/src/lib.rs
@@ -28,7 +28,10 @@ mod chalk_ext;
28use std::{iter, mem, sync::Arc}; 28use std::{iter, mem, sync::Arc};
29 29
30use base_db::salsa; 30use base_db::salsa;
31use chalk_ir::cast::{CastTo, Caster}; 31use chalk_ir::{
32 cast::{CastTo, Caster},
33 interner::HasInterner,
34};
32use hir_def::{ 35use hir_def::{
33 builtin_type::BuiltinType, expr::ExprId, type_ref::Rawness, AssocContainerId, FunctionId, 36 builtin_type::BuiltinType, expr::ExprId, type_ref::Rawness, AssocContainerId, FunctionId,
34 GenericDefId, HasModule, LifetimeParamId, Lookup, TraitId, TypeAliasId, TypeParamId, 37 GenericDefId, HasModule, LifetimeParamId, Lookup, TraitId, TypeAliasId, TypeParamId,
@@ -490,13 +493,6 @@ impl Substitution {
490 ) 493 )
491 } 494 }
492 495
493 pub fn build_for_def(db: &dyn HirDatabase, def: impl Into<GenericDefId>) -> SubstsBuilder {
494 let def = def.into();
495 let params = generics(db.upcast(), def);
496 let param_count = params.len();
497 Substitution::builder(param_count)
498 }
499
500 pub(crate) fn build_for_generics(generic_params: &Generics) -> SubstsBuilder { 496 pub(crate) fn build_for_generics(generic_params: &Generics) -> SubstsBuilder {
501 Substitution::builder(generic_params.len()) 497 Substitution::builder(generic_params.len())
502 } 498 }
@@ -894,6 +890,18 @@ impl TyBuilder<()> {
894 } 890 }
895 } 891 }
896 } 892 }
893
894 pub fn subst_for_def(db: &dyn HirDatabase, def: impl Into<GenericDefId>) -> TyBuilder<()> {
895 let def = def.into();
896 let params = generics(db.upcast(), def);
897 let param_count = params.len();
898 TyBuilder::new((), param_count)
899 }
900
901 pub fn build(self) -> Substitution {
902 let ((), subst) = self.build_internal();
903 subst
904 }
897} 905}
898 906
899impl TyBuilder<hir_def::AdtId> { 907impl TyBuilder<hir_def::AdtId> {
@@ -956,6 +964,28 @@ impl TyBuilder<TypeAliasId> {
956 } 964 }
957} 965}
958 966
967impl<T: TypeWalk + HasInterner<Interner = Interner>> TyBuilder<Binders<T>> {
968 fn subst_binders(b: Binders<T>) -> Self {
969 let param_count = b.num_binders;
970 TyBuilder::new(b, param_count)
971 }
972
973 pub fn build(self) -> T {
974 let (b, subst) = self.build_internal();
975 b.subst(&subst)
976 }
977}
978
979impl TyBuilder<Binders<Ty>> {
980 pub fn def_ty(db: &dyn HirDatabase, def: TyDefId) -> TyBuilder<Binders<Ty>> {
981 TyBuilder::subst_binders(db.ty(def.into()))
982 }
983
984 pub fn impl_self_ty(db: &dyn HirDatabase, def: hir_def::ImplId) -> TyBuilder<Binders<Ty>> {
985 TyBuilder::subst_binders(db.impl_self_ty(def))
986 }
987}
988
959impl Ty { 989impl Ty {
960 pub fn as_reference(&self) -> Option<(&Ty, Mutability)> { 990 pub fn as_reference(&self) -> Option<(&Ty, Mutability)> {
961 match self.kind(&Interner) { 991 match self.kind(&Interner) {