diff options
Diffstat (limited to 'crates/ra_hir_ty/src/traits/chalk')
-rw-r--r-- | crates/ra_hir_ty/src/traits/chalk/interner.rs | 14 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/traits/chalk/mapping.rs | 93 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/traits/chalk/tls.rs | 18 |
3 files changed, 85 insertions, 40 deletions
diff --git a/crates/ra_hir_ty/src/traits/chalk/interner.rs b/crates/ra_hir_ty/src/traits/chalk/interner.rs index 060372819..e27074ba6 100644 --- a/crates/ra_hir_ty/src/traits/chalk/interner.rs +++ b/crates/ra_hir_ty/src/traits/chalk/interner.rs | |||
@@ -11,15 +11,17 @@ use std::{fmt, sync::Arc}; | |||
11 | pub struct Interner; | 11 | pub struct Interner; |
12 | 12 | ||
13 | pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>; | 13 | pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>; |
14 | pub type AssociatedTyDatum = chalk_rust_ir::AssociatedTyDatum<Interner>; | 14 | pub type AssociatedTyDatum = chalk_solve::rust_ir::AssociatedTyDatum<Interner>; |
15 | pub type TraitId = chalk_ir::TraitId<Interner>; | 15 | pub type TraitId = chalk_ir::TraitId<Interner>; |
16 | pub type TraitDatum = chalk_rust_ir::TraitDatum<Interner>; | 16 | pub type TraitDatum = chalk_solve::rust_ir::TraitDatum<Interner>; |
17 | pub type AdtId = chalk_ir::AdtId<Interner>; | 17 | pub type AdtId = chalk_ir::AdtId<Interner>; |
18 | pub type StructDatum = chalk_rust_ir::AdtDatum<Interner>; | 18 | pub type StructDatum = chalk_solve::rust_ir::AdtDatum<Interner>; |
19 | pub type ImplId = chalk_ir::ImplId<Interner>; | 19 | pub type ImplId = chalk_ir::ImplId<Interner>; |
20 | pub type ImplDatum = chalk_rust_ir::ImplDatum<Interner>; | 20 | pub type ImplDatum = chalk_solve::rust_ir::ImplDatum<Interner>; |
21 | pub type AssociatedTyValueId = chalk_rust_ir::AssociatedTyValueId<Interner>; | 21 | pub type AssociatedTyValueId = chalk_solve::rust_ir::AssociatedTyValueId<Interner>; |
22 | pub type AssociatedTyValue = chalk_rust_ir::AssociatedTyValue<Interner>; | 22 | pub type AssociatedTyValue = chalk_solve::rust_ir::AssociatedTyValue<Interner>; |
23 | pub type FnDefId = chalk_ir::FnDefId<Interner>; | ||
24 | pub type FnDefDatum = chalk_solve::rust_ir::FnDefDatum<Interner>; | ||
23 | 25 | ||
24 | impl chalk_ir::interner::Interner for Interner { | 26 | impl chalk_ir::interner::Interner for Interner { |
25 | type InternedType = Box<chalk_ir::TyData<Self>>; // FIXME use Arc? | 27 | type InternedType = Box<chalk_ir::TyData<Self>>; // FIXME use Arc? |
diff --git a/crates/ra_hir_ty/src/traits/chalk/mapping.rs b/crates/ra_hir_ty/src/traits/chalk/mapping.rs index a83d82fd8..5f6daf842 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs | |||
@@ -7,6 +7,7 @@ use chalk_ir::{ | |||
7 | cast::Cast, fold::shift::Shift, interner::HasInterner, PlaceholderIndex, Scalar, TypeName, | 7 | cast::Cast, fold::shift::Shift, interner::HasInterner, PlaceholderIndex, Scalar, TypeName, |
8 | UniverseIndex, | 8 | UniverseIndex, |
9 | }; | 9 | }; |
10 | use chalk_solve::rust_ir; | ||
10 | 11 | ||
11 | use hir_def::{type_ref::Mutability, AssocContainerId, GenericDefId, Lookup, TypeAliasId}; | 12 | use hir_def::{type_ref::Mutability, AssocContainerId, GenericDefId, Lookup, TypeAliasId}; |
12 | use ra_db::salsa::InternKey; | 13 | use ra_db::salsa::InternKey; |
@@ -15,8 +16,8 @@ use crate::{ | |||
15 | db::HirDatabase, | 16 | db::HirDatabase, |
16 | primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness, Uncertain}, | 17 | primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness, Uncertain}, |
17 | traits::{builtin, AssocTyValue, Canonical, Impl, Obligation}, | 18 | traits::{builtin, AssocTyValue, Canonical, Impl, Obligation}, |
18 | ApplicationTy, GenericPredicate, InEnvironment, ProjectionPredicate, ProjectionTy, Substs, | 19 | ApplicationTy, CallableDef, GenericPredicate, InEnvironment, ProjectionPredicate, ProjectionTy, |
19 | TraitEnvironment, TraitRef, Ty, TypeCtor, | 20 | Substs, TraitEnvironment, TraitRef, Ty, TypeCtor, |
20 | }; | 21 | }; |
21 | 22 | ||
22 | use super::interner::*; | 23 | use super::interner::*; |
@@ -26,14 +27,19 @@ impl ToChalk for Ty { | |||
26 | type Chalk = chalk_ir::Ty<Interner>; | 27 | type Chalk = chalk_ir::Ty<Interner>; |
27 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> { | 28 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> { |
28 | match self { | 29 | match self { |
29 | Ty::Apply(apply_ty) => { | 30 | Ty::Apply(apply_ty) => match apply_ty.ctor { |
30 | if let TypeCtor::Ref(m) = apply_ty.ctor { | 31 | TypeCtor::Ref(m) => ref_to_chalk(db, m, apply_ty.parameters), |
31 | return ref_to_chalk(db, m, apply_ty.parameters); | 32 | TypeCtor::FnPtr { num_args: _ } => { |
33 | let substitution = apply_ty.parameters.to_chalk(db).shifted_in(&Interner); | ||
34 | chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: 0, substitution }) | ||
35 | .intern(&Interner) | ||
32 | } | 36 | } |
33 | let name = apply_ty.ctor.to_chalk(db); | 37 | _ => { |
34 | let substitution = apply_ty.parameters.to_chalk(db); | 38 | let name = apply_ty.ctor.to_chalk(db); |
35 | chalk_ir::ApplicationTy { name, substitution }.cast(&Interner).intern(&Interner) | 39 | let substitution = apply_ty.parameters.to_chalk(db); |
36 | } | 40 | chalk_ir::ApplicationTy { name, substitution }.cast(&Interner).intern(&Interner) |
41 | } | ||
42 | }, | ||
37 | Ty::Projection(proj_ty) => { | 43 | Ty::Projection(proj_ty) => { |
38 | let associated_ty_id = proj_ty.associated_ty.to_chalk(db); | 44 | let associated_ty_id = proj_ty.associated_ty.to_chalk(db); |
39 | let substitution = proj_ty.parameters.to_chalk(db); | 45 | let substitution = proj_ty.parameters.to_chalk(db); |
@@ -93,9 +99,15 @@ impl ToChalk for Ty { | |||
93 | Ty::Projection(ProjectionTy { associated_ty, parameters }) | 99 | Ty::Projection(ProjectionTy { associated_ty, parameters }) |
94 | } | 100 | } |
95 | chalk_ir::TyData::Alias(chalk_ir::AliasTy::Opaque(_)) => unimplemented!(), | 101 | chalk_ir::TyData::Alias(chalk_ir::AliasTy::Opaque(_)) => unimplemented!(), |
96 | chalk_ir::TyData::Function(_) => unimplemented!(), | 102 | chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: _, substitution }) => { |
103 | let parameters: Substs = from_chalk(db, substitution); | ||
104 | Ty::Apply(ApplicationTy { | ||
105 | ctor: TypeCtor::FnPtr { num_args: (parameters.len() - 1) as u16 }, | ||
106 | parameters, | ||
107 | }) | ||
108 | } | ||
97 | chalk_ir::TyData::BoundVar(idx) => Ty::Bound(idx), | 109 | chalk_ir::TyData::BoundVar(idx) => Ty::Bound(idx), |
98 | chalk_ir::TyData::InferenceVar(_iv) => Ty::Unknown, | 110 | chalk_ir::TyData::InferenceVar(_iv, _kind) => Ty::Unknown, |
99 | chalk_ir::TyData::Dyn(where_clauses) => { | 111 | chalk_ir::TyData::Dyn(where_clauses) => { |
100 | assert_eq!(where_clauses.bounds.binders.len(&Interner), 1); | 112 | assert_eq!(where_clauses.bounds.binders.len(&Interner), 1); |
101 | let predicates = where_clauses | 113 | let predicates = where_clauses |
@@ -217,13 +229,17 @@ impl ToChalk for TypeCtor { | |||
217 | TypeCtor::Slice => TypeName::Slice, | 229 | TypeCtor::Slice => TypeName::Slice, |
218 | TypeCtor::Ref(mutability) => TypeName::Ref(mutability.to_chalk(db)), | 230 | TypeCtor::Ref(mutability) => TypeName::Ref(mutability.to_chalk(db)), |
219 | TypeCtor::Str => TypeName::Str, | 231 | TypeCtor::Str => TypeName::Str, |
232 | TypeCtor::FnDef(callable_def) => { | ||
233 | let id = callable_def.to_chalk(db); | ||
234 | TypeName::FnDef(id) | ||
235 | } | ||
236 | TypeCtor::Never => TypeName::Never, | ||
237 | |||
220 | TypeCtor::Int(Uncertain::Unknown) | 238 | TypeCtor::Int(Uncertain::Unknown) |
221 | | TypeCtor::Float(Uncertain::Unknown) | 239 | | TypeCtor::Float(Uncertain::Unknown) |
222 | | TypeCtor::Adt(_) | 240 | | TypeCtor::Adt(_) |
223 | | TypeCtor::Array | 241 | | TypeCtor::Array |
224 | | TypeCtor::FnDef(_) | ||
225 | | TypeCtor::FnPtr { .. } | 242 | | TypeCtor::FnPtr { .. } |
226 | | TypeCtor::Never | ||
227 | | TypeCtor::Closure { .. } => { | 243 | | TypeCtor::Closure { .. } => { |
228 | // other TypeCtors get interned and turned into a chalk StructId | 244 | // other TypeCtors get interned and turned into a chalk StructId |
229 | let struct_id = db.intern_type_ctor(self).into(); | 245 | let struct_id = db.intern_type_ctor(self).into(); |
@@ -259,10 +275,14 @@ impl ToChalk for TypeCtor { | |||
259 | TypeName::Slice => TypeCtor::Slice, | 275 | TypeName::Slice => TypeCtor::Slice, |
260 | TypeName::Ref(mutability) => TypeCtor::Ref(from_chalk(db, mutability)), | 276 | TypeName::Ref(mutability) => TypeCtor::Ref(from_chalk(db, mutability)), |
261 | TypeName::Str => TypeCtor::Str, | 277 | TypeName::Str => TypeCtor::Str, |
278 | TypeName::Never => TypeCtor::Never, | ||
262 | 279 | ||
263 | TypeName::FnDef(_) => unreachable!(), | 280 | TypeName::FnDef(fn_def_id) => { |
281 | let callable_def = from_chalk(db, fn_def_id); | ||
282 | TypeCtor::FnDef(callable_def) | ||
283 | } | ||
264 | 284 | ||
265 | TypeName::Error => { | 285 | TypeName::Array | TypeName::Error => { |
266 | // this should not be reached, since we don't represent TypeName::Error with TypeCtor | 286 | // this should not be reached, since we don't represent TypeName::Error with TypeCtor |
267 | unreachable!() | 287 | unreachable!() |
268 | } | 288 | } |
@@ -347,6 +367,18 @@ impl ToChalk for Impl { | |||
347 | } | 367 | } |
348 | } | 368 | } |
349 | 369 | ||
370 | impl ToChalk for CallableDef { | ||
371 | type Chalk = FnDefId; | ||
372 | |||
373 | fn to_chalk(self, db: &dyn HirDatabase) -> FnDefId { | ||
374 | db.intern_callable_def(self).into() | ||
375 | } | ||
376 | |||
377 | fn from_chalk(db: &dyn HirDatabase, fn_def_id: FnDefId) -> CallableDef { | ||
378 | db.lookup_intern_callable_def(fn_def_id.into()) | ||
379 | } | ||
380 | } | ||
381 | |||
350 | impl ToChalk for TypeAliasId { | 382 | impl ToChalk for TypeAliasId { |
351 | type Chalk = AssocTypeId; | 383 | type Chalk = AssocTypeId; |
352 | 384 | ||
@@ -479,7 +511,7 @@ where | |||
479 | 511 | ||
480 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Canonical<T::Chalk> { | 512 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Canonical<T::Chalk> { |
481 | let parameter = chalk_ir::CanonicalVarKind::new( | 513 | let parameter = chalk_ir::CanonicalVarKind::new( |
482 | chalk_ir::VariableKind::Ty, | 514 | chalk_ir::VariableKind::Ty(chalk_ir::TyKind::General), |
483 | chalk_ir::UniverseIndex::ROOT, | 515 | chalk_ir::UniverseIndex::ROOT, |
484 | ); | 516 | ); |
485 | let value = self.value.to_chalk(db); | 517 | let value = self.value.to_chalk(db); |
@@ -550,17 +582,17 @@ impl ToChalk for builtin::BuiltinImplData { | |||
550 | type Chalk = ImplDatum; | 582 | type Chalk = ImplDatum; |
551 | 583 | ||
552 | fn to_chalk(self, db: &dyn HirDatabase) -> ImplDatum { | 584 | fn to_chalk(self, db: &dyn HirDatabase) -> ImplDatum { |
553 | let impl_type = chalk_rust_ir::ImplType::External; | 585 | let impl_type = rust_ir::ImplType::External; |
554 | let where_clauses = self.where_clauses.into_iter().map(|w| w.to_chalk(db)).collect(); | 586 | let where_clauses = self.where_clauses.into_iter().map(|w| w.to_chalk(db)).collect(); |
555 | 587 | ||
556 | let impl_datum_bound = | 588 | let impl_datum_bound = |
557 | chalk_rust_ir::ImplDatumBound { trait_ref: self.trait_ref.to_chalk(db), where_clauses }; | 589 | rust_ir::ImplDatumBound { trait_ref: self.trait_ref.to_chalk(db), where_clauses }; |
558 | let associated_ty_value_ids = | 590 | let associated_ty_value_ids = |
559 | self.assoc_ty_values.into_iter().map(|v| v.to_chalk(db)).collect(); | 591 | self.assoc_ty_values.into_iter().map(|v| v.to_chalk(db)).collect(); |
560 | chalk_rust_ir::ImplDatum { | 592 | rust_ir::ImplDatum { |
561 | binders: make_binders(impl_datum_bound, self.num_vars), | 593 | binders: make_binders(impl_datum_bound, self.num_vars), |
562 | impl_type, | 594 | impl_type, |
563 | polarity: chalk_rust_ir::Polarity::Positive, | 595 | polarity: rust_ir::Polarity::Positive, |
564 | associated_ty_value_ids, | 596 | associated_ty_value_ids, |
565 | } | 597 | } |
566 | } | 598 | } |
@@ -575,9 +607,9 @@ impl ToChalk for builtin::BuiltinImplAssocTyValueData { | |||
575 | 607 | ||
576 | fn to_chalk(self, db: &dyn HirDatabase) -> AssociatedTyValue { | 608 | fn to_chalk(self, db: &dyn HirDatabase) -> AssociatedTyValue { |
577 | let ty = self.value.to_chalk(db); | 609 | let ty = self.value.to_chalk(db); |
578 | let value_bound = chalk_rust_ir::AssociatedTyValueBound { ty }; | 610 | let value_bound = rust_ir::AssociatedTyValueBound { ty }; |
579 | 611 | ||
580 | chalk_rust_ir::AssociatedTyValue { | 612 | rust_ir::AssociatedTyValue { |
581 | associated_ty_id: self.assoc_ty_id.to_chalk(db), | 613 | associated_ty_id: self.assoc_ty_id.to_chalk(db), |
582 | impl_id: self.impl_.to_chalk(db), | 614 | impl_id: self.impl_.to_chalk(db), |
583 | value: make_binders(value_bound, self.num_vars), | 615 | value: make_binders(value_bound, self.num_vars), |
@@ -599,7 +631,7 @@ where | |||
599 | chalk_ir::Binders::new( | 631 | chalk_ir::Binders::new( |
600 | chalk_ir::VariableKinds::from( | 632 | chalk_ir::VariableKinds::from( |
601 | &Interner, | 633 | &Interner, |
602 | std::iter::repeat(chalk_ir::VariableKind::Ty).take(num_vars), | 634 | std::iter::repeat(chalk_ir::VariableKind::Ty(chalk_ir::TyKind::General)).take(num_vars), |
603 | ), | 635 | ), |
604 | value, | 636 | value, |
605 | ) | 637 | ) |
@@ -626,7 +658,7 @@ pub(super) fn generic_predicate_to_inline_bound( | |||
626 | db: &dyn HirDatabase, | 658 | db: &dyn HirDatabase, |
627 | pred: &GenericPredicate, | 659 | pred: &GenericPredicate, |
628 | self_ty: &Ty, | 660 | self_ty: &Ty, |
629 | ) -> Option<chalk_rust_ir::InlineBound<Interner>> { | 661 | ) -> Option<rust_ir::InlineBound<Interner>> { |
630 | // An InlineBound is like a GenericPredicate, except the self type is left out. | 662 | // An InlineBound is like a GenericPredicate, except the self type is left out. |
631 | // We don't have a special type for this, but Chalk does. | 663 | // We don't have a special type for this, but Chalk does. |
632 | match pred { | 664 | match pred { |
@@ -641,8 +673,8 @@ pub(super) fn generic_predicate_to_inline_bound( | |||
641 | .map(|ty| ty.clone().to_chalk(db).cast(&Interner)) | 673 | .map(|ty| ty.clone().to_chalk(db).cast(&Interner)) |
642 | .collect(); | 674 | .collect(); |
643 | let trait_bound = | 675 | let trait_bound = |
644 | chalk_rust_ir::TraitBound { trait_id: trait_ref.trait_.to_chalk(db), args_no_self }; | 676 | rust_ir::TraitBound { trait_id: trait_ref.trait_.to_chalk(db), args_no_self }; |
645 | Some(chalk_rust_ir::InlineBound::TraitBound(trait_bound)) | 677 | Some(rust_ir::InlineBound::TraitBound(trait_bound)) |
646 | } | 678 | } |
647 | GenericPredicate::Projection(proj) => { | 679 | GenericPredicate::Projection(proj) => { |
648 | if &proj.projection_ty.parameters[0] != self_ty { | 680 | if &proj.projection_ty.parameters[0] != self_ty { |
@@ -656,16 +688,13 @@ pub(super) fn generic_predicate_to_inline_bound( | |||
656 | .iter() | 688 | .iter() |
657 | .map(|ty| ty.clone().to_chalk(db).cast(&Interner)) | 689 | .map(|ty| ty.clone().to_chalk(db).cast(&Interner)) |
658 | .collect(); | 690 | .collect(); |
659 | let alias_eq_bound = chalk_rust_ir::AliasEqBound { | 691 | let alias_eq_bound = rust_ir::AliasEqBound { |
660 | value: proj.ty.clone().to_chalk(db), | 692 | value: proj.ty.clone().to_chalk(db), |
661 | trait_bound: chalk_rust_ir::TraitBound { | 693 | trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self }, |
662 | trait_id: trait_.to_chalk(db), | ||
663 | args_no_self, | ||
664 | }, | ||
665 | associated_ty_id: proj.projection_ty.associated_ty.to_chalk(db), | 694 | associated_ty_id: proj.projection_ty.associated_ty.to_chalk(db), |
666 | parameters: Vec::new(), // FIXME we don't support generic associated types yet | 695 | parameters: Vec::new(), // FIXME we don't support generic associated types yet |
667 | }; | 696 | }; |
668 | Some(chalk_rust_ir::InlineBound::AliasEqBound(alias_eq_bound)) | 697 | Some(rust_ir::InlineBound::AliasEqBound(alias_eq_bound)) |
669 | } | 698 | } |
670 | GenericPredicate::Error => None, | 699 | GenericPredicate::Error => None, |
671 | } | 700 | } |
diff --git a/crates/ra_hir_ty/src/traits/chalk/tls.rs b/crates/ra_hir_ty/src/traits/chalk/tls.rs index ebf402a07..d88828c7c 100644 --- a/crates/ra_hir_ty/src/traits/chalk/tls.rs +++ b/crates/ra_hir_ty/src/traits/chalk/tls.rs | |||
@@ -247,10 +247,24 @@ impl DebugContext<'_> { | |||
247 | 247 | ||
248 | pub fn debug_fn_def_id( | 248 | pub fn debug_fn_def_id( |
249 | &self, | 249 | &self, |
250 | _fn_def_id: chalk_ir::FnDefId<Interner>, | 250 | fn_def_id: chalk_ir::FnDefId<Interner>, |
251 | fmt: &mut fmt::Formatter<'_>, | 251 | fmt: &mut fmt::Formatter<'_>, |
252 | ) -> Result<(), fmt::Error> { | 252 | ) -> Result<(), fmt::Error> { |
253 | write!(fmt, "fn") | 253 | let def: CallableDef = from_chalk(self.0, fn_def_id); |
254 | let name = match def { | ||
255 | CallableDef::FunctionId(ff) => self.0.function_data(ff).name.clone(), | ||
256 | CallableDef::StructId(s) => self.0.struct_data(s).name.clone(), | ||
257 | CallableDef::EnumVariantId(e) => { | ||
258 | let enum_data = self.0.enum_data(e.parent); | ||
259 | enum_data.variants[e.local_id].name.clone() | ||
260 | } | ||
261 | }; | ||
262 | match def { | ||
263 | CallableDef::FunctionId(_) => write!(fmt, "{{fn {}}}", name), | ||
264 | CallableDef::StructId(_) | CallableDef::EnumVariantId(_) => { | ||
265 | write!(fmt, "{{ctor {}}}", name) | ||
266 | } | ||
267 | } | ||
254 | } | 268 | } |
255 | 269 | ||
256 | pub fn debug_const( | 270 | pub fn debug_const( |