diff options
author | Zac Pullar-Strecker <[email protected]> | 2020-07-31 03:12:44 +0100 |
---|---|---|
committer | Zac Pullar-Strecker <[email protected]> | 2020-07-31 03:12:44 +0100 |
commit | f05d7b41a719d848844b054a16477b29d0f063c6 (patch) | |
tree | 0a8a0946e8aef2ce64d4c13d0035ba41cce2daf3 /crates/ra_hir_ty/src/traits/chalk | |
parent | 73ff610e41959e3e7c78a2b4b25b086883132956 (diff) | |
parent | 6b7cb8b5ab539fc4333ce34bc29bf77c976f232a (diff) |
Merge remote-tracking branch 'upstream/master' into 503-hover-doc-links
Hasn't fixed tests yet.
Diffstat (limited to 'crates/ra_hir_ty/src/traits/chalk')
-rw-r--r-- | crates/ra_hir_ty/src/traits/chalk/interner.rs | 29 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/traits/chalk/mapping.rs | 233 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/traits/chalk/tls.rs | 28 |
3 files changed, 179 insertions, 111 deletions
diff --git a/crates/ra_hir_ty/src/traits/chalk/interner.rs b/crates/ra_hir_ty/src/traits/chalk/interner.rs index 15426b022..8d4c51a8f 100644 --- a/crates/ra_hir_ty/src/traits/chalk/interner.rs +++ b/crates/ra_hir_ty/src/traits/chalk/interner.rs | |||
@@ -39,8 +39,9 @@ impl chalk_ir::interner::Interner for Interner { | |||
39 | type InternedQuantifiedWhereClauses = Vec<chalk_ir::QuantifiedWhereClause<Self>>; | 39 | type InternedQuantifiedWhereClauses = Vec<chalk_ir::QuantifiedWhereClause<Self>>; |
40 | type InternedVariableKinds = Vec<chalk_ir::VariableKind<Self>>; | 40 | type InternedVariableKinds = Vec<chalk_ir::VariableKind<Self>>; |
41 | type InternedCanonicalVarKinds = Vec<chalk_ir::CanonicalVarKind<Self>>; | 41 | type InternedCanonicalVarKinds = Vec<chalk_ir::CanonicalVarKind<Self>>; |
42 | type InternedConstraints = Vec<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>>; | ||
42 | type DefId = InternId; | 43 | type DefId = InternId; |
43 | type InternedAdtId = crate::TypeCtorId; | 44 | type InternedAdtId = hir_def::AdtId; |
44 | type Identifier = TypeAliasId; | 45 | type Identifier = TypeAliasId; |
45 | type FnAbi = (); | 46 | type FnAbi = (); |
46 | 47 | ||
@@ -349,6 +350,32 @@ impl chalk_ir::interner::Interner for Interner { | |||
349 | ) -> &'a [chalk_ir::CanonicalVarKind<Self>] { | 350 | ) -> &'a [chalk_ir::CanonicalVarKind<Self>] { |
350 | &canonical_var_kinds | 351 | &canonical_var_kinds |
351 | } | 352 | } |
353 | |||
354 | fn intern_constraints<E>( | ||
355 | &self, | ||
356 | data: impl IntoIterator<Item = Result<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>, E>>, | ||
357 | ) -> Result<Self::InternedConstraints, E> { | ||
358 | data.into_iter().collect() | ||
359 | } | ||
360 | |||
361 | fn constraints_data<'a>( | ||
362 | &self, | ||
363 | constraints: &'a Self::InternedConstraints, | ||
364 | ) -> &'a [chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>] { | ||
365 | constraints | ||
366 | } | ||
367 | fn debug_closure_id( | ||
368 | _fn_def_id: chalk_ir::ClosureId<Self>, | ||
369 | _fmt: &mut fmt::Formatter<'_>, | ||
370 | ) -> Option<fmt::Result> { | ||
371 | None | ||
372 | } | ||
373 | fn debug_constraints( | ||
374 | _clauses: &chalk_ir::Constraints<Self>, | ||
375 | _fmt: &mut fmt::Formatter<'_>, | ||
376 | ) -> Option<fmt::Result> { | ||
377 | None | ||
378 | } | ||
352 | } | 379 | } |
353 | 380 | ||
354 | impl chalk_ir::interner::HasInterner for Interner { | 381 | impl chalk_ir::interner::HasInterner for Interner { |
diff --git a/crates/ra_hir_ty/src/traits/chalk/mapping.rs b/crates/ra_hir_ty/src/traits/chalk/mapping.rs index ac82ea831..b3e92993d 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs | |||
@@ -15,9 +15,9 @@ use ra_db::salsa::InternKey; | |||
15 | use crate::{ | 15 | use crate::{ |
16 | db::HirDatabase, | 16 | db::HirDatabase, |
17 | primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness}, | 17 | primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness}, |
18 | traits::{builtin, AssocTyValue, Canonical, Impl, Obligation}, | 18 | traits::{Canonical, Obligation}, |
19 | ApplicationTy, CallableDef, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId, | 19 | ApplicationTy, CallableDefId, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId, |
20 | ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeCtor, | 20 | ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TyKind, TypeCtor, |
21 | }; | 21 | }; |
22 | 22 | ||
23 | use super::interner::*; | 23 | use super::interner::*; |
@@ -29,10 +29,17 @@ impl ToChalk for Ty { | |||
29 | match self { | 29 | match self { |
30 | Ty::Apply(apply_ty) => match apply_ty.ctor { | 30 | Ty::Apply(apply_ty) => match apply_ty.ctor { |
31 | TypeCtor::Ref(m) => ref_to_chalk(db, m, apply_ty.parameters), | 31 | TypeCtor::Ref(m) => ref_to_chalk(db, m, apply_ty.parameters), |
32 | TypeCtor::FnPtr { num_args: _ } => { | 32 | TypeCtor::Array => array_to_chalk(db, apply_ty.parameters), |
33 | TypeCtor::FnPtr { num_args: _, is_varargs } => { | ||
33 | let substitution = apply_ty.parameters.to_chalk(db).shifted_in(&Interner); | 34 | let substitution = apply_ty.parameters.to_chalk(db).shifted_in(&Interner); |
34 | chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: 0, substitution }) | 35 | chalk_ir::TyData::Function(chalk_ir::FnPointer { |
35 | .intern(&Interner) | 36 | num_binders: 0, |
37 | abi: (), | ||
38 | safety: chalk_ir::Safety::Safe, | ||
39 | variadic: is_varargs, | ||
40 | substitution, | ||
41 | }) | ||
42 | .intern(&Interner) | ||
36 | } | 43 | } |
37 | _ => { | 44 | _ => { |
38 | let name = apply_ty.ctor.to_chalk(db); | 45 | let name = apply_ty.ctor.to_chalk(db); |
@@ -61,13 +68,13 @@ impl ToChalk for Ty { | |||
61 | Ty::Bound(idx) => chalk_ir::TyData::BoundVar(idx).intern(&Interner), | 68 | Ty::Bound(idx) => chalk_ir::TyData::BoundVar(idx).intern(&Interner), |
62 | Ty::Infer(_infer_ty) => panic!("uncanonicalized infer ty"), | 69 | Ty::Infer(_infer_ty) => panic!("uncanonicalized infer ty"), |
63 | Ty::Dyn(predicates) => { | 70 | Ty::Dyn(predicates) => { |
64 | let where_clauses = chalk_ir::QuantifiedWhereClauses::from( | 71 | let where_clauses = chalk_ir::QuantifiedWhereClauses::from_iter( |
65 | &Interner, | 72 | &Interner, |
66 | predicates.iter().filter(|p| !p.is_error()).cloned().map(|p| p.to_chalk(db)), | 73 | predicates.iter().filter(|p| !p.is_error()).cloned().map(|p| p.to_chalk(db)), |
67 | ); | 74 | ); |
68 | let bounded_ty = chalk_ir::DynTy { | 75 | let bounded_ty = chalk_ir::DynTy { |
69 | bounds: make_binders(where_clauses, 1), | 76 | bounds: make_binders(where_clauses, 1), |
70 | lifetime: LIFETIME_PLACEHOLDER.to_lifetime(&Interner), | 77 | lifetime: FAKE_PLACEHOLDER.to_lifetime(&Interner), |
71 | }; | 78 | }; |
72 | chalk_ir::TyData::Dyn(bounded_ty).intern(&Interner) | 79 | chalk_ir::TyData::Dyn(bounded_ty).intern(&Interner) |
73 | } | 80 | } |
@@ -92,6 +99,7 @@ impl ToChalk for Ty { | |||
92 | chalk_ir::TyData::Apply(apply_ty) => match apply_ty.name { | 99 | chalk_ir::TyData::Apply(apply_ty) => match apply_ty.name { |
93 | TypeName::Error => Ty::Unknown, | 100 | TypeName::Error => Ty::Unknown, |
94 | TypeName::Ref(m) => ref_from_chalk(db, m, apply_ty.substitution), | 101 | TypeName::Ref(m) => ref_from_chalk(db, m, apply_ty.substitution), |
102 | TypeName::Array => array_from_chalk(db, apply_ty.substitution), | ||
95 | _ => { | 103 | _ => { |
96 | let ctor = from_chalk(db, apply_ty.name); | 104 | let ctor = from_chalk(db, apply_ty.name); |
97 | let parameters = from_chalk(db, apply_ty.substitution); | 105 | let parameters = from_chalk(db, apply_ty.substitution); |
@@ -115,10 +123,22 @@ impl ToChalk for Ty { | |||
115 | let parameters = from_chalk(db, opaque_ty.substitution); | 123 | let parameters = from_chalk(db, opaque_ty.substitution); |
116 | Ty::Opaque(OpaqueTy { opaque_ty_id: impl_trait_id, parameters }) | 124 | Ty::Opaque(OpaqueTy { opaque_ty_id: impl_trait_id, parameters }) |
117 | } | 125 | } |
118 | chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: _, substitution }) => { | 126 | chalk_ir::TyData::Function(chalk_ir::FnPointer { |
119 | let parameters: Substs = from_chalk(db, substitution); | 127 | num_binders, |
128 | variadic, | ||
129 | substitution, | ||
130 | .. | ||
131 | }) => { | ||
132 | assert_eq!(num_binders, 0); | ||
133 | let parameters: Substs = from_chalk( | ||
134 | db, | ||
135 | substitution.shifted_out(&Interner).expect("fn ptr should have no binders"), | ||
136 | ); | ||
120 | Ty::Apply(ApplicationTy { | 137 | Ty::Apply(ApplicationTy { |
121 | ctor: TypeCtor::FnPtr { num_args: (parameters.len() - 1) as u16 }, | 138 | ctor: TypeCtor::FnPtr { |
139 | num_args: (parameters.len() - 1) as u16, | ||
140 | is_varargs: variadic, | ||
141 | }, | ||
122 | parameters, | 142 | parameters, |
123 | }) | 143 | }) |
124 | } | 144 | } |
@@ -138,7 +158,7 @@ impl ToChalk for Ty { | |||
138 | } | 158 | } |
139 | } | 159 | } |
140 | 160 | ||
141 | const LIFETIME_PLACEHOLDER: PlaceholderIndex = | 161 | const FAKE_PLACEHOLDER: PlaceholderIndex = |
142 | PlaceholderIndex { ui: UniverseIndex::ROOT, idx: usize::MAX }; | 162 | PlaceholderIndex { ui: UniverseIndex::ROOT, idx: usize::MAX }; |
143 | 163 | ||
144 | /// We currently don't model lifetimes, but Chalk does. So, we have to insert a | 164 | /// We currently don't model lifetimes, but Chalk does. So, we have to insert a |
@@ -149,10 +169,10 @@ fn ref_to_chalk( | |||
149 | subst: Substs, | 169 | subst: Substs, |
150 | ) -> chalk_ir::Ty<Interner> { | 170 | ) -> chalk_ir::Ty<Interner> { |
151 | let arg = subst[0].clone().to_chalk(db); | 171 | let arg = subst[0].clone().to_chalk(db); |
152 | let lifetime = LIFETIME_PLACEHOLDER.to_lifetime(&Interner); | 172 | let lifetime = FAKE_PLACEHOLDER.to_lifetime(&Interner); |
153 | chalk_ir::ApplicationTy { | 173 | chalk_ir::ApplicationTy { |
154 | name: TypeName::Ref(mutability.to_chalk(db)), | 174 | name: TypeName::Ref(mutability.to_chalk(db)), |
155 | substitution: chalk_ir::Substitution::from( | 175 | substitution: chalk_ir::Substitution::from_iter( |
156 | &Interner, | 176 | &Interner, |
157 | vec![lifetime.cast(&Interner), arg.cast(&Interner)], | 177 | vec![lifetime.cast(&Interner), arg.cast(&Interner)], |
158 | ), | 178 | ), |
@@ -173,11 +193,40 @@ fn ref_from_chalk( | |||
173 | Ty::apply(TypeCtor::Ref(from_chalk(db, mutability)), Substs(tys)) | 193 | Ty::apply(TypeCtor::Ref(from_chalk(db, mutability)), Substs(tys)) |
174 | } | 194 | } |
175 | 195 | ||
196 | /// We currently don't model constants, but Chalk does. So, we have to insert a | ||
197 | /// fake constant here, because Chalks built-in logic may expect it to be there. | ||
198 | fn array_to_chalk(db: &dyn HirDatabase, subst: Substs) -> chalk_ir::Ty<Interner> { | ||
199 | let arg = subst[0].clone().to_chalk(db); | ||
200 | let usize_ty = chalk_ir::ApplicationTy { | ||
201 | name: TypeName::Scalar(Scalar::Uint(chalk_ir::UintTy::Usize)), | ||
202 | substitution: chalk_ir::Substitution::empty(&Interner), | ||
203 | } | ||
204 | .intern(&Interner); | ||
205 | let const_ = FAKE_PLACEHOLDER.to_const(&Interner, usize_ty); | ||
206 | chalk_ir::ApplicationTy { | ||
207 | name: TypeName::Array, | ||
208 | substitution: chalk_ir::Substitution::from_iter( | ||
209 | &Interner, | ||
210 | vec![arg.cast(&Interner), const_.cast(&Interner)], | ||
211 | ), | ||
212 | } | ||
213 | .intern(&Interner) | ||
214 | } | ||
215 | |||
216 | /// Here we remove the const from the type we got from Chalk. | ||
217 | fn array_from_chalk(db: &dyn HirDatabase, subst: chalk_ir::Substitution<Interner>) -> Ty { | ||
218 | let tys = subst | ||
219 | .iter(&Interner) | ||
220 | .filter_map(|p| Some(from_chalk(db, p.ty(&Interner)?.clone()))) | ||
221 | .collect(); | ||
222 | Ty::apply(TypeCtor::Array, Substs(tys)) | ||
223 | } | ||
224 | |||
176 | impl ToChalk for Substs { | 225 | impl ToChalk for Substs { |
177 | type Chalk = chalk_ir::Substitution<Interner>; | 226 | type Chalk = chalk_ir::Substitution<Interner>; |
178 | 227 | ||
179 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Substitution<Interner> { | 228 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Substitution<Interner> { |
180 | chalk_ir::Substitution::from(&Interner, self.iter().map(|ty| ty.clone().to_chalk(db))) | 229 | chalk_ir::Substitution::from_iter(&Interner, self.iter().map(|ty| ty.clone().to_chalk(db))) |
181 | } | 230 | } |
182 | 231 | ||
183 | fn from_chalk(db: &dyn HirDatabase, parameters: chalk_ir::Substitution<Interner>) -> Substs { | 232 | fn from_chalk(db: &dyn HirDatabase, parameters: chalk_ir::Substitution<Interner>) -> Substs { |
@@ -263,6 +312,7 @@ impl ToChalk for TypeCtor { | |||
263 | TypeCtor::Tuple { cardinality } => TypeName::Tuple(cardinality.into()), | 312 | TypeCtor::Tuple { cardinality } => TypeName::Tuple(cardinality.into()), |
264 | TypeCtor::RawPtr(mutability) => TypeName::Raw(mutability.to_chalk(db)), | 313 | TypeCtor::RawPtr(mutability) => TypeName::Raw(mutability.to_chalk(db)), |
265 | TypeCtor::Slice => TypeName::Slice, | 314 | TypeCtor::Slice => TypeName::Slice, |
315 | TypeCtor::Array => TypeName::Array, | ||
266 | TypeCtor::Ref(mutability) => TypeName::Ref(mutability.to_chalk(db)), | 316 | TypeCtor::Ref(mutability) => TypeName::Ref(mutability.to_chalk(db)), |
267 | TypeCtor::Str => TypeName::Str, | 317 | TypeCtor::Str => TypeName::Str, |
268 | TypeCtor::FnDef(callable_def) => { | 318 | TypeCtor::FnDef(callable_def) => { |
@@ -271,20 +321,24 @@ impl ToChalk for TypeCtor { | |||
271 | } | 321 | } |
272 | TypeCtor::Never => TypeName::Never, | 322 | TypeCtor::Never => TypeName::Never, |
273 | 323 | ||
274 | TypeCtor::Adt(_) | 324 | TypeCtor::Closure { def, expr } => { |
275 | | TypeCtor::Array | 325 | let closure_id = db.intern_closure((def, expr)); |
276 | | TypeCtor::FnPtr { .. } | 326 | TypeName::Closure(closure_id.into()) |
277 | | TypeCtor::Closure { .. } => { | 327 | } |
278 | // other TypeCtors get interned and turned into a chalk StructId | 328 | |
279 | let struct_id = db.intern_type_ctor(self).into(); | 329 | TypeCtor::Adt(adt_id) => TypeName::Adt(chalk_ir::AdtId(adt_id)), |
280 | TypeName::Adt(struct_id) | 330 | |
331 | TypeCtor::FnPtr { .. } => { | ||
332 | // This should not be reached, since Chalk doesn't represent | ||
333 | // function pointers with TypeName | ||
334 | unreachable!() | ||
281 | } | 335 | } |
282 | } | 336 | } |
283 | } | 337 | } |
284 | 338 | ||
285 | fn from_chalk(db: &dyn HirDatabase, type_name: TypeName<Interner>) -> TypeCtor { | 339 | fn from_chalk(db: &dyn HirDatabase, type_name: TypeName<Interner>) -> TypeCtor { |
286 | match type_name { | 340 | match type_name { |
287 | TypeName::Adt(struct_id) => db.lookup_intern_type_ctor(struct_id.into()), | 341 | TypeName::Adt(struct_id) => TypeCtor::Adt(struct_id.0), |
288 | TypeName::AssociatedType(type_id) => TypeCtor::AssociatedType(from_chalk(db, type_id)), | 342 | TypeName::AssociatedType(type_id) => TypeCtor::AssociatedType(from_chalk(db, type_id)), |
289 | TypeName::OpaqueType(opaque_type_id) => { | 343 | TypeName::OpaqueType(opaque_type_id) => { |
290 | TypeCtor::OpaqueType(from_chalk(db, opaque_type_id)) | 344 | TypeCtor::OpaqueType(from_chalk(db, opaque_type_id)) |
@@ -317,13 +371,16 @@ impl ToChalk for TypeCtor { | |||
317 | let callable_def = from_chalk(db, fn_def_id); | 371 | let callable_def = from_chalk(db, fn_def_id); |
318 | TypeCtor::FnDef(callable_def) | 372 | TypeCtor::FnDef(callable_def) |
319 | } | 373 | } |
374 | TypeName::Array => TypeCtor::Array, | ||
320 | 375 | ||
321 | TypeName::Array | TypeName::Error => { | 376 | TypeName::Closure(id) => { |
322 | // this should not be reached, since we don't represent TypeName::Error with TypeCtor | 377 | let id: crate::db::ClosureId = id.into(); |
323 | unreachable!() | 378 | let (def, expr) = db.lookup_intern_closure(id); |
379 | TypeCtor::Closure { def, expr } | ||
324 | } | 380 | } |
325 | TypeName::Closure(_) => { | 381 | |
326 | // FIXME: implement closure support | 382 | TypeName::Error => { |
383 | // this should not be reached, since we don't represent TypeName::Error with TypeCtor | ||
327 | unreachable!() | 384 | unreachable!() |
328 | } | 385 | } |
329 | } | 386 | } |
@@ -395,26 +452,26 @@ impl ToChalk for Mutability { | |||
395 | } | 452 | } |
396 | } | 453 | } |
397 | 454 | ||
398 | impl ToChalk for Impl { | 455 | impl ToChalk for hir_def::ImplId { |
399 | type Chalk = ImplId; | 456 | type Chalk = ImplId; |
400 | 457 | ||
401 | fn to_chalk(self, db: &dyn HirDatabase) -> ImplId { | 458 | fn to_chalk(self, _db: &dyn HirDatabase) -> ImplId { |
402 | db.intern_chalk_impl(self).into() | 459 | chalk_ir::ImplId(self.as_intern_id()) |
403 | } | 460 | } |
404 | 461 | ||
405 | fn from_chalk(db: &dyn HirDatabase, impl_id: ImplId) -> Impl { | 462 | fn from_chalk(_db: &dyn HirDatabase, impl_id: ImplId) -> hir_def::ImplId { |
406 | db.lookup_intern_chalk_impl(impl_id.into()) | 463 | InternKey::from_intern_id(impl_id.0) |
407 | } | 464 | } |
408 | } | 465 | } |
409 | 466 | ||
410 | impl ToChalk for CallableDef { | 467 | impl ToChalk for CallableDefId { |
411 | type Chalk = FnDefId; | 468 | type Chalk = FnDefId; |
412 | 469 | ||
413 | fn to_chalk(self, db: &dyn HirDatabase) -> FnDefId { | 470 | fn to_chalk(self, db: &dyn HirDatabase) -> FnDefId { |
414 | db.intern_callable_def(self).into() | 471 | db.intern_callable_def(self).into() |
415 | } | 472 | } |
416 | 473 | ||
417 | fn from_chalk(db: &dyn HirDatabase, fn_def_id: FnDefId) -> CallableDef { | 474 | fn from_chalk(db: &dyn HirDatabase, fn_def_id: FnDefId) -> CallableDefId { |
418 | db.lookup_intern_callable_def(fn_def_id.into()) | 475 | db.lookup_intern_callable_def(fn_def_id.into()) |
419 | } | 476 | } |
420 | } | 477 | } |
@@ -431,15 +488,20 @@ impl ToChalk for TypeAliasId { | |||
431 | } | 488 | } |
432 | } | 489 | } |
433 | 490 | ||
434 | impl ToChalk for AssocTyValue { | 491 | pub struct TypeAliasAsValue(pub TypeAliasId); |
492 | |||
493 | impl ToChalk for TypeAliasAsValue { | ||
435 | type Chalk = AssociatedTyValueId; | 494 | type Chalk = AssociatedTyValueId; |
436 | 495 | ||
437 | fn to_chalk(self, db: &dyn HirDatabase) -> AssociatedTyValueId { | 496 | fn to_chalk(self, _db: &dyn HirDatabase) -> AssociatedTyValueId { |
438 | db.intern_assoc_ty_value(self).into() | 497 | rust_ir::AssociatedTyValueId(self.0.as_intern_id()) |
439 | } | 498 | } |
440 | 499 | ||
441 | fn from_chalk(db: &dyn HirDatabase, assoc_ty_value_id: AssociatedTyValueId) -> AssocTyValue { | 500 | fn from_chalk( |
442 | db.lookup_intern_assoc_ty_value(assoc_ty_value_id.into()) | 501 | _db: &dyn HirDatabase, |
502 | assoc_ty_value_id: AssociatedTyValueId, | ||
503 | ) -> TypeAliasAsValue { | ||
504 | TypeAliasAsValue(TypeAliasId::from_intern_id(assoc_ty_value_id.0)) | ||
443 | } | 505 | } |
444 | } | 506 | } |
445 | 507 | ||
@@ -492,6 +554,11 @@ impl ToChalk for GenericPredicate { | |||
492 | // we shouldn't get these from Chalk | 554 | // we shouldn't get these from Chalk |
493 | panic!("encountered LifetimeOutlives from Chalk") | 555 | panic!("encountered LifetimeOutlives from Chalk") |
494 | } | 556 | } |
557 | |||
558 | chalk_ir::WhereClause::TypeOutlives(_) => { | ||
559 | // we shouldn't get these from Chalk | ||
560 | panic!("encountered TypeOutlives from Chalk") | ||
561 | } | ||
495 | } | 562 | } |
496 | } | 563 | } |
497 | } | 564 | } |
@@ -555,22 +622,42 @@ where | |||
555 | type Chalk = chalk_ir::Canonical<T::Chalk>; | 622 | type Chalk = chalk_ir::Canonical<T::Chalk>; |
556 | 623 | ||
557 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Canonical<T::Chalk> { | 624 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Canonical<T::Chalk> { |
558 | let parameter = chalk_ir::CanonicalVarKind::new( | 625 | let kinds = self |
559 | chalk_ir::VariableKind::Ty(chalk_ir::TyKind::General), | 626 | .kinds |
560 | chalk_ir::UniverseIndex::ROOT, | 627 | .iter() |
561 | ); | 628 | .map(|k| match k { |
629 | TyKind::General => chalk_ir::TyKind::General, | ||
630 | TyKind::Integer => chalk_ir::TyKind::Integer, | ||
631 | TyKind::Float => chalk_ir::TyKind::Float, | ||
632 | }) | ||
633 | .map(|tk| { | ||
634 | chalk_ir::CanonicalVarKind::new( | ||
635 | chalk_ir::VariableKind::Ty(tk), | ||
636 | chalk_ir::UniverseIndex::ROOT, | ||
637 | ) | ||
638 | }); | ||
562 | let value = self.value.to_chalk(db); | 639 | let value = self.value.to_chalk(db); |
563 | chalk_ir::Canonical { | 640 | chalk_ir::Canonical { |
564 | value, | 641 | value, |
565 | binders: chalk_ir::CanonicalVarKinds::from(&Interner, vec![parameter; self.num_vars]), | 642 | binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds), |
566 | } | 643 | } |
567 | } | 644 | } |
568 | 645 | ||
569 | fn from_chalk(db: &dyn HirDatabase, canonical: chalk_ir::Canonical<T::Chalk>) -> Canonical<T> { | 646 | fn from_chalk(db: &dyn HirDatabase, canonical: chalk_ir::Canonical<T::Chalk>) -> Canonical<T> { |
570 | Canonical { | 647 | let kinds = canonical |
571 | num_vars: canonical.binders.len(&Interner), | 648 | .binders |
572 | value: from_chalk(db, canonical.value), | 649 | .iter(&Interner) |
573 | } | 650 | .map(|k| match k.kind { |
651 | chalk_ir::VariableKind::Ty(tk) => match tk { | ||
652 | chalk_ir::TyKind::General => TyKind::General, | ||
653 | chalk_ir::TyKind::Integer => TyKind::Integer, | ||
654 | chalk_ir::TyKind::Float => TyKind::Float, | ||
655 | }, | ||
656 | chalk_ir::VariableKind::Lifetime => panic!("unexpected lifetime from Chalk"), | ||
657 | chalk_ir::VariableKind::Const(_) => panic!("unexpected const from Chalk"), | ||
658 | }) | ||
659 | .collect(); | ||
660 | Canonical { kinds, value: from_chalk(db, canonical.value) } | ||
574 | } | 661 | } |
575 | } | 662 | } |
576 | 663 | ||
@@ -623,58 +710,12 @@ where | |||
623 | } | 710 | } |
624 | } | 711 | } |
625 | 712 | ||
626 | impl ToChalk for builtin::BuiltinImplData { | ||
627 | type Chalk = ImplDatum; | ||
628 | |||
629 | fn to_chalk(self, db: &dyn HirDatabase) -> ImplDatum { | ||
630 | let impl_type = rust_ir::ImplType::External; | ||
631 | let where_clauses = self.where_clauses.into_iter().map(|w| w.to_chalk(db)).collect(); | ||
632 | |||
633 | let impl_datum_bound = | ||
634 | rust_ir::ImplDatumBound { trait_ref: self.trait_ref.to_chalk(db), where_clauses }; | ||
635 | let associated_ty_value_ids = | ||
636 | self.assoc_ty_values.into_iter().map(|v| v.to_chalk(db)).collect(); | ||
637 | rust_ir::ImplDatum { | ||
638 | binders: make_binders(impl_datum_bound, self.num_vars), | ||
639 | impl_type, | ||
640 | polarity: rust_ir::Polarity::Positive, | ||
641 | associated_ty_value_ids, | ||
642 | } | ||
643 | } | ||
644 | |||
645 | fn from_chalk(_db: &dyn HirDatabase, _data: ImplDatum) -> Self { | ||
646 | unimplemented!() | ||
647 | } | ||
648 | } | ||
649 | |||
650 | impl ToChalk for builtin::BuiltinImplAssocTyValueData { | ||
651 | type Chalk = AssociatedTyValue; | ||
652 | |||
653 | fn to_chalk(self, db: &dyn HirDatabase) -> AssociatedTyValue { | ||
654 | let ty = self.value.to_chalk(db); | ||
655 | let value_bound = rust_ir::AssociatedTyValueBound { ty }; | ||
656 | |||
657 | rust_ir::AssociatedTyValue { | ||
658 | associated_ty_id: self.assoc_ty_id.to_chalk(db), | ||
659 | impl_id: self.impl_.to_chalk(db), | ||
660 | value: make_binders(value_bound, self.num_vars), | ||
661 | } | ||
662 | } | ||
663 | |||
664 | fn from_chalk( | ||
665 | _db: &dyn HirDatabase, | ||
666 | _data: AssociatedTyValue, | ||
667 | ) -> builtin::BuiltinImplAssocTyValueData { | ||
668 | unimplemented!() | ||
669 | } | ||
670 | } | ||
671 | |||
672 | pub(super) fn make_binders<T>(value: T, num_vars: usize) -> chalk_ir::Binders<T> | 713 | pub(super) fn make_binders<T>(value: T, num_vars: usize) -> chalk_ir::Binders<T> |
673 | where | 714 | where |
674 | T: HasInterner<Interner = Interner>, | 715 | T: HasInterner<Interner = Interner>, |
675 | { | 716 | { |
676 | chalk_ir::Binders::new( | 717 | chalk_ir::Binders::new( |
677 | chalk_ir::VariableKinds::from( | 718 | chalk_ir::VariableKinds::from_iter( |
678 | &Interner, | 719 | &Interner, |
679 | std::iter::repeat(chalk_ir::VariableKind::Ty(chalk_ir::TyKind::General)).take(num_vars), | 720 | std::iter::repeat(chalk_ir::VariableKind::Ty(chalk_ir::TyKind::General)).take(num_vars), |
680 | ), | 721 | ), |
diff --git a/crates/ra_hir_ty/src/traits/chalk/tls.rs b/crates/ra_hir_ty/src/traits/chalk/tls.rs index 556af7098..db915625c 100644 --- a/crates/ra_hir_ty/src/traits/chalk/tls.rs +++ b/crates/ra_hir_ty/src/traits/chalk/tls.rs | |||
@@ -5,12 +5,12 @@ use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplicat | |||
5 | use itertools::Itertools; | 5 | use itertools::Itertools; |
6 | 6 | ||
7 | use super::{from_chalk, Interner}; | 7 | use super::{from_chalk, Interner}; |
8 | use crate::{db::HirDatabase, CallableDef, TypeCtor}; | 8 | use crate::{db::HirDatabase, CallableDefId, TypeCtor}; |
9 | use hir_def::{AdtId, AssocContainerId, DefWithBodyId, Lookup, TypeAliasId}; | 9 | use hir_def::{AdtId, AssocContainerId, DefWithBodyId, Lookup, TypeAliasId}; |
10 | 10 | ||
11 | pub use unsafe_tls::{set_current_program, with_current_program}; | 11 | pub use unsafe_tls::{set_current_program, with_current_program}; |
12 | 12 | ||
13 | pub struct DebugContext<'a>(&'a (dyn HirDatabase + 'a)); | 13 | pub struct DebugContext<'a>(&'a dyn HirDatabase); |
14 | 14 | ||
15 | impl DebugContext<'_> { | 15 | impl DebugContext<'_> { |
16 | pub fn debug_struct_id( | 16 | pub fn debug_struct_id( |
@@ -38,16 +38,16 @@ impl DebugContext<'_> { | |||
38 | } | 38 | } |
39 | TypeCtor::FnDef(def) => { | 39 | TypeCtor::FnDef(def) => { |
40 | let name = match def { | 40 | let name = match def { |
41 | CallableDef::FunctionId(ff) => self.0.function_data(ff).name.clone(), | 41 | CallableDefId::FunctionId(ff) => self.0.function_data(ff).name.clone(), |
42 | CallableDef::StructId(s) => self.0.struct_data(s).name.clone(), | 42 | CallableDefId::StructId(s) => self.0.struct_data(s).name.clone(), |
43 | CallableDef::EnumVariantId(e) => { | 43 | CallableDefId::EnumVariantId(e) => { |
44 | let enum_data = self.0.enum_data(e.parent); | 44 | let enum_data = self.0.enum_data(e.parent); |
45 | enum_data.variants[e.local_id].name.clone() | 45 | enum_data.variants[e.local_id].name.clone() |
46 | } | 46 | } |
47 | }; | 47 | }; |
48 | match def { | 48 | match def { |
49 | CallableDef::FunctionId(_) => write!(f, "{{fn {}}}", name)?, | 49 | CallableDefId::FunctionId(_) => write!(f, "{{fn {}}}", name)?, |
50 | CallableDef::StructId(_) | CallableDef::EnumVariantId(_) => { | 50 | CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_) => { |
51 | write!(f, "{{ctor {}}}", name)? | 51 | write!(f, "{{ctor {}}}", name)? |
52 | } | 52 | } |
53 | } | 53 | } |
@@ -157,7 +157,7 @@ impl DebugContext<'_> { | |||
157 | _ => panic!("associated type not in trait"), | 157 | _ => panic!("associated type not in trait"), |
158 | }; | 158 | }; |
159 | let trait_data = self.0.trait_data(trait_); | 159 | let trait_data = self.0.trait_data(trait_); |
160 | let params = projection_ty.substitution.parameters(&Interner); | 160 | let params = projection_ty.substitution.as_slice(&Interner); |
161 | write!(fmt, "<{:?} as {}", ¶ms[0], trait_data.name,)?; | 161 | write!(fmt, "<{:?} as {}", ¶ms[0], trait_data.name,)?; |
162 | if params.len() > 1 { | 162 | if params.len() > 1 { |
163 | write!( | 163 | write!( |
@@ -255,18 +255,18 @@ impl DebugContext<'_> { | |||
255 | fn_def_id: chalk_ir::FnDefId<Interner>, | 255 | fn_def_id: chalk_ir::FnDefId<Interner>, |
256 | fmt: &mut fmt::Formatter<'_>, | 256 | fmt: &mut fmt::Formatter<'_>, |
257 | ) -> Result<(), fmt::Error> { | 257 | ) -> Result<(), fmt::Error> { |
258 | let def: CallableDef = from_chalk(self.0, fn_def_id); | 258 | let def: CallableDefId = from_chalk(self.0, fn_def_id); |
259 | let name = match def { | 259 | let name = match def { |
260 | CallableDef::FunctionId(ff) => self.0.function_data(ff).name.clone(), | 260 | CallableDefId::FunctionId(ff) => self.0.function_data(ff).name.clone(), |
261 | CallableDef::StructId(s) => self.0.struct_data(s).name.clone(), | 261 | CallableDefId::StructId(s) => self.0.struct_data(s).name.clone(), |
262 | CallableDef::EnumVariantId(e) => { | 262 | CallableDefId::EnumVariantId(e) => { |
263 | let enum_data = self.0.enum_data(e.parent); | 263 | let enum_data = self.0.enum_data(e.parent); |
264 | enum_data.variants[e.local_id].name.clone() | 264 | enum_data.variants[e.local_id].name.clone() |
265 | } | 265 | } |
266 | }; | 266 | }; |
267 | match def { | 267 | match def { |
268 | CallableDef::FunctionId(_) => write!(fmt, "{{fn {}}}", name), | 268 | CallableDefId::FunctionId(_) => write!(fmt, "{{fn {}}}", name), |
269 | CallableDef::StructId(_) | CallableDef::EnumVariantId(_) => { | 269 | CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_) => { |
270 | write!(fmt, "{{ctor {}}}", name) | 270 | write!(fmt, "{{ctor {}}}", name) |
271 | } | 271 | } |
272 | } | 272 | } |