diff options
Diffstat (limited to 'crates/hir_ty/src/traits/chalk')
-rw-r--r-- | crates/hir_ty/src/traits/chalk/interner.rs | 2 | ||||
-rw-r--r-- | crates/hir_ty/src/traits/chalk/mapping.rs | 287 | ||||
-rw-r--r-- | crates/hir_ty/src/traits/chalk/tls.rs | 9 |
3 files changed, 92 insertions, 206 deletions
diff --git a/crates/hir_ty/src/traits/chalk/interner.rs b/crates/hir_ty/src/traits/chalk/interner.rs index 54bd1c724..1dc3f497d 100644 --- a/crates/hir_ty/src/traits/chalk/interner.rs +++ b/crates/hir_ty/src/traits/chalk/interner.rs | |||
@@ -12,7 +12,6 @@ pub struct Interner; | |||
12 | 12 | ||
13 | pub(crate) type AssocTypeId = chalk_ir::AssocTypeId<Interner>; | 13 | pub(crate) type AssocTypeId = chalk_ir::AssocTypeId<Interner>; |
14 | pub(crate) type AssociatedTyDatum = chalk_solve::rust_ir::AssociatedTyDatum<Interner>; | 14 | pub(crate) type AssociatedTyDatum = chalk_solve::rust_ir::AssociatedTyDatum<Interner>; |
15 | pub(crate) type ForeignDefId = chalk_ir::ForeignDefId<Interner>; | ||
16 | pub(crate) type TraitId = chalk_ir::TraitId<Interner>; | 15 | pub(crate) type TraitId = chalk_ir::TraitId<Interner>; |
17 | pub(crate) type TraitDatum = chalk_solve::rust_ir::TraitDatum<Interner>; | 16 | pub(crate) type TraitDatum = chalk_solve::rust_ir::TraitDatum<Interner>; |
18 | pub(crate) type AdtId = chalk_ir::AdtId<Interner>; | 17 | pub(crate) type AdtId = chalk_ir::AdtId<Interner>; |
@@ -21,7 +20,6 @@ pub(crate) type ImplId = chalk_ir::ImplId<Interner>; | |||
21 | pub(crate) type ImplDatum = chalk_solve::rust_ir::ImplDatum<Interner>; | 20 | pub(crate) type ImplDatum = chalk_solve::rust_ir::ImplDatum<Interner>; |
22 | pub(crate) type AssociatedTyValueId = chalk_solve::rust_ir::AssociatedTyValueId<Interner>; | 21 | pub(crate) type AssociatedTyValueId = chalk_solve::rust_ir::AssociatedTyValueId<Interner>; |
23 | pub(crate) type AssociatedTyValue = chalk_solve::rust_ir::AssociatedTyValue<Interner>; | 22 | pub(crate) type AssociatedTyValue = chalk_solve::rust_ir::AssociatedTyValue<Interner>; |
24 | pub(crate) type FnDefId = chalk_ir::FnDefId<Interner>; | ||
25 | pub(crate) type FnDefDatum = chalk_solve::rust_ir::FnDefDatum<Interner>; | 23 | pub(crate) type FnDefDatum = chalk_solve::rust_ir::FnDefDatum<Interner>; |
26 | pub(crate) type OpaqueTyId = chalk_ir::OpaqueTyId<Interner>; | 24 | pub(crate) type OpaqueTyId = chalk_ir::OpaqueTyId<Interner>; |
27 | pub(crate) type OpaqueTyDatum = chalk_solve::rust_ir::OpaqueTyDatum<Interner>; | 25 | pub(crate) type OpaqueTyDatum = chalk_solve::rust_ir::OpaqueTyDatum<Interner>; |
diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index 3a08b67e9..6a8b6752e 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 | ||
6 | use chalk_ir::{ | 6 | use chalk_ir::{cast::Cast, fold::shift::Shift, interner::HasInterner, LifetimeData}; |
7 | cast::Cast, fold::shift::Shift, interner::HasInterner, LifetimeData, PlaceholderIndex, | ||
8 | UniverseIndex, | ||
9 | }; | ||
10 | use chalk_solve::rust_ir; | 7 | use chalk_solve::rust_ir; |
11 | 8 | ||
12 | use base_db::salsa::InternKey; | 9 | use base_db::salsa::InternKey; |
@@ -14,10 +11,11 @@ use hir_def::{AssocContainerId, GenericDefId, Lookup, TypeAliasId}; | |||
14 | 11 | ||
15 | use crate::{ | 12 | use crate::{ |
16 | db::HirDatabase, | 13 | db::HirDatabase, |
14 | from_assoc_type_id, | ||
17 | primitive::UintTy, | 15 | primitive::UintTy, |
18 | traits::{Canonical, Obligation}, | 16 | traits::{Canonical, Obligation}, |
19 | AliasTy, CallableDefId, FnPointer, FnSig, GenericPredicate, InEnvironment, OpaqueTy, | 17 | AliasTy, CallableDefId, FnPointer, GenericPredicate, InEnvironment, OpaqueTy, |
20 | OpaqueTyId, ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment, TraitRef, Ty, | 18 | ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitRef, Ty, |
21 | }; | 19 | }; |
22 | 20 | ||
23 | use super::interner::*; | 21 | use super::interner::*; |
@@ -26,71 +24,60 @@ use super::*; | |||
26 | impl ToChalk for Ty { | 24 | impl ToChalk for Ty { |
27 | type Chalk = chalk_ir::Ty<Interner>; | 25 | type Chalk = chalk_ir::Ty<Interner>; |
28 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> { | 26 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> { |
29 | match self { | 27 | match self.into_inner() { |
30 | Ty::Ref(m, parameters) => ref_to_chalk(db, m, parameters), | 28 | TyKind::Ref(m, ty) => ref_to_chalk(db, m, ty), |
31 | Ty::Array(parameters) => array_to_chalk(db, parameters), | 29 | TyKind::Array(ty) => array_to_chalk(db, ty), |
32 | Ty::Function(FnPointer { sig: FnSig { variadic }, substs, .. }) => { | 30 | TyKind::Function(FnPointer { sig, substs, .. }) => { |
33 | let substitution = chalk_ir::FnSubst(substs.to_chalk(db).shifted_in(&Interner)); | 31 | let substitution = chalk_ir::FnSubst(substs.to_chalk(db).shifted_in(&Interner)); |
34 | chalk_ir::TyKind::Function(chalk_ir::FnPointer { | 32 | chalk_ir::TyKind::Function(chalk_ir::FnPointer { |
35 | num_binders: 0, | 33 | num_binders: 0, |
36 | sig: chalk_ir::FnSig { abi: (), safety: chalk_ir::Safety::Safe, variadic }, | 34 | sig, |
37 | substitution, | 35 | substitution, |
38 | }) | 36 | }) |
39 | .intern(&Interner) | 37 | .intern(&Interner) |
40 | } | 38 | } |
41 | Ty::AssociatedType(type_alias, substs) => { | 39 | 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); | 40 | let substitution = substs.to_chalk(db); |
45 | chalk_ir::TyKind::AssociatedType(assoc_type_id, substitution).intern(&Interner) | 41 | chalk_ir::TyKind::AssociatedType(assoc_type_id, substitution).intern(&Interner) |
46 | } | 42 | } |
47 | 43 | ||
48 | Ty::OpaqueType(impl_trait_id, substs) => { | 44 | TyKind::OpaqueType(id, substs) => { |
49 | let id = impl_trait_id.to_chalk(db); | ||
50 | let substitution = substs.to_chalk(db); | 45 | let substitution = substs.to_chalk(db); |
51 | chalk_ir::TyKind::OpaqueType(id, substitution).intern(&Interner) | 46 | chalk_ir::TyKind::OpaqueType(id, substitution).intern(&Interner) |
52 | } | 47 | } |
53 | 48 | ||
54 | Ty::ForeignType(type_alias) => { | 49 | 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 | 50 | ||
60 | Ty::Scalar(scalar) => chalk_ir::TyKind::Scalar(scalar).intern(&Interner), | 51 | TyKind::Scalar(scalar) => chalk_ir::TyKind::Scalar(scalar).intern(&Interner), |
61 | 52 | ||
62 | Ty::Tuple(cardinality, substs) => { | 53 | TyKind::Tuple(cardinality, substs) => { |
63 | let substitution = substs.to_chalk(db); | 54 | let substitution = substs.to_chalk(db); |
64 | chalk_ir::TyKind::Tuple(cardinality.into(), substitution).intern(&Interner) | 55 | chalk_ir::TyKind::Tuple(cardinality.into(), substitution).intern(&Interner) |
65 | } | 56 | } |
66 | Ty::Raw(mutability, substs) => { | 57 | TyKind::Raw(mutability, ty) => { |
67 | let ty = substs[0].clone().to_chalk(db); | 58 | let ty = ty.to_chalk(db); |
68 | chalk_ir::TyKind::Raw(mutability, ty).intern(&Interner) | 59 | chalk_ir::TyKind::Raw(mutability, ty).intern(&Interner) |
69 | } | 60 | } |
70 | Ty::Slice(substs) => { | 61 | TyKind::Slice(ty) => chalk_ir::TyKind::Slice(ty.to_chalk(db)).intern(&Interner), |
71 | chalk_ir::TyKind::Slice(substs[0].clone().to_chalk(db)).intern(&Interner) | 62 | TyKind::Str => chalk_ir::TyKind::Str.intern(&Interner), |
72 | } | 63 | TyKind::FnDef(id, substs) => { |
73 | Ty::Str => chalk_ir::TyKind::Str.intern(&Interner), | ||
74 | Ty::FnDef(callable_def, substs) => { | ||
75 | let id = callable_def.to_chalk(db); | ||
76 | let substitution = substs.to_chalk(db); | 64 | let substitution = substs.to_chalk(db); |
77 | chalk_ir::TyKind::FnDef(id, substitution).intern(&Interner) | 65 | chalk_ir::TyKind::FnDef(id, substitution).intern(&Interner) |
78 | } | 66 | } |
79 | Ty::Never => chalk_ir::TyKind::Never.intern(&Interner), | 67 | TyKind::Never => chalk_ir::TyKind::Never.intern(&Interner), |
80 | 68 | ||
81 | Ty::Closure(def, expr, substs) => { | 69 | TyKind::Closure(closure_id, substs) => { |
82 | let closure_id = db.intern_closure((def, expr)); | ||
83 | let substitution = substs.to_chalk(db); | 70 | let substitution = substs.to_chalk(db); |
84 | chalk_ir::TyKind::Closure(closure_id.into(), substitution).intern(&Interner) | 71 | chalk_ir::TyKind::Closure(closure_id, substitution).intern(&Interner) |
85 | } | 72 | } |
86 | 73 | ||
87 | Ty::Adt(adt_id, substs) => { | 74 | TyKind::Adt(adt_id, substs) => { |
88 | let substitution = substs.to_chalk(db); | 75 | let substitution = substs.to_chalk(db); |
89 | chalk_ir::TyKind::Adt(adt_id, substitution).intern(&Interner) | 76 | chalk_ir::TyKind::Adt(adt_id, substitution).intern(&Interner) |
90 | } | 77 | } |
91 | Ty::Alias(AliasTy::Projection(proj_ty)) => { | 78 | TyKind::Alias(AliasTy::Projection(proj_ty)) => { |
92 | let associated_ty_id = TypeAliasAsAssocType(proj_ty.associated_ty).to_chalk(db); | 79 | let associated_ty_id = proj_ty.associated_ty_id; |
93 | let substitution = proj_ty.parameters.to_chalk(db); | 80 | let substitution = proj_ty.substitution.to_chalk(db); |
94 | chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy { | 81 | chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy { |
95 | associated_ty_id, | 82 | associated_ty_id, |
96 | substitution, | 83 | substitution, |
@@ -98,17 +85,17 @@ impl ToChalk for Ty { | |||
98 | .cast(&Interner) | 85 | .cast(&Interner) |
99 | .intern(&Interner) | 86 | .intern(&Interner) |
100 | } | 87 | } |
101 | Ty::Placeholder(id) => { | 88 | TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { |
102 | let interned_id = db.intern_type_param_id(id); | 89 | let opaque_ty_id = opaque_ty.opaque_ty_id; |
103 | PlaceholderIndex { | 90 | let substitution = opaque_ty.substitution.to_chalk(db); |
104 | ui: UniverseIndex::ROOT, | 91 | chalk_ir::AliasTy::Opaque(chalk_ir::OpaqueTy { opaque_ty_id, substitution }) |
105 | idx: interned_id.as_intern_id().as_usize(), | 92 | .cast(&Interner) |
106 | } | 93 | .intern(&Interner) |
107 | .to_ty::<Interner>(&Interner) | 94 | } |
108 | } | 95 | TyKind::Placeholder(idx) => idx.to_ty::<Interner>(&Interner), |
109 | Ty::BoundVar(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner), | 96 | TyKind::BoundVar(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner), |
110 | Ty::InferenceVar(..) => panic!("uncanonicalized infer ty"), | 97 | TyKind::InferenceVar(..) => panic!("uncanonicalized infer ty"), |
111 | Ty::Dyn(predicates) => { | 98 | TyKind::Dyn(predicates) => { |
112 | let where_clauses = chalk_ir::QuantifiedWhereClauses::from_iter( | 99 | let where_clauses = chalk_ir::QuantifiedWhereClauses::from_iter( |
113 | &Interner, | 100 | &Interner, |
114 | predicates.iter().filter(|p| !p.is_error()).cloned().map(|p| p.to_chalk(db)), | 101 | predicates.iter().filter(|p| !p.is_error()).cloned().map(|p| p.to_chalk(db)), |
@@ -119,43 +106,30 @@ impl ToChalk for Ty { | |||
119 | }; | 106 | }; |
120 | chalk_ir::TyKind::Dyn(bounded_ty).intern(&Interner) | 107 | chalk_ir::TyKind::Dyn(bounded_ty).intern(&Interner) |
121 | } | 108 | } |
122 | Ty::Alias(AliasTy::Opaque(opaque_ty)) => { | 109 | TyKind::Unknown => chalk_ir::TyKind::Error.intern(&Interner), |
123 | let opaque_ty_id = opaque_ty.opaque_ty_id.to_chalk(db); | ||
124 | let substitution = opaque_ty.parameters.to_chalk(db); | ||
125 | chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(chalk_ir::OpaqueTy { | ||
126 | opaque_ty_id, | ||
127 | substitution, | ||
128 | })) | ||
129 | .intern(&Interner) | ||
130 | } | ||
131 | Ty::Unknown => chalk_ir::TyKind::Error.intern(&Interner), | ||
132 | } | 110 | } |
133 | } | 111 | } |
134 | fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self { | 112 | fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self { |
135 | match chalk.data(&Interner).kind.clone() { | 113 | match chalk.data(&Interner).kind.clone() { |
136 | chalk_ir::TyKind::Error => Ty::Unknown, | 114 | chalk_ir::TyKind::Error => TyKind::Unknown, |
137 | chalk_ir::TyKind::Array(ty, _size) => Ty::Array(Substs::single(from_chalk(db, ty))), | 115 | chalk_ir::TyKind::Array(ty, _size) => TyKind::Array(from_chalk(db, ty)), |
138 | chalk_ir::TyKind::Placeholder(idx) => { | 116 | chalk_ir::TyKind::Placeholder(idx) => TyKind::Placeholder(idx), |
139 | assert_eq!(idx.ui, UniverseIndex::ROOT); | ||
140 | let interned_id = crate::db::GlobalTypeParamId::from_intern_id( | ||
141 | crate::salsa::InternId::from(idx.idx), | ||
142 | ); | ||
143 | Ty::Placeholder(db.lookup_intern_type_param_id(interned_id)) | ||
144 | } | ||
145 | chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => { | 117 | chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => { |
146 | let associated_ty = | 118 | 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); | 119 | let parameters = from_chalk(db, proj.substitution); |
149 | Ty::Alias(AliasTy::Projection(ProjectionTy { associated_ty, parameters })) | 120 | TyKind::Alias(AliasTy::Projection(ProjectionTy { |
121 | associated_ty_id: associated_ty, | ||
122 | substitution: parameters, | ||
123 | })) | ||
150 | } | 124 | } |
151 | chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(opaque_ty)) => { | 125 | chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(opaque_ty)) => { |
152 | let impl_trait_id = from_chalk(db, opaque_ty.opaque_ty_id); | 126 | let opaque_ty_id = opaque_ty.opaque_ty_id; |
153 | let parameters = from_chalk(db, opaque_ty.substitution); | 127 | let parameters = from_chalk(db, opaque_ty.substitution); |
154 | Ty::Alias(AliasTy::Opaque(OpaqueTy { opaque_ty_id: impl_trait_id, parameters })) | 128 | TyKind::Alias(AliasTy::Opaque(OpaqueTy { opaque_ty_id, substitution: parameters })) |
155 | } | 129 | } |
156 | chalk_ir::TyKind::Function(chalk_ir::FnPointer { | 130 | chalk_ir::TyKind::Function(chalk_ir::FnPointer { |
157 | num_binders, | 131 | num_binders, |
158 | sig: chalk_ir::FnSig { variadic, .. }, | 132 | sig, |
159 | substitution, | 133 | substitution, |
160 | .. | 134 | .. |
161 | }) => { | 135 | }) => { |
@@ -164,14 +138,10 @@ impl ToChalk for Ty { | |||
164 | db, | 138 | db, |
165 | substitution.0.shifted_out(&Interner).expect("fn ptr should have no binders"), | 139 | substitution.0.shifted_out(&Interner).expect("fn ptr should have no binders"), |
166 | ); | 140 | ); |
167 | Ty::Function(FnPointer { | 141 | TyKind::Function(FnPointer { num_args: (substs.len() - 1), sig, substs }) |
168 | num_args: (substs.len() - 1), | ||
169 | sig: FnSig { variadic }, | ||
170 | substs, | ||
171 | }) | ||
172 | } | 142 | } |
173 | chalk_ir::TyKind::BoundVar(idx) => Ty::BoundVar(idx), | 143 | chalk_ir::TyKind::BoundVar(idx) => TyKind::BoundVar(idx), |
174 | chalk_ir::TyKind::InferenceVar(_iv, _kind) => Ty::Unknown, | 144 | chalk_ir::TyKind::InferenceVar(_iv, _kind) => TyKind::Unknown, |
175 | chalk_ir::TyKind::Dyn(where_clauses) => { | 145 | chalk_ir::TyKind::Dyn(where_clauses) => { |
176 | assert_eq!(where_clauses.bounds.binders.len(&Interner), 1); | 146 | assert_eq!(where_clauses.bounds.binders.len(&Interner), 1); |
177 | let predicates = where_clauses | 147 | let predicates = where_clauses |
@@ -180,49 +150,41 @@ impl ToChalk for Ty { | |||
180 | .iter(&Interner) | 150 | .iter(&Interner) |
181 | .map(|c| from_chalk(db, c.clone())) | 151 | .map(|c| from_chalk(db, c.clone())) |
182 | .collect(); | 152 | .collect(); |
183 | Ty::Dyn(predicates) | 153 | TyKind::Dyn(predicates) |
184 | } | 154 | } |
185 | 155 | ||
186 | chalk_ir::TyKind::Adt(adt_id, subst) => Ty::Adt(adt_id, from_chalk(db, subst)), | 156 | chalk_ir::TyKind::Adt(adt_id, subst) => TyKind::Adt(adt_id, from_chalk(db, subst)), |
187 | chalk_ir::TyKind::AssociatedType(type_id, subst) => Ty::AssociatedType( | 157 | chalk_ir::TyKind::AssociatedType(type_id, subst) => { |
188 | from_chalk::<TypeAliasAsAssocType, _>(db, type_id).0, | 158 | TyKind::AssociatedType(type_id, from_chalk(db, subst)) |
189 | from_chalk(db, subst), | 159 | } |
190 | ), | ||
191 | 160 | ||
192 | chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => { | 161 | chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => { |
193 | Ty::OpaqueType(from_chalk(db, opaque_type_id), from_chalk(db, subst)) | 162 | TyKind::OpaqueType(opaque_type_id, from_chalk(db, subst)) |
194 | } | 163 | } |
195 | 164 | ||
196 | chalk_ir::TyKind::Scalar(scalar) => Ty::Scalar(scalar), | 165 | chalk_ir::TyKind::Scalar(scalar) => TyKind::Scalar(scalar), |
197 | chalk_ir::TyKind::Tuple(cardinality, subst) => { | 166 | chalk_ir::TyKind::Tuple(cardinality, subst) => { |
198 | Ty::Tuple(cardinality, from_chalk(db, subst)) | 167 | TyKind::Tuple(cardinality, from_chalk(db, subst)) |
199 | } | 168 | } |
200 | chalk_ir::TyKind::Raw(mutability, ty) => { | 169 | chalk_ir::TyKind::Raw(mutability, ty) => TyKind::Raw(mutability, from_chalk(db, ty)), |
201 | Ty::Raw(mutability, Substs::single(from_chalk(db, ty))) | 170 | chalk_ir::TyKind::Slice(ty) => TyKind::Slice(from_chalk(db, ty)), |
202 | } | ||
203 | chalk_ir::TyKind::Slice(ty) => Ty::Slice(Substs::single(from_chalk(db, ty))), | ||
204 | chalk_ir::TyKind::Ref(mutability, _lifetime, ty) => { | 171 | chalk_ir::TyKind::Ref(mutability, _lifetime, ty) => { |
205 | Ty::Ref(mutability, Substs::single(from_chalk(db, ty))) | 172 | TyKind::Ref(mutability, from_chalk(db, ty)) |
206 | } | 173 | } |
207 | chalk_ir::TyKind::Str => Ty::Str, | 174 | chalk_ir::TyKind::Str => TyKind::Str, |
208 | chalk_ir::TyKind::Never => Ty::Never, | 175 | chalk_ir::TyKind::Never => TyKind::Never, |
209 | 176 | ||
210 | chalk_ir::TyKind::FnDef(fn_def_id, subst) => { | 177 | chalk_ir::TyKind::FnDef(fn_def_id, subst) => { |
211 | Ty::FnDef(from_chalk(db, fn_def_id), from_chalk(db, subst)) | 178 | TyKind::FnDef(fn_def_id, from_chalk(db, subst)) |
212 | } | 179 | } |
213 | 180 | ||
214 | chalk_ir::TyKind::Closure(id, subst) => { | 181 | chalk_ir::TyKind::Closure(id, subst) => TyKind::Closure(id, from_chalk(db, subst)), |
215 | let id: crate::db::ClosureId = id.into(); | ||
216 | let (def, expr) = db.lookup_intern_closure(id); | ||
217 | Ty::Closure(def, expr, from_chalk(db, subst)) | ||
218 | } | ||
219 | 182 | ||
220 | chalk_ir::TyKind::Foreign(foreign_def_id) => { | 183 | chalk_ir::TyKind::Foreign(foreign_def_id) => TyKind::ForeignType(foreign_def_id), |
221 | Ty::ForeignType(from_chalk::<TypeAliasAsForeignType, _>(db, foreign_def_id).0) | ||
222 | } | ||
223 | chalk_ir::TyKind::Generator(_, _) => unimplemented!(), // FIXME | 184 | chalk_ir::TyKind::Generator(_, _) => unimplemented!(), // FIXME |
224 | chalk_ir::TyKind::GeneratorWitness(_, _) => unimplemented!(), // FIXME | 185 | chalk_ir::TyKind::GeneratorWitness(_, _) => unimplemented!(), // FIXME |
225 | } | 186 | } |
187 | .intern(&Interner) | ||
226 | } | 188 | } |
227 | } | 189 | } |
228 | 190 | ||
@@ -231,17 +193,17 @@ impl ToChalk for Ty { | |||
231 | fn ref_to_chalk( | 193 | fn ref_to_chalk( |
232 | db: &dyn HirDatabase, | 194 | db: &dyn HirDatabase, |
233 | mutability: chalk_ir::Mutability, | 195 | mutability: chalk_ir::Mutability, |
234 | subst: Substs, | 196 | ty: Ty, |
235 | ) -> chalk_ir::Ty<Interner> { | 197 | ) -> chalk_ir::Ty<Interner> { |
236 | let arg = subst[0].clone().to_chalk(db); | 198 | let arg = ty.to_chalk(db); |
237 | let lifetime = LifetimeData::Static.intern(&Interner); | 199 | let lifetime = LifetimeData::Static.intern(&Interner); |
238 | chalk_ir::TyKind::Ref(mutability, lifetime, arg).intern(&Interner) | 200 | chalk_ir::TyKind::Ref(mutability, lifetime, arg).intern(&Interner) |
239 | } | 201 | } |
240 | 202 | ||
241 | /// We currently don't model constants, but Chalk does. So, we have to insert a | 203 | /// We currently don't model constants, but Chalk does. So, we have to insert a |
242 | /// fake constant here, because Chalks built-in logic may expect it to be there. | 204 | /// fake constant here, because Chalks built-in logic may expect it to be there. |
243 | fn array_to_chalk(db: &dyn HirDatabase, subst: Substs) -> chalk_ir::Ty<Interner> { | 205 | fn array_to_chalk(db: &dyn HirDatabase, ty: Ty) -> chalk_ir::Ty<Interner> { |
244 | let arg = subst[0].clone().to_chalk(db); | 206 | let arg = ty.to_chalk(db); |
245 | let usize_ty = chalk_ir::TyKind::Scalar(Scalar::Uint(UintTy::Usize)).intern(&Interner); | 207 | let usize_ty = chalk_ir::TyKind::Scalar(Scalar::Uint(UintTy::Usize)).intern(&Interner); |
246 | let const_ = chalk_ir::ConstData { | 208 | let const_ = chalk_ir::ConstData { |
247 | ty: usize_ty, | 209 | ty: usize_ty, |
@@ -298,21 +260,6 @@ impl ToChalk for hir_def::TraitId { | |||
298 | } | 260 | } |
299 | } | 261 | } |
300 | 262 | ||
301 | impl ToChalk for OpaqueTyId { | ||
302 | type Chalk = chalk_ir::OpaqueTyId<Interner>; | ||
303 | |||
304 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::OpaqueTyId<Interner> { | ||
305 | db.intern_impl_trait_id(self).into() | ||
306 | } | ||
307 | |||
308 | fn from_chalk( | ||
309 | db: &dyn HirDatabase, | ||
310 | opaque_ty_id: chalk_ir::OpaqueTyId<Interner>, | ||
311 | ) -> OpaqueTyId { | ||
312 | db.lookup_intern_impl_trait_id(opaque_ty_id.into()) | ||
313 | } | ||
314 | } | ||
315 | |||
316 | impl ToChalk for hir_def::ImplId { | 263 | impl ToChalk for hir_def::ImplId { |
317 | type Chalk = ImplId; | 264 | type Chalk = ImplId; |
318 | 265 | ||
@@ -337,34 +284,6 @@ impl ToChalk for CallableDefId { | |||
337 | } | 284 | } |
338 | } | 285 | } |
339 | 286 | ||
340 | pub(crate) struct TypeAliasAsAssocType(pub(crate) TypeAliasId); | ||
341 | |||
342 | impl ToChalk for TypeAliasAsAssocType { | ||
343 | type Chalk = AssocTypeId; | ||
344 | |||
345 | fn to_chalk(self, _db: &dyn HirDatabase) -> AssocTypeId { | ||
346 | chalk_ir::AssocTypeId(self.0.as_intern_id()) | ||
347 | } | ||
348 | |||
349 | fn from_chalk(_db: &dyn HirDatabase, assoc_type_id: AssocTypeId) -> TypeAliasAsAssocType { | ||
350 | TypeAliasAsAssocType(InternKey::from_intern_id(assoc_type_id.0)) | ||
351 | } | ||
352 | } | ||
353 | |||
354 | pub(crate) struct TypeAliasAsForeignType(pub(crate) TypeAliasId); | ||
355 | |||
356 | impl ToChalk for TypeAliasAsForeignType { | ||
357 | type Chalk = ForeignDefId; | ||
358 | |||
359 | fn to_chalk(self, _db: &dyn HirDatabase) -> ForeignDefId { | ||
360 | chalk_ir::ForeignDefId(self.0.as_intern_id()) | ||
361 | } | ||
362 | |||
363 | fn from_chalk(_db: &dyn HirDatabase, foreign_def_id: ForeignDefId) -> TypeAliasAsForeignType { | ||
364 | TypeAliasAsForeignType(InternKey::from_intern_id(foreign_def_id.0)) | ||
365 | } | ||
366 | } | ||
367 | |||
368 | pub(crate) struct TypeAliasAsValue(pub(crate) TypeAliasId); | 287 | pub(crate) struct TypeAliasAsValue(pub(crate) TypeAliasId); |
369 | 288 | ||
370 | impl ToChalk for TypeAliasAsValue { | 289 | impl ToChalk for TypeAliasAsValue { |
@@ -446,8 +365,8 @@ impl ToChalk for ProjectionTy { | |||
446 | 365 | ||
447 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::ProjectionTy<Interner> { | 366 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::ProjectionTy<Interner> { |
448 | chalk_ir::ProjectionTy { | 367 | chalk_ir::ProjectionTy { |
449 | associated_ty_id: TypeAliasAsAssocType(self.associated_ty).to_chalk(db), | 368 | associated_ty_id: self.associated_ty_id, |
450 | substitution: self.parameters.to_chalk(db), | 369 | substitution: self.substitution.to_chalk(db), |
451 | } | 370 | } |
452 | } | 371 | } |
453 | 372 | ||
@@ -456,12 +375,8 @@ impl ToChalk for ProjectionTy { | |||
456 | projection_ty: chalk_ir::ProjectionTy<Interner>, | 375 | projection_ty: chalk_ir::ProjectionTy<Interner>, |
457 | ) -> ProjectionTy { | 376 | ) -> ProjectionTy { |
458 | ProjectionTy { | 377 | ProjectionTy { |
459 | associated_ty: from_chalk::<TypeAliasAsAssocType, _>( | 378 | associated_ty_id: projection_ty.associated_ty_id, |
460 | db, | 379 | substitution: from_chalk(db, projection_ty.substitution), |
461 | projection_ty.associated_ty_id, | ||
462 | ) | ||
463 | .0, | ||
464 | parameters: from_chalk(db, projection_ty.substitution), | ||
465 | } | 380 | } |
466 | } | 381 | } |
467 | } | 382 | } |
@@ -536,31 +451,6 @@ where | |||
536 | } | 451 | } |
537 | } | 452 | } |
538 | 453 | ||
539 | impl ToChalk for Arc<TraitEnvironment> { | ||
540 | type Chalk = chalk_ir::Environment<Interner>; | ||
541 | |||
542 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Environment<Interner> { | ||
543 | let mut clauses = Vec::new(); | ||
544 | for pred in &self.predicates { | ||
545 | if pred.is_error() { | ||
546 | // for env, we just ignore errors | ||
547 | continue; | ||
548 | } | ||
549 | let program_clause: chalk_ir::ProgramClause<Interner> = | ||
550 | pred.clone().to_chalk(db).cast(&Interner); | ||
551 | clauses.push(program_clause.into_from_env_clause(&Interner)); | ||
552 | } | ||
553 | chalk_ir::Environment::new(&Interner).add_clauses(&Interner, clauses) | ||
554 | } | ||
555 | |||
556 | fn from_chalk( | ||
557 | _db: &dyn HirDatabase, | ||
558 | _env: chalk_ir::Environment<Interner>, | ||
559 | ) -> Arc<TraitEnvironment> { | ||
560 | unimplemented!() | ||
561 | } | ||
562 | } | ||
563 | |||
564 | impl<T: ToChalk> ToChalk for InEnvironment<T> | 454 | impl<T: ToChalk> ToChalk for InEnvironment<T> |
565 | where | 455 | where |
566 | T::Chalk: chalk_ir::interner::HasInterner<Interner = Interner>, | 456 | T::Chalk: chalk_ir::interner::HasInterner<Interner = Interner>, |
@@ -569,19 +459,16 @@ where | |||
569 | 459 | ||
570 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::InEnvironment<T::Chalk> { | 460 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::InEnvironment<T::Chalk> { |
571 | chalk_ir::InEnvironment { | 461 | chalk_ir::InEnvironment { |
572 | environment: self.environment.to_chalk(db), | 462 | environment: self.environment.env.clone(), |
573 | goal: self.value.to_chalk(db), | 463 | goal: self.value.to_chalk(db), |
574 | } | 464 | } |
575 | } | 465 | } |
576 | 466 | ||
577 | fn from_chalk( | 467 | fn from_chalk( |
578 | db: &dyn HirDatabase, | 468 | _db: &dyn HirDatabase, |
579 | in_env: chalk_ir::InEnvironment<T::Chalk>, | 469 | _in_env: chalk_ir::InEnvironment<T::Chalk>, |
580 | ) -> InEnvironment<T> { | 470 | ) -> InEnvironment<T> { |
581 | InEnvironment { | 471 | unimplemented!() |
582 | environment: from_chalk(db, in_env.environment), | ||
583 | value: from_chalk(db, in_env.goal), | ||
584 | } | ||
585 | } | 472 | } |
586 | } | 473 | } |
587 | 474 | ||
@@ -639,22 +526,24 @@ pub(super) fn generic_predicate_to_inline_bound( | |||
639 | Some(rust_ir::InlineBound::TraitBound(trait_bound)) | 526 | Some(rust_ir::InlineBound::TraitBound(trait_bound)) |
640 | } | 527 | } |
641 | GenericPredicate::Projection(proj) => { | 528 | GenericPredicate::Projection(proj) => { |
642 | if &proj.projection_ty.parameters[0] != self_ty { | 529 | if &proj.projection_ty.substitution[0] != self_ty { |
643 | return None; | 530 | return None; |
644 | } | 531 | } |
645 | let trait_ = match proj.projection_ty.associated_ty.lookup(db.upcast()).container { | 532 | let trait_ = match from_assoc_type_id(proj.projection_ty.associated_ty_id) |
533 | .lookup(db.upcast()) | ||
534 | .container | ||
535 | { | ||
646 | AssocContainerId::TraitId(t) => t, | 536 | AssocContainerId::TraitId(t) => t, |
647 | _ => panic!("associated type not in trait"), | 537 | _ => panic!("associated type not in trait"), |
648 | }; | 538 | }; |
649 | let args_no_self = proj.projection_ty.parameters[1..] | 539 | let args_no_self = proj.projection_ty.substitution[1..] |
650 | .iter() | 540 | .iter() |
651 | .map(|ty| ty.clone().to_chalk(db).cast(&Interner)) | 541 | .map(|ty| ty.clone().to_chalk(db).cast(&Interner)) |
652 | .collect(); | 542 | .collect(); |
653 | let alias_eq_bound = rust_ir::AliasEqBound { | 543 | let alias_eq_bound = rust_ir::AliasEqBound { |
654 | value: proj.ty.clone().to_chalk(db), | 544 | value: proj.ty.clone().to_chalk(db), |
655 | trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self }, | 545 | trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self }, |
656 | associated_ty_id: TypeAliasAsAssocType(proj.projection_ty.associated_ty) | 546 | associated_ty_id: proj.projection_ty.associated_ty_id, |
657 | .to_chalk(db), | ||
658 | parameters: Vec::new(), // FIXME we don't support generic associated types yet | 547 | parameters: Vec::new(), // FIXME we don't support generic associated types yet |
659 | }; | 548 | }; |
660 | Some(rust_ir::InlineBound::AliasEqBound(alias_eq_bound)) | 549 | 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, |