aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2021-03-13 18:47:34 +0000
committerFlorian Diebold <[email protected]>2021-03-13 18:47:34 +0000
commit1bf6b7360c3f1d0e20dece5227979bc4d74a352f (patch)
tree13a5ae932c1af88d50bbde849ea6d4c317d196f2 /crates
parent2d69eb131f58dee1bc188b8df8d5cf0ebf9d97f2 (diff)
Use chalk_ir::PlaceholderIndex
Diffstat (limited to 'crates')
-rw-r--r--crates/hir/src/lib.rs2
-rw-r--r--crates/hir_ty/src/db.rs6
-rw-r--r--crates/hir_ty/src/display.rs15
-rw-r--r--crates/hir_ty/src/infer.rs2
-rw-r--r--crates/hir_ty/src/infer/path.rs2
-rw-r--r--crates/hir_ty/src/lib.rs31
-rw-r--r--crates/hir_ty/src/lower.rs17
-rw-r--r--crates/hir_ty/src/traits/chalk/mapping.rs22
8 files changed, 53 insertions, 44 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 7f8f936d3..571b89bc3 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -1384,7 +1384,7 @@ impl TypeParam {
1384 pub fn ty(self, db: &dyn HirDatabase) -> Type { 1384 pub fn ty(self, db: &dyn HirDatabase) -> Type {
1385 let resolver = self.id.parent.resolver(db.upcast()); 1385 let resolver = self.id.parent.resolver(db.upcast());
1386 let krate = self.id.parent.module(db.upcast()).krate(); 1386 let krate = self.id.parent.module(db.upcast()).krate();
1387 let ty = TyKind::Placeholder(self.id).intern(&Interner); 1387 let ty = TyKind::Placeholder(hir_ty::to_placeholder_idx(db, self.id)).intern(&Interner);
1388 Type::new_with_resolver_inner(db, krate, &resolver, ty) 1388 Type::new_with_resolver_inner(db, krate, &resolver, ty)
1389 } 1389 }
1390 1390
diff --git a/crates/hir_ty/src/db.rs b/crates/hir_ty/src/db.rs
index 1b59616ba..c198f6903 100644
--- a/crates/hir_ty/src/db.rs
+++ b/crates/hir_ty/src/db.rs
@@ -81,7 +81,7 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
81 #[salsa::interned] 81 #[salsa::interned]
82 fn intern_callable_def(&self, callable_def: CallableDefId) -> InternedCallableDefId; 82 fn intern_callable_def(&self, callable_def: CallableDefId) -> InternedCallableDefId;
83 #[salsa::interned] 83 #[salsa::interned]
84 fn intern_type_param_id(&self, param_id: TypeParamId) -> GlobalTypeParamId; 84 fn intern_type_param_id(&self, param_id: TypeParamId) -> InternedTypeParamId;
85 #[salsa::interned] 85 #[salsa::interned]
86 fn intern_impl_trait_id(&self, id: OpaqueTyId) -> InternedOpaqueTyId; 86 fn intern_impl_trait_id(&self, id: OpaqueTyId) -> InternedOpaqueTyId;
87 #[salsa::interned] 87 #[salsa::interned]
@@ -149,8 +149,8 @@ fn hir_database_is_object_safe() {
149} 149}
150 150
151#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 151#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
152pub struct GlobalTypeParamId(salsa::InternId); 152pub struct InternedTypeParamId(salsa::InternId);
153impl_intern_key!(GlobalTypeParamId); 153impl_intern_key!(InternedTypeParamId);
154 154
155#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 155#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
156pub struct InternedOpaqueTyId(salsa::InternId); 156pub struct InternedOpaqueTyId(salsa::InternId);
diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs
index b7e85e024..0a13c8636 100644
--- a/crates/hir_ty/src/display.rs
+++ b/crates/hir_ty/src/display.rs
@@ -11,10 +11,10 @@ use hir_def::{
11use hir_expand::name::Name; 11use hir_expand::name::Name;
12 12
13use crate::{ 13use crate::{
14 db::HirDatabase, from_assoc_type_id, from_foreign_def_id, primitive, to_assoc_type_id, 14 db::HirDatabase, from_assoc_type_id, from_foreign_def_id, from_placeholder_idx, primitive,
15 traits::chalk::from_chalk, utils::generics, AdtId, AliasTy, CallableDefId, CallableSig, 15 to_assoc_type_id, traits::chalk::from_chalk, utils::generics, AdtId, AliasTy, CallableDefId,
16 GenericPredicate, Interner, Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, 16 CallableSig, GenericPredicate, Interner, Lifetime, Obligation, OpaqueTy, OpaqueTyId,
17 Substs, TraitRef, Ty, TyKind, 17 ProjectionTy, Scalar, Substs, TraitRef, Ty, TyKind,
18}; 18};
19 19
20pub struct HirFormatter<'a> { 20pub struct HirFormatter<'a> {
@@ -541,7 +541,8 @@ impl HirDisplay for Ty {
541 write!(f, "{{closure}}")?; 541 write!(f, "{{closure}}")?;
542 } 542 }
543 } 543 }
544 TyKind::Placeholder(id) => { 544 TyKind::Placeholder(idx) => {
545 let id = from_placeholder_idx(f.db, *idx);
545 let generics = generics(f.db.upcast(), id.parent); 546 let generics = generics(f.db.upcast(), id.parent);
546 let param_data = &generics.params.types[id.local_id]; 547 let param_data = &generics.params.types[id.local_id];
547 match param_data.provenance { 548 match param_data.provenance {
@@ -549,8 +550,8 @@ impl HirDisplay for Ty {
549 write!(f, "{}", param_data.name.clone().unwrap_or_else(Name::missing))? 550 write!(f, "{}", param_data.name.clone().unwrap_or_else(Name::missing))?
550 } 551 }
551 TypeParamProvenance::ArgumentImplTrait => { 552 TypeParamProvenance::ArgumentImplTrait => {
552 let bounds = f.db.generic_predicates_for_param(*id); 553 let bounds = f.db.generic_predicates_for_param(id);
553 let substs = Substs::type_params_for_generics(&generics); 554 let substs = Substs::type_params_for_generics(f.db, &generics);
554 write_bounds_like_dyn_trait_with_prefix( 555 write_bounds_like_dyn_trait_with_prefix(
555 "impl", 556 "impl",
556 &bounds.iter().map(|b| b.clone().subst(&substs)).collect::<Vec<_>>(), 557 &bounds.iter().map(|b| b.clone().subst(&substs)).collect::<Vec<_>>(),
diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs
index 9d9bf549c..4f7463422 100644
--- a/crates/hir_ty/src/infer.rs
+++ b/crates/hir_ty/src/infer.rs
@@ -454,7 +454,7 @@ impl<'a> InferenceContext<'a> {
454 } 454 }
455 TypeNs::SelfType(impl_id) => { 455 TypeNs::SelfType(impl_id) => {
456 let generics = crate::utils::generics(self.db.upcast(), impl_id.into()); 456 let generics = crate::utils::generics(self.db.upcast(), impl_id.into());
457 let substs = Substs::type_params_for_generics(&generics); 457 let substs = Substs::type_params_for_generics(self.db, &generics);
458 let ty = self.db.impl_self_ty(impl_id).subst(&substs); 458 let ty = self.db.impl_self_ty(impl_id).subst(&substs);
459 match unresolved { 459 match unresolved {
460 None => { 460 None => {
diff --git a/crates/hir_ty/src/infer/path.rs b/crates/hir_ty/src/infer/path.rs
index 392952178..c6681834c 100644
--- a/crates/hir_ty/src/infer/path.rs
+++ b/crates/hir_ty/src/infer/path.rs
@@ -79,7 +79,7 @@ impl<'a> InferenceContext<'a> {
79 } 79 }
80 ValueNs::ImplSelf(impl_id) => { 80 ValueNs::ImplSelf(impl_id) => {
81 let generics = crate::utils::generics(self.db.upcast(), impl_id.into()); 81 let generics = crate::utils::generics(self.db.upcast(), impl_id.into());
82 let substs = Substs::type_params_for_generics(&generics); 82 let substs = Substs::type_params_for_generics(self.db, &generics);
83 let ty = self.db.impl_self_ty(impl_id).subst(&substs); 83 let ty = self.db.impl_self_ty(impl_id).subst(&substs);
84 if let Some((AdtId::StructId(struct_id), substs)) = ty.as_adt() { 84 if let Some((AdtId::StructId(struct_id), substs)) = ty.as_adt() {
85 let ty = self.db.value_ty(struct_id.into()).subst(&substs); 85 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 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}
diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs
index b8b1400eb..78a914cac 100644
--- a/crates/hir_ty/src/lower.rs
+++ b/crates/hir_ty/src/lower.rs
@@ -27,7 +27,7 @@ use stdx::impl_from;
27 27
28use crate::{ 28use crate::{
29 db::HirDatabase, 29 db::HirDatabase,
30 to_assoc_type_id, 30 to_assoc_type_id, to_placeholder_idx,
31 traits::chalk::{Interner, ToChalk}, 31 traits::chalk::{Interner, ToChalk},
32 utils::{ 32 utils::{
33 all_super_trait_refs, associated_type_by_name_including_super_traits, generics, 33 all_super_trait_refs, associated_type_by_name_including_super_traits, generics,
@@ -249,7 +249,9 @@ impl Ty {
249 data.provenance == TypeParamProvenance::ArgumentImplTrait 249 data.provenance == TypeParamProvenance::ArgumentImplTrait
250 }) 250 })
251 .nth(idx as usize) 251 .nth(idx as usize)
252 .map_or(TyKind::Unknown, |(id, _)| TyKind::Placeholder(id)); 252 .map_or(TyKind::Unknown, |(id, _)| {
253 TyKind::Placeholder(to_placeholder_idx(ctx.db, id))
254 });
253 param.intern(&Interner) 255 param.intern(&Interner)
254 } else { 256 } else {
255 TyKind::Unknown.intern(&Interner) 257 TyKind::Unknown.intern(&Interner)
@@ -384,7 +386,9 @@ impl Ty {
384 ctx.resolver.generic_def().expect("generics in scope"), 386 ctx.resolver.generic_def().expect("generics in scope"),
385 ); 387 );
386 match ctx.type_param_mode { 388 match ctx.type_param_mode {
387 TypeParamLoweringMode::Placeholder => TyKind::Placeholder(param_id), 389 TypeParamLoweringMode::Placeholder => {
390 TyKind::Placeholder(to_placeholder_idx(ctx.db, param_id))
391 }
388 TypeParamLoweringMode::Variable => { 392 TypeParamLoweringMode::Variable => {
389 let idx = generics.param_idx(param_id).expect("matching generics"); 393 let idx = generics.param_idx(param_id).expect("matching generics");
390 TyKind::BoundVar(BoundVar::new(ctx.in_binders, idx)) 394 TyKind::BoundVar(BoundVar::new(ctx.in_binders, idx))
@@ -396,7 +400,7 @@ impl Ty {
396 let generics = generics(ctx.db.upcast(), impl_id.into()); 400 let generics = generics(ctx.db.upcast(), impl_id.into());
397 let substs = match ctx.type_param_mode { 401 let substs = match ctx.type_param_mode {
398 TypeParamLoweringMode::Placeholder => { 402 TypeParamLoweringMode::Placeholder => {
399 Substs::type_params_for_generics(&generics) 403 Substs::type_params_for_generics(ctx.db, &generics)
400 } 404 }
401 TypeParamLoweringMode::Variable => { 405 TypeParamLoweringMode::Variable => {
402 Substs::bound_vars(&generics, ctx.in_binders) 406 Substs::bound_vars(&generics, ctx.in_binders)
@@ -408,7 +412,7 @@ impl Ty {
408 let generics = generics(ctx.db.upcast(), adt.into()); 412 let generics = generics(ctx.db.upcast(), adt.into());
409 let substs = match ctx.type_param_mode { 413 let substs = match ctx.type_param_mode {
410 TypeParamLoweringMode::Placeholder => { 414 TypeParamLoweringMode::Placeholder => {
411 Substs::type_params_for_generics(&generics) 415 Substs::type_params_for_generics(ctx.db, &generics)
412 } 416 }
413 TypeParamLoweringMode::Variable => { 417 TypeParamLoweringMode::Variable => {
414 Substs::bound_vars(&generics, ctx.in_binders) 418 Substs::bound_vars(&generics, ctx.in_binders)
@@ -689,8 +693,9 @@ impl GenericPredicate {
689 let generics = generics(ctx.db.upcast(), generic_def); 693 let generics = generics(ctx.db.upcast(), generic_def);
690 let param_id = 694 let param_id =
691 hir_def::TypeParamId { parent: generic_def, local_id: *param_id }; 695 hir_def::TypeParamId { parent: generic_def, local_id: *param_id };
696 let placeholder = to_placeholder_idx(ctx.db, param_id);
692 match ctx.type_param_mode { 697 match ctx.type_param_mode {
693 TypeParamLoweringMode::Placeholder => TyKind::Placeholder(param_id), 698 TypeParamLoweringMode::Placeholder => TyKind::Placeholder(placeholder),
694 TypeParamLoweringMode::Variable => { 699 TypeParamLoweringMode::Variable => {
695 let idx = generics.param_idx(param_id).expect("matching generics"); 700 let idx = generics.param_idx(param_id).expect("matching generics");
696 TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, idx)) 701 TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, idx))
diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs
index 56a30363b..460955756 100644
--- a/crates/hir_ty/src/traits/chalk/mapping.rs
+++ b/crates/hir_ty/src/traits/chalk/mapping.rs
@@ -3,10 +3,7 @@
3//! Chalk (in both directions); plus some helper functions for more specialized 3//! Chalk (in both directions); plus some helper functions for more specialized
4//! conversions. 4//! conversions.
5 5
6use chalk_ir::{ 6use chalk_ir::{cast::Cast, fold::shift::Shift, interner::HasInterner, LifetimeData};
7 cast::Cast, fold::shift::Shift, interner::HasInterner, LifetimeData, PlaceholderIndex,
8 UniverseIndex,
9};
10use chalk_solve::rust_ir; 7use chalk_solve::rust_ir;
11 8
12use base_db::salsa::InternKey; 9use base_db::salsa::InternKey;
@@ -91,14 +88,7 @@ impl ToChalk for Ty {
91 .cast(&Interner) 88 .cast(&Interner)
92 .intern(&Interner) 89 .intern(&Interner)
93 } 90 }
94 TyKind::Placeholder(id) => { 91 TyKind::Placeholder(idx) => idx.to_ty::<Interner>(&Interner),
95 let interned_id = db.intern_type_param_id(id);
96 PlaceholderIndex {
97 ui: UniverseIndex::ROOT,
98 idx: interned_id.as_intern_id().as_usize(),
99 }
100 .to_ty::<Interner>(&Interner)
101 }
102 TyKind::BoundVar(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner), 92 TyKind::BoundVar(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner),
103 TyKind::InferenceVar(..) => panic!("uncanonicalized infer ty"), 93 TyKind::InferenceVar(..) => panic!("uncanonicalized infer ty"),
104 TyKind::Dyn(predicates) => { 94 TyKind::Dyn(predicates) => {
@@ -128,13 +118,7 @@ impl ToChalk for Ty {
128 match chalk.data(&Interner).kind.clone() { 118 match chalk.data(&Interner).kind.clone() {
129 chalk_ir::TyKind::Error => TyKind::Unknown, 119 chalk_ir::TyKind::Error => TyKind::Unknown,
130 chalk_ir::TyKind::Array(ty, _size) => TyKind::Array(Substs::single(from_chalk(db, ty))), 120 chalk_ir::TyKind::Array(ty, _size) => TyKind::Array(Substs::single(from_chalk(db, ty))),
131 chalk_ir::TyKind::Placeholder(idx) => { 121 chalk_ir::TyKind::Placeholder(idx) => TyKind::Placeholder(idx),
132 assert_eq!(idx.ui, UniverseIndex::ROOT);
133 let interned_id = crate::db::GlobalTypeParamId::from_intern_id(
134 crate::salsa::InternId::from(idx.idx),
135 );
136 TyKind::Placeholder(db.lookup_intern_type_param_id(interned_id))
137 }
138 chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => { 122 chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => {
139 let associated_ty = proj.associated_ty_id; 123 let associated_ty = proj.associated_ty_id;
140 let parameters = from_chalk(db, proj.substitution); 124 let parameters = from_chalk(db, proj.substitution);