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.rs31
1 files changed, 25 insertions, 6 deletions
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs
index ec2010e4b..d1c018283 100644
--- a/crates/hir_ty/src/lib.rs
+++ b/crates/hir_ty/src/lib.rs
@@ -54,6 +54,7 @@ pub type ForeignDefId = chalk_ir::ForeignDefId<Interner>;
54pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>; 54pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>;
55pub type FnDefId = chalk_ir::FnDefId<Interner>; 55pub type FnDefId = chalk_ir::FnDefId<Interner>;
56pub type ClosureId = chalk_ir::ClosureId<Interner>; 56pub type ClosureId = chalk_ir::ClosureId<Interner>;
57pub type PlaceholderIndex = chalk_ir::PlaceholderIndex;
57 58
58#[derive(Clone, PartialEq, Eq, Debug, Hash)] 59#[derive(Clone, PartialEq, Eq, Debug, Hash)]
59pub enum Lifetime { 60pub enum Lifetime {
@@ -220,7 +221,7 @@ pub enum TyKind {
220 /// {}` when we're type-checking the body of that function. In this 221 /// {}` when we're type-checking the body of that function. In this
221 /// situation, we know this stands for *some* type, but don't know the exact 222 /// situation, we know this stands for *some* type, but don't know the exact
222 /// type. 223 /// type.
223 Placeholder(TypeParamId), 224 Placeholder(PlaceholderIndex),
224 225
225 /// A bound type variable. This is used in various places: when representing 226 /// A bound type variable. This is used in various places: when representing
226 /// some polymorphic type like the type of function `fn f<T>`, the type 227 /// some polymorphic type like the type of function `fn f<T>`, the type
@@ -310,11 +311,14 @@ impl Substs {
310 } 311 }
311 312
312 /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`). 313 /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`).
313 pub(crate) fn type_params_for_generics(generic_params: &Generics) -> Substs { 314 pub(crate) fn type_params_for_generics(
315 db: &dyn HirDatabase,
316 generic_params: &Generics,
317 ) -> Substs {
314 Substs( 318 Substs(
315 generic_params 319 generic_params
316 .iter() 320 .iter()
317 .map(|(id, _)| TyKind::Placeholder(id).intern(&Interner)) 321 .map(|(id, _)| TyKind::Placeholder(to_placeholder_idx(db, id)).intern(&Interner))
318 .collect(), 322 .collect(),
319 ) 323 )
320 } 324 }
@@ -322,7 +326,7 @@ impl Substs {
322 /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`). 326 /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`).
323 pub fn type_params(db: &dyn HirDatabase, def: impl Into<GenericDefId>) -> Substs { 327 pub fn type_params(db: &dyn HirDatabase, def: impl Into<GenericDefId>) -> Substs {
324 let params = generics(db.upcast(), def.into()); 328 let params = generics(db.upcast(), def.into());
325 Substs::type_params_for_generics(&params) 329 Substs::type_params_for_generics(db, &params)
326 } 330 }
327 331
328 /// Return Substs that replace each parameter by a bound variable. 332 /// Return Substs that replace each parameter by a bound variable.
@@ -909,13 +913,14 @@ impl Ty {
909 913
910 predicates.map(|it| it.value) 914 predicates.map(|it| it.value)
911 } 915 }
912 TyKind::Placeholder(id) => { 916 TyKind::Placeholder(idx) => {
917 let id = from_placeholder_idx(db, *idx);
913 let generic_params = db.generic_params(id.parent); 918 let generic_params = db.generic_params(id.parent);
914 let param_data = &generic_params.types[id.local_id]; 919 let param_data = &generic_params.types[id.local_id];
915 match param_data.provenance { 920 match param_data.provenance {
916 hir_def::generics::TypeParamProvenance::ArgumentImplTrait => { 921 hir_def::generics::TypeParamProvenance::ArgumentImplTrait => {
917 let predicates = db 922 let predicates = db
918 .generic_predicates_for_param(*id) 923 .generic_predicates_for_param(id)
919 .into_iter() 924 .into_iter()
920 .map(|pred| pred.value.clone()) 925 .map(|pred| pred.value.clone())
921 .collect_vec(); 926 .collect_vec();
@@ -1148,3 +1153,17 @@ pub fn to_assoc_type_id(id: TypeAliasId) -> AssocTypeId {
1148pub fn from_assoc_type_id(id: AssocTypeId) -> TypeAliasId { 1153pub fn from_assoc_type_id(id: AssocTypeId) -> TypeAliasId {
1149 salsa::InternKey::from_intern_id(id.0) 1154 salsa::InternKey::from_intern_id(id.0)
1150} 1155}
1156
1157pub fn from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> TypeParamId {
1158 assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT);
1159 let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx));
1160 db.lookup_intern_type_param_id(interned_id)
1161}
1162
1163pub fn to_placeholder_idx(db: &dyn HirDatabase, id: TypeParamId) -> PlaceholderIndex {
1164 let interned_id = db.intern_type_param_id(id);
1165 PlaceholderIndex {
1166 ui: chalk_ir::UniverseIndex::ROOT,
1167 idx: salsa::InternKey::as_intern_id(&interned_id).as_usize(),
1168 }
1169}