diff options
author | Florian Diebold <[email protected]> | 2021-03-13 16:36:07 +0000 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2021-03-13 16:56:48 +0000 |
commit | 19664e276aba21a42cad5351a2c91995d1ce5d52 (patch) | |
tree | 34a0c9ed9c374605f59ab839a5c8193b440c7a2a /crates | |
parent | dfafcd926a4cc9b09aba0eb3cc5275a4abe633b9 (diff) |
Use chalk_ir::AssocTypeId
Diffstat (limited to 'crates')
-rw-r--r-- | crates/hir/src/lib.rs | 7 | ||||
-rw-r--r-- | crates/hir_ty/src/autoderef.rs | 3 | ||||
-rw-r--r-- | crates/hir_ty/src/display.rs | 28 | ||||
-rw-r--r-- | crates/hir_ty/src/infer.rs | 7 | ||||
-rw-r--r-- | crates/hir_ty/src/infer/expr.rs | 7 | ||||
-rw-r--r-- | crates/hir_ty/src/lib.rs | 28 | ||||
-rw-r--r-- | crates/hir_ty/src/lower.rs | 10 | ||||
-rw-r--r-- | crates/hir_ty/src/traits/chalk.rs | 17 | ||||
-rw-r--r-- | crates/hir_ty/src/traits/chalk/mapping.rs | 47 | ||||
-rw-r--r-- | crates/hir_ty/src/traits/chalk/tls.rs | 9 |
10 files changed, 87 insertions, 76 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 7b9de11ed..f15fb1a82 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs | |||
@@ -51,7 +51,7 @@ use hir_expand::{diagnostics::DiagnosticSink, name::name, MacroDefKind}; | |||
51 | use hir_ty::{ | 51 | use hir_ty::{ |
52 | autoderef, | 52 | autoderef, |
53 | display::{write_bounds_like_dyn_trait_with_prefix, HirDisplayError, HirFormatter}, | 53 | display::{write_bounds_like_dyn_trait_with_prefix, HirDisplayError, HirFormatter}, |
54 | method_resolution, | 54 | method_resolution, to_assoc_type_id, |
55 | traits::{FnTrait, Solution, SolutionVariables}, | 55 | traits::{FnTrait, Solution, SolutionVariables}, |
56 | AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, DebruijnIndex, GenericPredicate, | 56 | AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, DebruijnIndex, GenericPredicate, |
57 | InEnvironment, Interner, Obligation, ProjectionPredicate, ProjectionTy, Scalar, Substs, | 57 | InEnvironment, Interner, Obligation, ProjectionPredicate, ProjectionTy, Scalar, Substs, |
@@ -1683,7 +1683,10 @@ impl Type { | |||
1683 | .fill(args.iter().map(|t| t.ty.value.clone())) | 1683 | .fill(args.iter().map(|t| t.ty.value.clone())) |
1684 | .build(); | 1684 | .build(); |
1685 | let predicate = ProjectionPredicate { | 1685 | let predicate = ProjectionPredicate { |
1686 | projection_ty: ProjectionTy { associated_ty: alias.id, parameters: subst }, | 1686 | projection_ty: ProjectionTy { |
1687 | associated_ty: to_assoc_type_id(alias.id), | ||
1688 | parameters: subst, | ||
1689 | }, | ||
1687 | ty: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)).intern(&Interner), | 1690 | ty: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)).intern(&Interner), |
1688 | }; | 1691 | }; |
1689 | let goal = Canonical { | 1692 | let goal = Canonical { |
diff --git a/crates/hir_ty/src/autoderef.rs b/crates/hir_ty/src/autoderef.rs index 09009a3d8..d739d5d60 100644 --- a/crates/hir_ty/src/autoderef.rs +++ b/crates/hir_ty/src/autoderef.rs | |||
@@ -12,6 +12,7 @@ use log::{info, warn}; | |||
12 | 12 | ||
13 | use crate::{ | 13 | use crate::{ |
14 | db::HirDatabase, | 14 | db::HirDatabase, |
15 | to_assoc_type_id, | ||
15 | traits::{InEnvironment, Solution}, | 16 | traits::{InEnvironment, Solution}, |
16 | utils::generics, | 17 | utils::generics, |
17 | BoundVar, Canonical, DebruijnIndex, Interner, Obligation, Substs, TraitRef, Ty, TyKind, | 18 | BoundVar, Canonical, DebruijnIndex, Interner, Obligation, Substs, TraitRef, Ty, TyKind, |
@@ -83,7 +84,7 @@ fn deref_by_trait( | |||
83 | let projection = super::traits::ProjectionPredicate { | 84 | let projection = super::traits::ProjectionPredicate { |
84 | ty: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, ty.value.kinds.len())) | 85 | ty: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, ty.value.kinds.len())) |
85 | .intern(&Interner), | 86 | .intern(&Interner), |
86 | projection_ty: super::ProjectionTy { associated_ty: target, parameters }, | 87 | projection_ty: super::ProjectionTy { associated_ty: to_assoc_type_id(target), parameters }, |
87 | }; | 88 | }; |
88 | 89 | ||
89 | let obligation = super::Obligation::Projection(projection); | 90 | let obligation = super::Obligation::Projection(projection); |
diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 9b1e2ad05..2022069d8 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs | |||
@@ -11,9 +11,9 @@ use hir_def::{ | |||
11 | use hir_expand::name::Name; | 11 | use hir_expand::name::Name; |
12 | 12 | ||
13 | use crate::{ | 13 | use crate::{ |
14 | db::HirDatabase, from_foreign_def_id, primitive, utils::generics, AdtId, AliasTy, | 14 | db::HirDatabase, from_assoc_type_id, from_foreign_def_id, primitive, to_assoc_type_id, |
15 | CallableDefId, CallableSig, GenericPredicate, Interner, Lifetime, Obligation, OpaqueTy, | 15 | utils::generics, AdtId, AliasTy, CallableDefId, CallableSig, GenericPredicate, Interner, |
16 | OpaqueTyId, ProjectionTy, Scalar, Substs, TraitRef, Ty, TyKind, | 16 | Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, Substs, TraitRef, Ty, TyKind, |
17 | }; | 17 | }; |
18 | 18 | ||
19 | pub struct HirFormatter<'a> { | 19 | pub struct HirFormatter<'a> { |
@@ -256,7 +256,7 @@ impl HirDisplay for ProjectionTy { | |||
256 | f.write_joined(&self.parameters[1..], ", ")?; | 256 | f.write_joined(&self.parameters[1..], ", ")?; |
257 | write!(f, ">")?; | 257 | write!(f, ">")?; |
258 | } | 258 | } |
259 | write!(f, ">::{}", f.db.type_alias_data(self.associated_ty).name)?; | 259 | write!(f, ">::{}", f.db.type_alias_data(from_assoc_type_id(self.associated_ty)).name)?; |
260 | Ok(()) | 260 | Ok(()) |
261 | } | 261 | } |
262 | } | 262 | } |
@@ -467,13 +467,14 @@ impl HirDisplay for Ty { | |||
467 | } | 467 | } |
468 | } | 468 | } |
469 | } | 469 | } |
470 | TyKind::AssociatedType(type_alias, parameters) => { | 470 | TyKind::AssociatedType(assoc_type_id, parameters) => { |
471 | let type_alias = from_assoc_type_id(*assoc_type_id); | ||
471 | let trait_ = match type_alias.lookup(f.db.upcast()).container { | 472 | let trait_ = match type_alias.lookup(f.db.upcast()).container { |
472 | AssocContainerId::TraitId(it) => it, | 473 | AssocContainerId::TraitId(it) => it, |
473 | _ => panic!("not an associated type"), | 474 | _ => panic!("not an associated type"), |
474 | }; | 475 | }; |
475 | let trait_ = f.db.trait_data(trait_); | 476 | let trait_ = f.db.trait_data(trait_); |
476 | let type_alias_data = f.db.type_alias_data(*type_alias); | 477 | let type_alias_data = f.db.type_alias_data(type_alias); |
477 | 478 | ||
478 | // Use placeholder associated types when the target is test (https://rust-lang.github.io/chalk/book/clauses/type_equality.html#placeholder-associated-types) | 479 | // Use placeholder associated types when the target is test (https://rust-lang.github.io/chalk/book/clauses/type_equality.html#placeholder-associated-types) |
479 | if f.display_target.is_test() { | 480 | if f.display_target.is_test() { |
@@ -484,8 +485,10 @@ impl HirDisplay for Ty { | |||
484 | write!(f, ">")?; | 485 | write!(f, ">")?; |
485 | } | 486 | } |
486 | } else { | 487 | } else { |
487 | let projection_ty = | 488 | let projection_ty = ProjectionTy { |
488 | ProjectionTy { associated_ty: *type_alias, parameters: parameters.clone() }; | 489 | associated_ty: to_assoc_type_id(type_alias), |
490 | parameters: parameters.clone(), | ||
491 | }; | ||
489 | 492 | ||
490 | projection_ty.hir_fmt(f)?; | 493 | projection_ty.hir_fmt(f)?; |
491 | } | 494 | } |
@@ -697,7 +700,9 @@ fn write_bounds_like_dyn_trait( | |||
697 | write!(f, "<")?; | 700 | write!(f, "<")?; |
698 | angle_open = true; | 701 | angle_open = true; |
699 | } | 702 | } |
700 | let type_alias = f.db.type_alias_data(projection_pred.projection_ty.associated_ty); | 703 | let type_alias = f.db.type_alias_data(from_assoc_type_id( |
704 | projection_pred.projection_ty.associated_ty, | ||
705 | )); | ||
701 | write!(f, "{} = ", type_alias.name)?; | 706 | write!(f, "{} = ", type_alias.name)?; |
702 | projection_pred.ty.hir_fmt(f)?; | 707 | projection_pred.ty.hir_fmt(f)?; |
703 | } | 708 | } |
@@ -768,7 +773,10 @@ impl HirDisplay for GenericPredicate { | |||
768 | write!( | 773 | write!( |
769 | f, | 774 | f, |
770 | ">::{} = ", | 775 | ">::{} = ", |
771 | f.db.type_alias_data(projection_pred.projection_ty.associated_ty).name, | 776 | f.db.type_alias_data(from_assoc_type_id( |
777 | projection_pred.projection_ty.associated_ty | ||
778 | )) | ||
779 | .name, | ||
772 | )?; | 780 | )?; |
773 | projection_pred.ty.hir_fmt(f)?; | 781 | projection_pred.ty.hir_fmt(f)?; |
774 | } | 782 | } |
diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs index acde99b04..9d9bf549c 100644 --- a/crates/hir_ty/src/infer.rs +++ b/crates/hir_ty/src/infer.rs | |||
@@ -42,7 +42,7 @@ use super::{ | |||
42 | }; | 42 | }; |
43 | use crate::{ | 43 | use crate::{ |
44 | db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode, | 44 | db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode, |
45 | AliasTy, Interner, TyKind, | 45 | to_assoc_type_id, AliasTy, Interner, TyKind, |
46 | }; | 46 | }; |
47 | 47 | ||
48 | pub(crate) use unify::unify; | 48 | pub(crate) use unify::unify; |
@@ -382,7 +382,10 @@ impl<'a> InferenceContext<'a> { | |||
382 | let trait_ref = TraitRef { trait_, substs: substs.clone() }; | 382 | let trait_ref = TraitRef { trait_, substs: substs.clone() }; |
383 | let projection = ProjectionPredicate { | 383 | let projection = ProjectionPredicate { |
384 | ty: ty.clone(), | 384 | ty: ty.clone(), |
385 | projection_ty: ProjectionTy { associated_ty: res_assoc_ty, parameters: substs }, | 385 | projection_ty: ProjectionTy { |
386 | associated_ty: to_assoc_type_id(res_assoc_ty), | ||
387 | parameters: substs, | ||
388 | }, | ||
386 | }; | 389 | }; |
387 | self.obligations.push(Obligation::Trait(trait_ref)); | 390 | self.obligations.push(Obligation::Trait(trait_ref)); |
388 | self.obligations.push(Obligation::Projection(projection)); | 391 | self.obligations.push(Obligation::Projection(projection)); |
diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 4e77f22fd..a72424dc7 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs | |||
@@ -18,6 +18,7 @@ use crate::{ | |||
18 | lower::lower_to_chalk_mutability, | 18 | lower::lower_to_chalk_mutability, |
19 | method_resolution, op, | 19 | method_resolution, op, |
20 | primitive::{self, UintTy}, | 20 | primitive::{self, UintTy}, |
21 | to_assoc_type_id, | ||
21 | traits::{FnTrait, InEnvironment}, | 22 | traits::{FnTrait, InEnvironment}, |
22 | utils::{generics, variant_data, Generics}, | 23 | utils::{generics, variant_data, Generics}, |
23 | AdtId, Binders, CallableDefId, FnPointer, FnSig, Interner, Obligation, OpaqueTyId, Rawness, | 24 | AdtId, Binders, CallableDefId, FnPointer, FnSig, Interner, Obligation, OpaqueTyId, Rawness, |
@@ -97,8 +98,10 @@ impl<'a> InferenceContext<'a> { | |||
97 | }); | 98 | }); |
98 | if self.db.trait_solve(krate, goal.value).is_some() { | 99 | if self.db.trait_solve(krate, goal.value).is_some() { |
99 | self.obligations.push(implements_fn_trait); | 100 | self.obligations.push(implements_fn_trait); |
100 | let output_proj_ty = | 101 | let output_proj_ty = crate::ProjectionTy { |
101 | crate::ProjectionTy { associated_ty: output_assoc_type, parameters: substs }; | 102 | associated_ty: to_assoc_type_id(output_assoc_type), |
103 | parameters: substs, | ||
104 | }; | ||
102 | let return_ty = self.normalize_projection_ty(output_proj_ty); | 105 | let return_ty = self.normalize_projection_ty(output_proj_ty); |
103 | Some((arg_tys, return_ty)) | 106 | Some((arg_tys, return_ty)) |
104 | } else { | 107 | } else { |
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 94cbb21ca..5752ddc4e 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs | |||
@@ -52,6 +52,7 @@ pub use chalk_ir::{AdtId, BoundVar, DebruijnIndex, Mutability, Scalar, TyVariabl | |||
52 | pub use crate::traits::chalk::Interner; | 52 | pub use crate::traits::chalk::Interner; |
53 | 53 | ||
54 | pub type ForeignDefId = chalk_ir::ForeignDefId<Interner>; | 54 | pub type ForeignDefId = chalk_ir::ForeignDefId<Interner>; |
55 | pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>; | ||
55 | 56 | ||
56 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] | 57 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
57 | pub enum Lifetime { | 58 | pub enum Lifetime { |
@@ -70,7 +71,7 @@ pub struct OpaqueTy { | |||
70 | /// trait and all its parameters are fully known. | 71 | /// trait and all its parameters are fully known. |
71 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] | 72 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
72 | pub struct ProjectionTy { | 73 | pub struct ProjectionTy { |
73 | pub associated_ty: TypeAliasId, | 74 | pub associated_ty: AssocTypeId, |
74 | pub parameters: Substs, | 75 | pub parameters: Substs, |
75 | } | 76 | } |
76 | 77 | ||
@@ -80,7 +81,7 @@ impl ProjectionTy { | |||
80 | } | 81 | } |
81 | 82 | ||
82 | fn trait_(&self, db: &dyn HirDatabase) -> TraitId { | 83 | fn trait_(&self, db: &dyn HirDatabase) -> TraitId { |
83 | match self.associated_ty.lookup(db.upcast()).container { | 84 | match from_assoc_type_id(self.associated_ty).lookup(db.upcast()).container { |
84 | AssocContainerId::TraitId(it) => it, | 85 | AssocContainerId::TraitId(it) => it, |
85 | _ => panic!("projection ty without parent trait"), | 86 | _ => panic!("projection ty without parent trait"), |
86 | } | 87 | } |
@@ -141,7 +142,7 @@ pub enum TyKind { | |||
141 | /// when we have tried to normalize a projection like `T::Item` but | 142 | /// when we have tried to normalize a projection like `T::Item` but |
142 | /// couldn't find a better representation. In that case, we generate | 143 | /// couldn't find a better representation. In that case, we generate |
143 | /// an **application type** like `(Iterator::Item)<T>`. | 144 | /// an **application type** like `(Iterator::Item)<T>`. |
144 | AssociatedType(TypeAliasId, Substs), | 145 | AssociatedType(AssocTypeId, Substs), |
145 | 146 | ||
146 | /// a scalar type like `bool` or `u32` | 147 | /// a scalar type like `bool` or `u32` |
147 | Scalar(Scalar), | 148 | Scalar(Scalar), |
@@ -706,7 +707,7 @@ impl Ty { | |||
706 | match *self.interned(&Interner) { | 707 | match *self.interned(&Interner) { |
707 | TyKind::Adt(AdtId(adt), ..) => Some(adt.into()), | 708 | TyKind::Adt(AdtId(adt), ..) => Some(adt.into()), |
708 | TyKind::FnDef(callable, ..) => Some(callable.into()), | 709 | TyKind::FnDef(callable, ..) => Some(callable.into()), |
709 | TyKind::AssociatedType(type_alias, ..) => Some(type_alias.into()), | 710 | TyKind::AssociatedType(type_alias, ..) => Some(from_assoc_type_id(type_alias).into()), |
710 | TyKind::ForeignType(type_alias, ..) => Some(from_foreign_def_id(type_alias).into()), | 711 | TyKind::ForeignType(type_alias, ..) => Some(from_foreign_def_id(type_alias).into()), |
711 | _ => None, | 712 | _ => None, |
712 | } | 713 | } |
@@ -920,14 +921,15 @@ impl Ty { | |||
920 | 921 | ||
921 | pub fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<TraitId> { | 922 | pub fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<TraitId> { |
922 | match self.interned(&Interner) { | 923 | match self.interned(&Interner) { |
923 | TyKind::AssociatedType(type_alias_id, ..) => { | 924 | TyKind::AssociatedType(id, ..) => { |
924 | match type_alias_id.lookup(db.upcast()).container { | 925 | match from_assoc_type_id(*id).lookup(db.upcast()).container { |
925 | AssocContainerId::TraitId(trait_id) => Some(trait_id), | 926 | AssocContainerId::TraitId(trait_id) => Some(trait_id), |
926 | _ => None, | 927 | _ => None, |
927 | } | 928 | } |
928 | } | 929 | } |
929 | TyKind::Alias(AliasTy::Projection(projection_ty)) => { | 930 | TyKind::Alias(AliasTy::Projection(projection_ty)) => { |
930 | match projection_ty.associated_ty.lookup(db.upcast()).container { | 931 | match from_assoc_type_id(projection_ty.associated_ty).lookup(db.upcast()).container |
932 | { | ||
931 | AssocContainerId::TraitId(trait_id) => Some(trait_id), | 933 | AssocContainerId::TraitId(trait_id) => Some(trait_id), |
932 | _ => None, | 934 | _ => None, |
933 | } | 935 | } |
@@ -1121,10 +1123,18 @@ pub(crate) struct ReturnTypeImplTrait { | |||
1121 | pub(crate) bounds: Binders<Vec<GenericPredicate>>, | 1123 | pub(crate) bounds: Binders<Vec<GenericPredicate>>, |
1122 | } | 1124 | } |
1123 | 1125 | ||
1124 | pub(crate) fn to_foreign_def_id(id: TypeAliasId) -> chalk_ir::ForeignDefId<Interner> { | 1126 | pub fn to_foreign_def_id(id: TypeAliasId) -> ForeignDefId { |
1125 | chalk_ir::ForeignDefId(salsa::InternKey::as_intern_id(&id)) | 1127 | chalk_ir::ForeignDefId(salsa::InternKey::as_intern_id(&id)) |
1126 | } | 1128 | } |
1127 | 1129 | ||
1128 | pub(crate) fn from_foreign_def_id(id: chalk_ir::ForeignDefId<Interner>) -> TypeAliasId { | 1130 | pub fn from_foreign_def_id(id: ForeignDefId) -> TypeAliasId { |
1131 | salsa::InternKey::from_intern_id(id.0) | ||
1132 | } | ||
1133 | |||
1134 | pub fn to_assoc_type_id(id: TypeAliasId) -> AssocTypeId { | ||
1135 | chalk_ir::AssocTypeId(salsa::InternKey::as_intern_id(&id)) | ||
1136 | } | ||
1137 | |||
1138 | pub fn from_assoc_type_id(id: AssocTypeId) -> TypeAliasId { | ||
1129 | salsa::InternKey::from_intern_id(id.0) | 1139 | salsa::InternKey::from_intern_id(id.0) |
1130 | } | 1140 | } |
diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index a35d7266d..a5ab1ff70 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs | |||
@@ -27,6 +27,7 @@ use stdx::impl_from; | |||
27 | 27 | ||
28 | use crate::{ | 28 | use crate::{ |
29 | db::HirDatabase, | 29 | db::HirDatabase, |
30 | to_assoc_type_id, | ||
30 | traits::chalk::{Interner, ToChalk}, | 31 | traits::chalk::{Interner, ToChalk}, |
31 | utils::{ | 32 | utils::{ |
32 | 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, |
@@ -358,7 +359,7 @@ impl Ty { | |||
358 | Some((super_trait_ref, associated_ty)) => { | 359 | Some((super_trait_ref, associated_ty)) => { |
359 | // FIXME handle type parameters on the segment | 360 | // FIXME handle type parameters on the segment |
360 | TyKind::Alias(AliasTy::Projection(ProjectionTy { | 361 | TyKind::Alias(AliasTy::Projection(ProjectionTy { |
361 | associated_ty, | 362 | associated_ty: to_assoc_type_id(associated_ty), |
362 | parameters: super_trait_ref.substs, | 363 | parameters: super_trait_ref.substs, |
363 | })) | 364 | })) |
364 | .intern(&Interner) | 365 | .intern(&Interner) |
@@ -487,7 +488,7 @@ impl Ty { | |||
487 | // FIXME handle type parameters on the segment | 488 | // FIXME handle type parameters on the segment |
488 | return Some( | 489 | return Some( |
489 | TyKind::Alias(AliasTy::Projection(ProjectionTy { | 490 | TyKind::Alias(AliasTy::Projection(ProjectionTy { |
490 | associated_ty, | 491 | associated_ty: to_assoc_type_id(associated_ty), |
491 | parameters: substs, | 492 | parameters: substs, |
492 | })) | 493 | })) |
493 | .intern(&Interner), | 494 | .intern(&Interner), |
@@ -753,7 +754,10 @@ fn assoc_type_bindings_from_type_bound<'a>( | |||
753 | None => return SmallVec::<[GenericPredicate; 1]>::new(), | 754 | None => return SmallVec::<[GenericPredicate; 1]>::new(), |
754 | Some(t) => t, | 755 | Some(t) => t, |
755 | }; | 756 | }; |
756 | let projection_ty = ProjectionTy { associated_ty, parameters: super_trait_ref.substs }; | 757 | let projection_ty = ProjectionTy { |
758 | associated_ty: to_assoc_type_id(associated_ty), | ||
759 | parameters: super_trait_ref.substs, | ||
760 | }; | ||
757 | let mut preds = SmallVec::with_capacity( | 761 | let mut preds = SmallVec::with_capacity( |
758 | binding.type_ref.as_ref().map_or(0, |_| 1) + binding.bounds.len(), | 762 | binding.type_ref.as_ref().map_or(0, |_| 1) + binding.bounds.len(), |
759 | ); | 763 | ); |
diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs index 55181cc49..2d3cd6719 100644 --- a/crates/hir_ty/src/traits/chalk.rs +++ b/crates/hir_ty/src/traits/chalk.rs | |||
@@ -17,14 +17,15 @@ use super::ChalkContext; | |||
17 | use crate::{ | 17 | use crate::{ |
18 | db::HirDatabase, | 18 | db::HirDatabase, |
19 | display::HirDisplay, | 19 | display::HirDisplay, |
20 | from_assoc_type_id, | ||
20 | method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS}, | 21 | method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS}, |
22 | to_assoc_type_id, | ||
21 | utils::generics, | 23 | utils::generics, |
22 | BoundVar, CallableDefId, CallableSig, DebruijnIndex, GenericPredicate, ProjectionPredicate, | 24 | BoundVar, CallableDefId, CallableSig, DebruijnIndex, GenericPredicate, ProjectionPredicate, |
23 | ProjectionTy, Substs, TraitRef, Ty, TyKind, | 25 | ProjectionTy, Substs, TraitRef, Ty, TyKind, |
24 | }; | 26 | }; |
25 | use mapping::{ | 27 | use mapping::{ |
26 | convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsAssocType, | 28 | convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue, |
27 | TypeAliasAsValue, | ||
28 | }; | 29 | }; |
29 | 30 | ||
30 | pub use self::interner::Interner; | 31 | pub use self::interner::Interner; |
@@ -234,7 +235,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
234 | ty: TyKind::BoundVar(BoundVar { debruijn: DebruijnIndex::ONE, index: 0 }) | 235 | ty: TyKind::BoundVar(BoundVar { debruijn: DebruijnIndex::ONE, index: 0 }) |
235 | .intern(&Interner), | 236 | .intern(&Interner), |
236 | projection_ty: ProjectionTy { | 237 | projection_ty: ProjectionTy { |
237 | associated_ty: future_output, | 238 | associated_ty: to_assoc_type_id(future_output), |
238 | // Self type as the first parameter. | 239 | // Self type as the first parameter. |
239 | parameters: Substs::single( | 240 | parameters: Substs::single( |
240 | TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)) | 241 | TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)) |
@@ -383,7 +384,7 @@ pub(crate) fn associated_ty_data_query( | |||
383 | id: AssocTypeId, | 384 | id: AssocTypeId, |
384 | ) -> Arc<AssociatedTyDatum> { | 385 | ) -> Arc<AssociatedTyDatum> { |
385 | debug!("associated_ty_data {:?}", id); | 386 | debug!("associated_ty_data {:?}", id); |
386 | let type_alias: TypeAliasId = from_chalk::<TypeAliasAsAssocType, _>(db, id).0; | 387 | let type_alias: TypeAliasId = from_assoc_type_id(id); |
387 | let trait_ = match type_alias.lookup(db.upcast()).container { | 388 | let trait_ = match type_alias.lookup(db.upcast()).container { |
388 | AssocContainerId::TraitId(t) => t, | 389 | AssocContainerId::TraitId(t) => t, |
389 | _ => panic!("associated type not in trait"), | 390 | _ => panic!("associated type not in trait"), |
@@ -438,10 +439,8 @@ pub(crate) fn trait_datum_query( | |||
438 | fundamental: false, | 439 | fundamental: false, |
439 | }; | 440 | }; |
440 | let where_clauses = convert_where_clauses(db, trait_.into(), &bound_vars); | 441 | let where_clauses = convert_where_clauses(db, trait_.into(), &bound_vars); |
441 | let associated_ty_ids = trait_data | 442 | let associated_ty_ids = |
442 | .associated_types() | 443 | trait_data.associated_types().map(|type_alias| to_assoc_type_id(type_alias)).collect(); |
443 | .map(|type_alias| TypeAliasAsAssocType(type_alias).to_chalk(db)) | ||
444 | .collect(); | ||
445 | let trait_datum_bound = rust_ir::TraitDatumBound { where_clauses }; | 444 | let trait_datum_bound = rust_ir::TraitDatumBound { where_clauses }; |
446 | let well_known = | 445 | let well_known = |
447 | lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name)); | 446 | lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name)); |
@@ -623,7 +622,7 @@ fn type_alias_associated_ty_value( | |||
623 | let value_bound = rust_ir::AssociatedTyValueBound { ty: ty.value.to_chalk(db) }; | 622 | let value_bound = rust_ir::AssociatedTyValueBound { ty: ty.value.to_chalk(db) }; |
624 | let value = rust_ir::AssociatedTyValue { | 623 | let value = rust_ir::AssociatedTyValue { |
625 | impl_id: impl_id.to_chalk(db), | 624 | impl_id: impl_id.to_chalk(db), |
626 | associated_ty_id: TypeAliasAsAssocType(assoc_ty).to_chalk(db), | 625 | associated_ty_id: to_assoc_type_id(assoc_ty), |
627 | value: make_binders(value_bound, ty.num_binders), | 626 | value: make_binders(value_bound, ty.num_binders), |
628 | }; | 627 | }; |
629 | Arc::new(value) | 628 | Arc::new(value) |
diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index cf490f9c5..67ac95a0e 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 | ||
15 | use crate::{ | 15 | use 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 | } |
@@ -85,7 +84,7 @@ impl ToChalk for Ty { | |||
85 | chalk_ir::TyKind::Adt(adt_id, substitution).intern(&Interner) | 84 | chalk_ir::TyKind::Adt(adt_id, substitution).intern(&Interner) |
86 | } | 85 | } |
87 | TyKind::Alias(AliasTy::Projection(proj_ty)) => { | 86 | TyKind::Alias(AliasTy::Projection(proj_ty)) => { |
88 | let associated_ty_id = TypeAliasAsAssocType(proj_ty.associated_ty).to_chalk(db); | 87 | let associated_ty_id = proj_ty.associated_ty; |
89 | let substitution = proj_ty.parameters.to_chalk(db); | 88 | let substitution = proj_ty.parameters.to_chalk(db); |
90 | chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy { | 89 | chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy { |
91 | associated_ty_id, | 90 | associated_ty_id, |
@@ -139,8 +138,7 @@ impl ToChalk for Ty { | |||
139 | TyKind::Placeholder(db.lookup_intern_type_param_id(interned_id)) | 138 | TyKind::Placeholder(db.lookup_intern_type_param_id(interned_id)) |
140 | } | 139 | } |
141 | chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => { | 140 | chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => { |
142 | let associated_ty = | 141 | let associated_ty = proj.associated_ty_id; |
143 | from_chalk::<TypeAliasAsAssocType, _>(db, proj.associated_ty_id).0; | ||
144 | let parameters = from_chalk(db, proj.substitution); | 142 | let parameters = from_chalk(db, proj.substitution); |
145 | TyKind::Alias(AliasTy::Projection(ProjectionTy { associated_ty, parameters })) | 143 | TyKind::Alias(AliasTy::Projection(ProjectionTy { associated_ty, parameters })) |
146 | } | 144 | } |
@@ -180,10 +178,9 @@ impl ToChalk for Ty { | |||
180 | } | 178 | } |
181 | 179 | ||
182 | chalk_ir::TyKind::Adt(adt_id, subst) => TyKind::Adt(adt_id, from_chalk(db, subst)), | 180 | chalk_ir::TyKind::Adt(adt_id, subst) => TyKind::Adt(adt_id, from_chalk(db, subst)), |
183 | chalk_ir::TyKind::AssociatedType(type_id, subst) => TyKind::AssociatedType( | 181 | chalk_ir::TyKind::AssociatedType(type_id, subst) => { |
184 | from_chalk::<TypeAliasAsAssocType, _>(db, type_id).0, | 182 | TyKind::AssociatedType(type_id, from_chalk(db, subst)) |
185 | from_chalk(db, subst), | 183 | } |
186 | ), | ||
187 | 184 | ||
188 | chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => { | 185 | chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => { |
189 | TyKind::OpaqueType(from_chalk(db, opaque_type_id), from_chalk(db, subst)) | 186 | TyKind::OpaqueType(from_chalk(db, opaque_type_id), from_chalk(db, subst)) |
@@ -332,20 +329,6 @@ impl ToChalk for CallableDefId { | |||
332 | } | 329 | } |
333 | } | 330 | } |
334 | 331 | ||
335 | pub(crate) struct TypeAliasAsAssocType(pub(crate) TypeAliasId); | ||
336 | |||
337 | impl ToChalk for TypeAliasAsAssocType { | ||
338 | type Chalk = AssocTypeId; | ||
339 | |||
340 | fn to_chalk(self, _db: &dyn HirDatabase) -> AssocTypeId { | ||
341 | chalk_ir::AssocTypeId(self.0.as_intern_id()) | ||
342 | } | ||
343 | |||
344 | fn from_chalk(_db: &dyn HirDatabase, assoc_type_id: AssocTypeId) -> TypeAliasAsAssocType { | ||
345 | TypeAliasAsAssocType(InternKey::from_intern_id(assoc_type_id.0)) | ||
346 | } | ||
347 | } | ||
348 | |||
349 | pub(crate) struct TypeAliasAsValue(pub(crate) TypeAliasId); | 332 | pub(crate) struct TypeAliasAsValue(pub(crate) TypeAliasId); |
350 | 333 | ||
351 | impl ToChalk for TypeAliasAsValue { | 334 | impl ToChalk for TypeAliasAsValue { |
@@ -427,7 +410,7 @@ impl ToChalk for ProjectionTy { | |||
427 | 410 | ||
428 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::ProjectionTy<Interner> { | 411 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::ProjectionTy<Interner> { |
429 | chalk_ir::ProjectionTy { | 412 | chalk_ir::ProjectionTy { |
430 | associated_ty_id: TypeAliasAsAssocType(self.associated_ty).to_chalk(db), | 413 | associated_ty_id: self.associated_ty, |
431 | substitution: self.parameters.to_chalk(db), | 414 | substitution: self.parameters.to_chalk(db), |
432 | } | 415 | } |
433 | } | 416 | } |
@@ -437,11 +420,7 @@ impl ToChalk for ProjectionTy { | |||
437 | projection_ty: chalk_ir::ProjectionTy<Interner>, | 420 | projection_ty: chalk_ir::ProjectionTy<Interner>, |
438 | ) -> ProjectionTy { | 421 | ) -> ProjectionTy { |
439 | ProjectionTy { | 422 | ProjectionTy { |
440 | associated_ty: from_chalk::<TypeAliasAsAssocType, _>( | 423 | associated_ty: projection_ty.associated_ty_id, |
441 | db, | ||
442 | projection_ty.associated_ty_id, | ||
443 | ) | ||
444 | .0, | ||
445 | parameters: from_chalk(db, projection_ty.substitution), | 424 | parameters: from_chalk(db, projection_ty.substitution), |
446 | } | 425 | } |
447 | } | 426 | } |
@@ -595,7 +574,10 @@ pub(super) fn generic_predicate_to_inline_bound( | |||
595 | if &proj.projection_ty.parameters[0] != self_ty { | 574 | if &proj.projection_ty.parameters[0] != self_ty { |
596 | return None; | 575 | return None; |
597 | } | 576 | } |
598 | let trait_ = match proj.projection_ty.associated_ty.lookup(db.upcast()).container { | 577 | let trait_ = match from_assoc_type_id(proj.projection_ty.associated_ty) |
578 | .lookup(db.upcast()) | ||
579 | .container | ||
580 | { | ||
599 | AssocContainerId::TraitId(t) => t, | 581 | AssocContainerId::TraitId(t) => t, |
600 | _ => panic!("associated type not in trait"), | 582 | _ => panic!("associated type not in trait"), |
601 | }; | 583 | }; |
@@ -606,8 +588,7 @@ pub(super) fn generic_predicate_to_inline_bound( | |||
606 | let alias_eq_bound = rust_ir::AliasEqBound { | 588 | let alias_eq_bound = rust_ir::AliasEqBound { |
607 | value: proj.ty.clone().to_chalk(db), | 589 | value: proj.ty.clone().to_chalk(db), |
608 | trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self }, | 590 | trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self }, |
609 | associated_ty_id: TypeAliasAsAssocType(proj.projection_ty.associated_ty) | 591 | associated_ty_id: proj.projection_ty.associated_ty, |
610 | .to_chalk(db), | ||
611 | parameters: Vec::new(), // FIXME we don't support generic associated types yet | 592 | parameters: Vec::new(), // FIXME we don't support generic associated types yet |
612 | }; | 593 | }; |
613 | Some(rust_ir::InlineBound::AliasEqBound(alias_eq_bound)) | 594 | Some(rust_ir::InlineBound::AliasEqBound(alias_eq_bound)) |
diff --git a/crates/hir_ty/src/traits/chalk/tls.rs b/crates/hir_ty/src/traits/chalk/tls.rs index 75b16172e..8892a63a9 100644 --- a/crates/hir_ty/src/traits/chalk/tls.rs +++ b/crates/hir_ty/src/traits/chalk/tls.rs | |||
@@ -4,8 +4,8 @@ use std::fmt; | |||
4 | use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplication}; | 4 | use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplication}; |
5 | use itertools::Itertools; | 5 | use itertools::Itertools; |
6 | 6 | ||
7 | use super::{from_chalk, Interner, TypeAliasAsAssocType}; | 7 | use super::{from_chalk, Interner}; |
8 | use crate::{db::HirDatabase, CallableDefId}; | 8 | use crate::{db::HirDatabase, from_assoc_type_id, CallableDefId}; |
9 | use hir_def::{AdtId, AssocContainerId, Lookup, TypeAliasId}; | 9 | use hir_def::{AdtId, AssocContainerId, Lookup, TypeAliasId}; |
10 | 10 | ||
11 | pub(crate) use unsafe_tls::{set_current_program, with_current_program}; | 11 | pub(crate) use unsafe_tls::{set_current_program, with_current_program}; |
@@ -41,7 +41,7 @@ impl DebugContext<'_> { | |||
41 | id: super::AssocTypeId, | 41 | id: super::AssocTypeId, |
42 | fmt: &mut fmt::Formatter<'_>, | 42 | fmt: &mut fmt::Formatter<'_>, |
43 | ) -> Result<(), fmt::Error> { | 43 | ) -> Result<(), fmt::Error> { |
44 | let type_alias: TypeAliasId = from_chalk::<TypeAliasAsAssocType, _>(self.0, id).0; | 44 | let type_alias: TypeAliasId = from_assoc_type_id(id); |
45 | let type_alias_data = self.0.type_alias_data(type_alias); | 45 | let type_alias_data = self.0.type_alias_data(type_alias); |
46 | let trait_ = match type_alias.lookup(self.0.upcast()).container { | 46 | let trait_ = match type_alias.lookup(self.0.upcast()).container { |
47 | AssocContainerId::TraitId(t) => t, | 47 | AssocContainerId::TraitId(t) => t, |
@@ -75,8 +75,7 @@ impl DebugContext<'_> { | |||
75 | projection_ty: &chalk_ir::ProjectionTy<Interner>, | 75 | projection_ty: &chalk_ir::ProjectionTy<Interner>, |
76 | fmt: &mut fmt::Formatter<'_>, | 76 | fmt: &mut fmt::Formatter<'_>, |
77 | ) -> Result<(), fmt::Error> { | 77 | ) -> Result<(), fmt::Error> { |
78 | let type_alias: TypeAliasId = | 78 | let type_alias = from_assoc_type_id(projection_ty.associated_ty_id); |
79 | from_chalk::<TypeAliasAsAssocType, _>(self.0, projection_ty.associated_ty_id).0; | ||
80 | let type_alias_data = self.0.type_alias_data(type_alias); | 79 | let type_alias_data = self.0.type_alias_data(type_alias); |
81 | let trait_ = match type_alias.lookup(self.0.upcast()).container { | 80 | let trait_ = match type_alias.lookup(self.0.upcast()).container { |
82 | AssocContainerId::TraitId(t) => t, | 81 | AssocContainerId::TraitId(t) => t, |