aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/traits/chalk/mapping.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-03-13 16:58:04 +0000
committerGitHub <[email protected]>2021-03-13 16:58:04 +0000
commit6ab405f2e9861c34ab9e1ad86af6604523745cef (patch)
treea9c674c83723e2ed145793f72450c873da62a6fc /crates/hir_ty/src/traits/chalk/mapping.rs
parentd4533785698d439417bc5474fecd62a7f1707368 (diff)
parent9719ce9fc731a400c9744ba1a6569e978c1a97e7 (diff)
Merge #7998
7998: Use more Chalk IDs r=flodiebold a=flodiebold Co-authored-by: Florian Diebold <[email protected]>
Diffstat (limited to 'crates/hir_ty/src/traits/chalk/mapping.rs')
-rw-r--r--crates/hir_ty/src/traits/chalk/mapping.rs76
1 files changed, 18 insertions, 58 deletions
diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs
index 44cfb9359..23ef07d77 100644
--- a/crates/hir_ty/src/traits/chalk/mapping.rs
+++ b/crates/hir_ty/src/traits/chalk/mapping.rs
@@ -14,6 +14,7 @@ use hir_def::{AssocContainerId, GenericDefId, Lookup, TypeAliasId};
14 14
15use crate::{ 15use crate::{
16 db::HirDatabase, 16 db::HirDatabase,
17 from_assoc_type_id,
17 primitive::UintTy, 18 primitive::UintTy,
18 traits::{Canonical, Obligation}, 19 traits::{Canonical, Obligation},
19 AliasTy, CallableDefId, FnPointer, FnSig, GenericPredicate, InEnvironment, OpaqueTy, 20 AliasTy, CallableDefId, FnPointer, FnSig, GenericPredicate, InEnvironment, OpaqueTy,
@@ -38,9 +39,7 @@ impl ToChalk for Ty {
38 }) 39 })
39 .intern(&Interner) 40 .intern(&Interner)
40 } 41 }
41 TyKind::AssociatedType(type_alias, substs) => { 42 TyKind::AssociatedType(assoc_type_id, substs) => {
42 let assoc_type = TypeAliasAsAssocType(type_alias);
43 let assoc_type_id = assoc_type.to_chalk(db);
44 let substitution = substs.to_chalk(db); 43 let substitution = substs.to_chalk(db);
45 chalk_ir::TyKind::AssociatedType(assoc_type_id, substitution).intern(&Interner) 44 chalk_ir::TyKind::AssociatedType(assoc_type_id, substitution).intern(&Interner)
46 } 45 }
@@ -51,11 +50,7 @@ impl ToChalk for Ty {
51 chalk_ir::TyKind::OpaqueType(id, substitution).intern(&Interner) 50 chalk_ir::TyKind::OpaqueType(id, substitution).intern(&Interner)
52 } 51 }
53 52
54 TyKind::ForeignType(type_alias) => { 53 TyKind::ForeignType(id) => chalk_ir::TyKind::Foreign(id).intern(&Interner),
55 let foreign_type = TypeAliasAsForeignType(type_alias);
56 let foreign_type_id = foreign_type.to_chalk(db);
57 chalk_ir::TyKind::Foreign(foreign_type_id).intern(&Interner)
58 }
59 54
60 TyKind::Scalar(scalar) => chalk_ir::TyKind::Scalar(scalar).intern(&Interner), 55 TyKind::Scalar(scalar) => chalk_ir::TyKind::Scalar(scalar).intern(&Interner),
61 56
@@ -71,8 +66,7 @@ impl ToChalk for Ty {
71 chalk_ir::TyKind::Slice(substs[0].clone().to_chalk(db)).intern(&Interner) 66 chalk_ir::TyKind::Slice(substs[0].clone().to_chalk(db)).intern(&Interner)
72 } 67 }
73 TyKind::Str => chalk_ir::TyKind::Str.intern(&Interner), 68 TyKind::Str => chalk_ir::TyKind::Str.intern(&Interner),
74 TyKind::FnDef(callable_def, substs) => { 69 TyKind::FnDef(id, substs) => {
75 let id = callable_def.to_chalk(db);
76 let substitution = substs.to_chalk(db); 70 let substitution = substs.to_chalk(db);
77 chalk_ir::TyKind::FnDef(id, substitution).intern(&Interner) 71 chalk_ir::TyKind::FnDef(id, substitution).intern(&Interner)
78 } 72 }
@@ -89,7 +83,7 @@ impl ToChalk for Ty {
89 chalk_ir::TyKind::Adt(adt_id, substitution).intern(&Interner) 83 chalk_ir::TyKind::Adt(adt_id, substitution).intern(&Interner)
90 } 84 }
91 TyKind::Alias(AliasTy::Projection(proj_ty)) => { 85 TyKind::Alias(AliasTy::Projection(proj_ty)) => {
92 let associated_ty_id = TypeAliasAsAssocType(proj_ty.associated_ty).to_chalk(db); 86 let associated_ty_id = proj_ty.associated_ty;
93 let substitution = proj_ty.parameters.to_chalk(db); 87 let substitution = proj_ty.parameters.to_chalk(db);
94 chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy { 88 chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy {
95 associated_ty_id, 89 associated_ty_id,
@@ -143,8 +137,7 @@ impl ToChalk for Ty {
143 TyKind::Placeholder(db.lookup_intern_type_param_id(interned_id)) 137 TyKind::Placeholder(db.lookup_intern_type_param_id(interned_id))
144 } 138 }
145 chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => { 139 chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => {
146 let associated_ty = 140 let associated_ty = proj.associated_ty_id;
147 from_chalk::<TypeAliasAsAssocType, _>(db, proj.associated_ty_id).0;
148 let parameters = from_chalk(db, proj.substitution); 141 let parameters = from_chalk(db, proj.substitution);
149 TyKind::Alias(AliasTy::Projection(ProjectionTy { associated_ty, parameters })) 142 TyKind::Alias(AliasTy::Projection(ProjectionTy { associated_ty, parameters }))
150 } 143 }
@@ -184,10 +177,9 @@ impl ToChalk for Ty {
184 } 177 }
185 178
186 chalk_ir::TyKind::Adt(adt_id, subst) => TyKind::Adt(adt_id, from_chalk(db, subst)), 179 chalk_ir::TyKind::Adt(adt_id, subst) => TyKind::Adt(adt_id, from_chalk(db, subst)),
187 chalk_ir::TyKind::AssociatedType(type_id, subst) => TyKind::AssociatedType( 180 chalk_ir::TyKind::AssociatedType(type_id, subst) => {
188 from_chalk::<TypeAliasAsAssocType, _>(db, type_id).0, 181 TyKind::AssociatedType(type_id, from_chalk(db, subst))
189 from_chalk(db, subst), 182 }
190 ),
191 183
192 chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => { 184 chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => {
193 TyKind::OpaqueType(from_chalk(db, opaque_type_id), from_chalk(db, subst)) 185 TyKind::OpaqueType(from_chalk(db, opaque_type_id), from_chalk(db, subst))
@@ -208,7 +200,7 @@ impl ToChalk for Ty {
208 chalk_ir::TyKind::Never => TyKind::Never, 200 chalk_ir::TyKind::Never => TyKind::Never,
209 201
210 chalk_ir::TyKind::FnDef(fn_def_id, subst) => { 202 chalk_ir::TyKind::FnDef(fn_def_id, subst) => {
211 TyKind::FnDef(from_chalk(db, fn_def_id), from_chalk(db, subst)) 203 TyKind::FnDef(fn_def_id, from_chalk(db, subst))
212 } 204 }
213 205
214 chalk_ir::TyKind::Closure(id, subst) => { 206 chalk_ir::TyKind::Closure(id, subst) => {
@@ -217,9 +209,7 @@ impl ToChalk for Ty {
217 TyKind::Closure(def, expr, from_chalk(db, subst)) 209 TyKind::Closure(def, expr, from_chalk(db, subst))
218 } 210 }
219 211
220 chalk_ir::TyKind::Foreign(foreign_def_id) => { 212 chalk_ir::TyKind::Foreign(foreign_def_id) => TyKind::ForeignType(foreign_def_id),
221 TyKind::ForeignType(from_chalk::<TypeAliasAsForeignType, _>(db, foreign_def_id).0)
222 }
223 chalk_ir::TyKind::Generator(_, _) => unimplemented!(), // FIXME 213 chalk_ir::TyKind::Generator(_, _) => unimplemented!(), // FIXME
224 chalk_ir::TyKind::GeneratorWitness(_, _) => unimplemented!(), // FIXME 214 chalk_ir::TyKind::GeneratorWitness(_, _) => unimplemented!(), // FIXME
225 } 215 }
@@ -338,34 +328,6 @@ impl ToChalk for CallableDefId {
338 } 328 }
339} 329}
340 330
341pub(crate) struct TypeAliasAsAssocType(pub(crate) TypeAliasId);
342
343impl ToChalk for TypeAliasAsAssocType {
344 type Chalk = AssocTypeId;
345
346 fn to_chalk(self, _db: &dyn HirDatabase) -> AssocTypeId {
347 chalk_ir::AssocTypeId(self.0.as_intern_id())
348 }
349
350 fn from_chalk(_db: &dyn HirDatabase, assoc_type_id: AssocTypeId) -> TypeAliasAsAssocType {
351 TypeAliasAsAssocType(InternKey::from_intern_id(assoc_type_id.0))
352 }
353}
354
355pub(crate) struct TypeAliasAsForeignType(pub(crate) TypeAliasId);
356
357impl ToChalk for TypeAliasAsForeignType {
358 type Chalk = ForeignDefId;
359
360 fn to_chalk(self, _db: &dyn HirDatabase) -> ForeignDefId {
361 chalk_ir::ForeignDefId(self.0.as_intern_id())
362 }
363
364 fn from_chalk(_db: &dyn HirDatabase, foreign_def_id: ForeignDefId) -> TypeAliasAsForeignType {
365 TypeAliasAsForeignType(InternKey::from_intern_id(foreign_def_id.0))
366 }
367}
368
369pub(crate) struct TypeAliasAsValue(pub(crate) TypeAliasId); 331pub(crate) struct TypeAliasAsValue(pub(crate) TypeAliasId);
370 332
371impl ToChalk for TypeAliasAsValue { 333impl ToChalk for TypeAliasAsValue {
@@ -447,7 +409,7 @@ impl ToChalk for ProjectionTy {
447 409
448 fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::ProjectionTy<Interner> { 410 fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::ProjectionTy<Interner> {
449 chalk_ir::ProjectionTy { 411 chalk_ir::ProjectionTy {
450 associated_ty_id: TypeAliasAsAssocType(self.associated_ty).to_chalk(db), 412 associated_ty_id: self.associated_ty,
451 substitution: self.parameters.to_chalk(db), 413 substitution: self.parameters.to_chalk(db),
452 } 414 }
453 } 415 }
@@ -457,11 +419,7 @@ impl ToChalk for ProjectionTy {
457 projection_ty: chalk_ir::ProjectionTy<Interner>, 419 projection_ty: chalk_ir::ProjectionTy<Interner>,
458 ) -> ProjectionTy { 420 ) -> ProjectionTy {
459 ProjectionTy { 421 ProjectionTy {
460 associated_ty: from_chalk::<TypeAliasAsAssocType, _>( 422 associated_ty: projection_ty.associated_ty_id,
461 db,
462 projection_ty.associated_ty_id,
463 )
464 .0,
465 parameters: from_chalk(db, projection_ty.substitution), 423 parameters: from_chalk(db, projection_ty.substitution),
466 } 424 }
467 } 425 }
@@ -615,7 +573,10 @@ pub(super) fn generic_predicate_to_inline_bound(
615 if &proj.projection_ty.parameters[0] != self_ty { 573 if &proj.projection_ty.parameters[0] != self_ty {
616 return None; 574 return None;
617 } 575 }
618 let trait_ = match proj.projection_ty.associated_ty.lookup(db.upcast()).container { 576 let trait_ = match from_assoc_type_id(proj.projection_ty.associated_ty)
577 .lookup(db.upcast())
578 .container
579 {
619 AssocContainerId::TraitId(t) => t, 580 AssocContainerId::TraitId(t) => t,
620 _ => panic!("associated type not in trait"), 581 _ => panic!("associated type not in trait"),
621 }; 582 };
@@ -626,8 +587,7 @@ pub(super) fn generic_predicate_to_inline_bound(
626 let alias_eq_bound = rust_ir::AliasEqBound { 587 let alias_eq_bound = rust_ir::AliasEqBound {
627 value: proj.ty.clone().to_chalk(db), 588 value: proj.ty.clone().to_chalk(db),
628 trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self }, 589 trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self },
629 associated_ty_id: TypeAliasAsAssocType(proj.projection_ty.associated_ty) 590 associated_ty_id: proj.projection_ty.associated_ty,
630 .to_chalk(db),
631 parameters: Vec::new(), // FIXME we don't support generic associated types yet 591 parameters: Vec::new(), // FIXME we don't support generic associated types yet
632 }; 592 };
633 Some(rust_ir::InlineBound::AliasEqBound(alias_eq_bound)) 593 Some(rust_ir::InlineBound::AliasEqBound(alias_eq_bound))