aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src')
-rw-r--r--crates/hir_ty/src/autoderef.rs3
-rw-r--r--crates/hir_ty/src/db.rs6
-rw-r--r--crates/hir_ty/src/diagnostics/unsafe_check.rs2
-rw-r--r--crates/hir_ty/src/display.rs31
-rw-r--r--crates/hir_ty/src/infer.rs7
-rw-r--r--crates/hir_ty/src/infer/expr.rs14
-rw-r--r--crates/hir_ty/src/lib.rs68
-rw-r--r--crates/hir_ty/src/lower.rs27
-rw-r--r--crates/hir_ty/src/method_resolution.rs15
-rw-r--r--crates/hir_ty/src/traits/chalk.rs21
-rw-r--r--crates/hir_ty/src/traits/chalk/interner.rs2
-rw-r--r--crates/hir_ty/src/traits/chalk/mapping.rs76
-rw-r--r--crates/hir_ty/src/traits/chalk/tls.rs9
13 files changed, 152 insertions, 129 deletions
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
13use crate::{ 13use 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/db.rs b/crates/hir_ty/src/db.rs
index 06714409f..a038674cf 100644
--- a/crates/hir_ty/src/db.rs
+++ b/crates/hir_ty/src/db.rs
@@ -12,7 +12,7 @@ use la_arena::ArenaMap;
12use crate::{ 12use crate::{
13 method_resolution::{InherentImpls, TraitImpls}, 13 method_resolution::{InherentImpls, TraitImpls},
14 traits::chalk, 14 traits::chalk,
15 Binders, CallableDefId, GenericPredicate, InferenceResult, OpaqueTyId, PolyFnSig, 15 Binders, CallableDefId, FnDefId, GenericPredicate, InferenceResult, OpaqueTyId, PolyFnSig,
16 ReturnTypeImplTraits, TraitRef, Ty, TyDefId, ValueTyDefId, 16 ReturnTypeImplTraits, TraitRef, Ty, TyDefId, ValueTyDefId,
17}; 17};
18use hir_expand::name::Name; 18use hir_expand::name::Name;
@@ -100,10 +100,10 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
100 fn impl_datum(&self, krate: CrateId, impl_id: chalk::ImplId) -> Arc<chalk::ImplDatum>; 100 fn impl_datum(&self, krate: CrateId, impl_id: chalk::ImplId) -> Arc<chalk::ImplDatum>;
101 101
102 #[salsa::invoke(crate::traits::chalk::fn_def_datum_query)] 102 #[salsa::invoke(crate::traits::chalk::fn_def_datum_query)]
103 fn fn_def_datum(&self, krate: CrateId, fn_def_id: chalk::FnDefId) -> Arc<chalk::FnDefDatum>; 103 fn fn_def_datum(&self, krate: CrateId, fn_def_id: FnDefId) -> Arc<chalk::FnDefDatum>;
104 104
105 #[salsa::invoke(crate::traits::chalk::fn_def_variance_query)] 105 #[salsa::invoke(crate::traits::chalk::fn_def_variance_query)]
106 fn fn_def_variance(&self, krate: CrateId, fn_def_id: chalk::FnDefId) -> chalk::Variances; 106 fn fn_def_variance(&self, krate: CrateId, fn_def_id: FnDefId) -> chalk::Variances;
107 107
108 #[salsa::invoke(crate::traits::chalk::adt_variance_query)] 108 #[salsa::invoke(crate::traits::chalk::adt_variance_query)]
109 fn adt_variance(&self, krate: CrateId, adt_id: chalk::AdtId) -> chalk::Variances; 109 fn adt_variance(&self, krate: CrateId, adt_id: chalk::AdtId) -> chalk::Variances;
diff --git a/crates/hir_ty/src/diagnostics/unsafe_check.rs b/crates/hir_ty/src/diagnostics/unsafe_check.rs
index e095bee28..20bb64827 100644
--- a/crates/hir_ty/src/diagnostics/unsafe_check.rs
+++ b/crates/hir_ty/src/diagnostics/unsafe_check.rs
@@ -85,7 +85,7 @@ fn walk_unsafe(
85 let expr = &body.exprs[current]; 85 let expr = &body.exprs[current];
86 match expr { 86 match expr {
87 &Expr::Call { callee, .. } => { 87 &Expr::Call { callee, .. } => {
88 if let Some(func) = infer[callee].as_fn_def() { 88 if let Some(func) = infer[callee].as_fn_def(db) {
89 if db.function_data(func).is_unsafe { 89 if db.function_data(func).is_unsafe {
90 unsafe_exprs.push(UnsafeExpr { expr: current, inside_unsafe_block }); 90 unsafe_exprs.push(UnsafeExpr { expr: current, inside_unsafe_block });
91 } 91 }
diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs
index ee15f4f52..b7e85e024 100644
--- a/crates/hir_ty/src/display.rs
+++ b/crates/hir_ty/src/display.rs
@@ -11,7 +11,8 @@ use hir_def::{
11use hir_expand::name::Name; 11use hir_expand::name::Name;
12 12
13use crate::{ 13use crate::{
14 db::HirDatabase, primitive, utils::generics, AdtId, AliasTy, CallableDefId, CallableSig, 14 db::HirDatabase, from_assoc_type_id, from_foreign_def_id, primitive, to_assoc_type_id,
15 traits::chalk::from_chalk, utils::generics, AdtId, AliasTy, CallableDefId, CallableSig,
15 GenericPredicate, Interner, Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, 16 GenericPredicate, Interner, Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar,
16 Substs, TraitRef, Ty, TyKind, 17 Substs, TraitRef, Ty, TyKind,
17}; 18};
@@ -256,7 +257,7 @@ impl HirDisplay for ProjectionTy {
256 f.write_joined(&self.parameters[1..], ", ")?; 257 f.write_joined(&self.parameters[1..], ", ")?;
257 write!(f, ">")?; 258 write!(f, ">")?;
258 } 259 }
259 write!(f, ">::{}", f.db.type_alias_data(self.associated_ty).name)?; 260 write!(f, ">::{}", f.db.type_alias_data(from_assoc_type_id(self.associated_ty)).name)?;
260 Ok(()) 261 Ok(())
261 } 262 }
262} 263}
@@ -363,7 +364,7 @@ impl HirDisplay for Ty {
363 sig.hir_fmt(f)?; 364 sig.hir_fmt(f)?;
364 } 365 }
365 TyKind::FnDef(def, parameters) => { 366 TyKind::FnDef(def, parameters) => {
366 let def = *def; 367 let def = from_chalk(f.db, *def);
367 let sig = f.db.callable_item_signature(def).subst(parameters); 368 let sig = f.db.callable_item_signature(def).subst(parameters);
368 match def { 369 match def {
369 CallableDefId::FunctionId(ff) => { 370 CallableDefId::FunctionId(ff) => {
@@ -431,7 +432,7 @@ impl HirDisplay for Ty {
431 || f.omit_verbose_types() 432 || f.omit_verbose_types()
432 { 433 {
433 match self 434 match self
434 .as_generic_def() 435 .as_generic_def(f.db)
435 .map(|generic_def_id| f.db.generic_defaults(generic_def_id)) 436 .map(|generic_def_id| f.db.generic_defaults(generic_def_id))
436 .filter(|defaults| !defaults.is_empty()) 437 .filter(|defaults| !defaults.is_empty())
437 { 438 {
@@ -467,13 +468,14 @@ impl HirDisplay for Ty {
467 } 468 }
468 } 469 }
469 } 470 }
470 TyKind::AssociatedType(type_alias, parameters) => { 471 TyKind::AssociatedType(assoc_type_id, parameters) => {
472 let type_alias = from_assoc_type_id(*assoc_type_id);
471 let trait_ = match type_alias.lookup(f.db.upcast()).container { 473 let trait_ = match type_alias.lookup(f.db.upcast()).container {
472 AssocContainerId::TraitId(it) => it, 474 AssocContainerId::TraitId(it) => it,
473 _ => panic!("not an associated type"), 475 _ => panic!("not an associated type"),
474 }; 476 };
475 let trait_ = f.db.trait_data(trait_); 477 let trait_ = f.db.trait_data(trait_);
476 let type_alias_data = f.db.type_alias_data(*type_alias); 478 let type_alias_data = f.db.type_alias_data(type_alias);
477 479
478 // Use placeholder associated types when the target is test (https://rust-lang.github.io/chalk/book/clauses/type_equality.html#placeholder-associated-types) 480 // 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() { 481 if f.display_target.is_test() {
@@ -484,14 +486,16 @@ impl HirDisplay for Ty {
484 write!(f, ">")?; 486 write!(f, ">")?;
485 } 487 }
486 } else { 488 } else {
487 let projection_ty = 489 let projection_ty = ProjectionTy {
488 ProjectionTy { associated_ty: *type_alias, parameters: parameters.clone() }; 490 associated_ty: to_assoc_type_id(type_alias),
491 parameters: parameters.clone(),
492 };
489 493
490 projection_ty.hir_fmt(f)?; 494 projection_ty.hir_fmt(f)?;
491 } 495 }
492 } 496 }
493 TyKind::ForeignType(type_alias) => { 497 TyKind::ForeignType(type_alias) => {
494 let type_alias = f.db.type_alias_data(*type_alias); 498 let type_alias = f.db.type_alias_data(from_foreign_def_id(*type_alias));
495 write!(f, "{}", type_alias.name)?; 499 write!(f, "{}", type_alias.name)?;
496 } 500 }
497 TyKind::OpaqueType(opaque_ty_id, parameters) => { 501 TyKind::OpaqueType(opaque_ty_id, parameters) => {
@@ -697,7 +701,9 @@ fn write_bounds_like_dyn_trait(
697 write!(f, "<")?; 701 write!(f, "<")?;
698 angle_open = true; 702 angle_open = true;
699 } 703 }
700 let type_alias = f.db.type_alias_data(projection_pred.projection_ty.associated_ty); 704 let type_alias = f.db.type_alias_data(from_assoc_type_id(
705 projection_pred.projection_ty.associated_ty,
706 ));
701 write!(f, "{} = ", type_alias.name)?; 707 write!(f, "{} = ", type_alias.name)?;
702 projection_pred.ty.hir_fmt(f)?; 708 projection_pred.ty.hir_fmt(f)?;
703 } 709 }
@@ -768,7 +774,10 @@ impl HirDisplay for GenericPredicate {
768 write!( 774 write!(
769 f, 775 f,
770 ">::{} = ", 776 ">::{} = ",
771 f.db.type_alias_data(projection_pred.projection_ty.associated_ty).name, 777 f.db.type_alias_data(from_assoc_type_id(
778 projection_pred.projection_ty.associated_ty
779 ))
780 .name,
772 )?; 781 )?;
773 projection_pred.ty.hir_fmt(f)?; 782 projection_pred.ty.hir_fmt(f)?;
774 } 783 }
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};
43use crate::{ 43use 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
48pub(crate) use unify::unify; 48pub(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..153f22f25 100644
--- a/crates/hir_ty/src/infer/expr.rs
+++ b/crates/hir_ty/src/infer/expr.rs
@@ -18,7 +18,8 @@ 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 traits::{FnTrait, InEnvironment}, 21 to_assoc_type_id,
22 traits::{chalk::from_chalk, 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,
24 Scalar, Substs, TraitRef, Ty, TyKind, 25 Scalar, Substs, TraitRef, Ty, TyKind,
@@ -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 {
@@ -929,8 +932,9 @@ impl<'a> InferenceContext<'a> {
929 } 932 }
930 933
931 fn register_obligations_for_call(&mut self, callable_ty: &Ty) { 934 fn register_obligations_for_call(&mut self, callable_ty: &Ty) {
932 if let TyKind::FnDef(def, parameters) = callable_ty.interned(&Interner) { 935 if let TyKind::FnDef(fn_def, parameters) = callable_ty.interned(&Interner) {
933 let generic_predicates = self.db.generic_predicates((*def).into()); 936 let def: CallableDefId = from_chalk(self.db, *fn_def);
937 let generic_predicates = self.db.generic_predicates(def.into());
934 for predicate in generic_predicates.iter() { 938 for predicate in generic_predicates.iter() {
935 let predicate = predicate.clone().subst(parameters); 939 let predicate = predicate.clone().subst(parameters);
936 if let Some(obligation) = Obligation::from_predicate(predicate) { 940 if let Some(obligation) = Obligation::from_predicate(predicate) {
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs
index 2309db492..6b3485264 100644
--- a/crates/hir_ty/src/lib.rs
+++ b/crates/hir_ty/src/lib.rs
@@ -51,6 +51,10 @@ pub use chalk_ir::{AdtId, BoundVar, DebruijnIndex, Mutability, Scalar, TyVariabl
51 51
52pub use crate::traits::chalk::Interner; 52pub use crate::traits::chalk::Interner;
53 53
54pub type ForeignDefId = chalk_ir::ForeignDefId<Interner>;
55pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>;
56pub(crate) type FnDefId = chalk_ir::FnDefId<Interner>;
57
54#[derive(Clone, PartialEq, Eq, Debug, Hash)] 58#[derive(Clone, PartialEq, Eq, Debug, Hash)]
55pub enum Lifetime { 59pub enum Lifetime {
56 Parameter(LifetimeParamId), 60 Parameter(LifetimeParamId),
@@ -68,7 +72,7 @@ pub struct OpaqueTy {
68/// trait and all its parameters are fully known. 72/// trait and all its parameters are fully known.
69#[derive(Clone, PartialEq, Eq, Debug, Hash)] 73#[derive(Clone, PartialEq, Eq, Debug, Hash)]
70pub struct ProjectionTy { 74pub struct ProjectionTy {
71 pub associated_ty: TypeAliasId, 75 pub associated_ty: AssocTypeId,
72 pub parameters: Substs, 76 pub parameters: Substs,
73} 77}
74 78
@@ -78,7 +82,7 @@ impl ProjectionTy {
78 } 82 }
79 83
80 fn trait_(&self, db: &dyn HirDatabase) -> TraitId { 84 fn trait_(&self, db: &dyn HirDatabase) -> TraitId {
81 match self.associated_ty.lookup(db.upcast()).container { 85 match from_assoc_type_id(self.associated_ty).lookup(db.upcast()).container {
82 AssocContainerId::TraitId(it) => it, 86 AssocContainerId::TraitId(it) => it,
83 _ => panic!("projection ty without parent trait"), 87 _ => panic!("projection ty without parent trait"),
84 } 88 }
@@ -139,7 +143,7 @@ pub enum TyKind {
139 /// when we have tried to normalize a projection like `T::Item` but 143 /// when we have tried to normalize a projection like `T::Item` but
140 /// couldn't find a better representation. In that case, we generate 144 /// couldn't find a better representation. In that case, we generate
141 /// an **application type** like `(Iterator::Item)<T>`. 145 /// an **application type** like `(Iterator::Item)<T>`.
142 AssociatedType(TypeAliasId, Substs), 146 AssociatedType(AssocTypeId, Substs),
143 147
144 /// a scalar type like `bool` or `u32` 148 /// a scalar type like `bool` or `u32`
145 Scalar(Scalar), 149 Scalar(Scalar),
@@ -179,7 +183,7 @@ pub enum TyKind {
179 /// fn foo() -> i32 { 1 } 183 /// fn foo() -> i32 { 1 }
180 /// let bar = foo; // bar: fn() -> i32 {foo} 184 /// let bar = foo; // bar: fn() -> i32 {foo}
181 /// ``` 185 /// ```
182 FnDef(CallableDefId, Substs), 186 FnDef(FnDefId, Substs),
183 187
184 /// The pointee of a string slice. Written as `str`. 188 /// The pointee of a string slice. Written as `str`.
185 Str, 189 Str,
@@ -194,7 +198,7 @@ pub enum TyKind {
194 Closure(DefWithBodyId, ExprId, Substs), 198 Closure(DefWithBodyId, ExprId, Substs),
195 199
196 /// Represents a foreign type declared in external blocks. 200 /// Represents a foreign type declared in external blocks.
197 ForeignType(TypeAliasId), 201 ForeignType(ForeignDefId),
198 202
199 /// A pointer to a function. Written as `fn() -> i32`. 203 /// A pointer to a function. Written as `fn() -> i32`.
200 /// 204 ///
@@ -700,12 +704,14 @@ impl Ty {
700 } 704 }
701 } 705 }
702 706
703 pub fn as_generic_def(&self) -> Option<GenericDefId> { 707 pub fn as_generic_def(&self, db: &dyn HirDatabase) -> Option<GenericDefId> {
704 match *self.interned(&Interner) { 708 match *self.interned(&Interner) {
705 TyKind::Adt(AdtId(adt), ..) => Some(adt.into()), 709 TyKind::Adt(AdtId(adt), ..) => Some(adt.into()),
706 TyKind::FnDef(callable, ..) => Some(callable.into()), 710 TyKind::FnDef(callable, ..) => {
707 TyKind::AssociatedType(type_alias, ..) => Some(type_alias.into()), 711 Some(db.lookup_intern_callable_def(callable.into()).into())
708 TyKind::ForeignType(type_alias, ..) => Some(type_alias.into()), 712 }
713 TyKind::AssociatedType(type_alias, ..) => Some(from_assoc_type_id(type_alias).into()),
714 TyKind::ForeignType(type_alias, ..) => Some(from_foreign_def_id(type_alias).into()),
709 _ => None, 715 _ => None,
710 } 716 }
711 } 717 }
@@ -724,8 +730,10 @@ impl Ty {
724 (TyKind::Slice(_), TyKind::Slice(_)) | (TyKind::Array(_), TyKind::Array(_)) => true, 730 (TyKind::Slice(_), TyKind::Slice(_)) | (TyKind::Array(_), TyKind::Array(_)) => true,
725 (TyKind::FnDef(def_id, ..), TyKind::FnDef(def_id2, ..)) => def_id == def_id2, 731 (TyKind::FnDef(def_id, ..), TyKind::FnDef(def_id2, ..)) => def_id == def_id2,
726 (TyKind::OpaqueType(ty_id, ..), TyKind::OpaqueType(ty_id2, ..)) => ty_id == ty_id2, 732 (TyKind::OpaqueType(ty_id, ..), TyKind::OpaqueType(ty_id2, ..)) => ty_id == ty_id2,
727 (TyKind::AssociatedType(ty_id, ..), TyKind::AssociatedType(ty_id2, ..)) 733 (TyKind::AssociatedType(ty_id, ..), TyKind::AssociatedType(ty_id2, ..)) => {
728 | (TyKind::ForeignType(ty_id, ..), TyKind::ForeignType(ty_id2, ..)) => ty_id == ty_id2, 734 ty_id == ty_id2
735 }
736 (TyKind::ForeignType(ty_id, ..), TyKind::ForeignType(ty_id2, ..)) => ty_id == ty_id2,
729 (TyKind::Closure(def, expr, _), TyKind::Closure(def2, expr2, _)) => { 737 (TyKind::Closure(def, expr, _), TyKind::Closure(def2, expr2, _)) => {
730 expr == expr2 && def == def2 738 expr == expr2 && def == def2
731 } 739 }
@@ -770,18 +778,27 @@ impl Ty {
770 } 778 }
771 } 779 }
772 780
773 pub fn as_fn_def(&self) -> Option<FunctionId> { 781 pub fn callable_def(&self, db: &dyn HirDatabase) -> Option<CallableDefId> {
774 match self.interned(&Interner) { 782 match self.interned(&Interner) {
775 &TyKind::FnDef(CallableDefId::FunctionId(func), ..) => Some(func), 783 &TyKind::FnDef(def, ..) => Some(db.lookup_intern_callable_def(def.into())),
776 _ => None, 784 _ => None,
777 } 785 }
778 } 786 }
779 787
788 pub fn as_fn_def(&self, db: &dyn HirDatabase) -> Option<FunctionId> {
789 if let Some(CallableDefId::FunctionId(func)) = self.callable_def(db) {
790 Some(func)
791 } else {
792 None
793 }
794 }
795
780 pub fn callable_sig(&self, db: &dyn HirDatabase) -> Option<CallableSig> { 796 pub fn callable_sig(&self, db: &dyn HirDatabase) -> Option<CallableSig> {
781 match self.interned(&Interner) { 797 match self.interned(&Interner) {
782 TyKind::Function(fn_ptr) => Some(CallableSig::from_fn_ptr(fn_ptr)), 798 TyKind::Function(fn_ptr) => Some(CallableSig::from_fn_ptr(fn_ptr)),
783 TyKind::FnDef(def, parameters) => { 799 TyKind::FnDef(def, parameters) => {
784 let sig = db.callable_item_signature(*def); 800 let callable_def = db.lookup_intern_callable_def((*def).into());
801 let sig = db.callable_item_signature(callable_def);
785 Some(sig.subst(&parameters)) 802 Some(sig.subst(&parameters))
786 } 803 }
787 TyKind::Closure(.., substs) => { 804 TyKind::Closure(.., substs) => {
@@ -916,14 +933,15 @@ impl Ty {
916 933
917 pub fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<TraitId> { 934 pub fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<TraitId> {
918 match self.interned(&Interner) { 935 match self.interned(&Interner) {
919 TyKind::AssociatedType(type_alias_id, ..) => { 936 TyKind::AssociatedType(id, ..) => {
920 match type_alias_id.lookup(db.upcast()).container { 937 match from_assoc_type_id(*id).lookup(db.upcast()).container {
921 AssocContainerId::TraitId(trait_id) => Some(trait_id), 938 AssocContainerId::TraitId(trait_id) => Some(trait_id),
922 _ => None, 939 _ => None,
923 } 940 }
924 } 941 }
925 TyKind::Alias(AliasTy::Projection(projection_ty)) => { 942 TyKind::Alias(AliasTy::Projection(projection_ty)) => {
926 match projection_ty.associated_ty.lookup(db.upcast()).container { 943 match from_assoc_type_id(projection_ty.associated_ty).lookup(db.upcast()).container
944 {
927 AssocContainerId::TraitId(trait_id) => Some(trait_id), 945 AssocContainerId::TraitId(trait_id) => Some(trait_id),
928 _ => None, 946 _ => None,
929 } 947 }
@@ -1116,3 +1134,19 @@ pub struct ReturnTypeImplTraits {
1116pub(crate) struct ReturnTypeImplTrait { 1134pub(crate) struct ReturnTypeImplTrait {
1117 pub(crate) bounds: Binders<Vec<GenericPredicate>>, 1135 pub(crate) bounds: Binders<Vec<GenericPredicate>>,
1118} 1136}
1137
1138pub fn to_foreign_def_id(id: TypeAliasId) -> ForeignDefId {
1139 chalk_ir::ForeignDefId(salsa::InternKey::as_intern_id(&id))
1140}
1141
1142pub fn from_foreign_def_id(id: ForeignDefId) -> TypeAliasId {
1143 salsa::InternKey::from_intern_id(id.0)
1144}
1145
1146pub fn to_assoc_type_id(id: TypeAliasId) -> AssocTypeId {
1147 chalk_ir::AssocTypeId(salsa::InternKey::as_intern_id(&id))
1148}
1149
1150pub fn from_assoc_type_id(id: AssocTypeId) -> TypeAliasId {
1151 salsa::InternKey::from_intern_id(id.0)
1152}
diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs
index 9fe7e3dce..b8b1400eb 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
28use crate::{ 28use 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 );
@@ -1060,7 +1064,10 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig {
1060fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> { 1064fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> {
1061 let generics = generics(db.upcast(), def.into()); 1065 let generics = generics(db.upcast(), def.into());
1062 let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); 1066 let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST);
1063 Binders::new(substs.len(), TyKind::FnDef(def.into(), substs).intern(&Interner)) 1067 Binders::new(
1068 substs.len(),
1069 TyKind::FnDef(CallableDefId::FunctionId(def).to_chalk(db), substs).intern(&Interner),
1070 )
1064} 1071}
1065 1072
1066/// Build the declared type of a const. 1073/// Build the declared type of a const.
@@ -1103,7 +1110,10 @@ fn type_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> Binders<T
1103 } 1110 }
1104 let generics = generics(db.upcast(), def.into()); 1111 let generics = generics(db.upcast(), def.into());
1105 let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); 1112 let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST);
1106 Binders::new(substs.len(), TyKind::FnDef(def.into(), substs).intern(&Interner)) 1113 Binders::new(
1114 substs.len(),
1115 TyKind::FnDef(CallableDefId::StructId(def).to_chalk(db), substs).intern(&Interner),
1116 )
1107} 1117}
1108 1118
1109fn fn_sig_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -> PolyFnSig { 1119fn fn_sig_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -> PolyFnSig {
@@ -1128,7 +1138,10 @@ fn type_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -
1128 } 1138 }
1129 let generics = generics(db.upcast(), def.parent.into()); 1139 let generics = generics(db.upcast(), def.parent.into());
1130 let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); 1140 let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST);
1131 Binders::new(substs.len(), TyKind::FnDef(def.into(), substs).intern(&Interner)) 1141 Binders::new(
1142 substs.len(),
1143 TyKind::FnDef(CallableDefId::EnumVariantId(def).to_chalk(db), substs).intern(&Interner),
1144 )
1132} 1145}
1133 1146
1134fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> { 1147fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> {
@@ -1143,7 +1156,7 @@ fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> {
1143 let ctx = 1156 let ctx =
1144 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); 1157 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable);
1145 if db.type_alias_data(t).is_extern { 1158 if db.type_alias_data(t).is_extern {
1146 Binders::new(0, TyKind::ForeignType(t).intern(&Interner)) 1159 Binders::new(0, TyKind::ForeignType(crate::to_foreign_def_id(t)).intern(&Interner))
1147 } else { 1160 } else {
1148 let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); 1161 let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST);
1149 let type_ref = &db.type_alias_data(t).type_ref; 1162 let type_ref = &db.type_alias_data(t).type_ref;
diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs
index f9877e760..c7055bee5 100644
--- a/crates/hir_ty/src/method_resolution.rs
+++ b/crates/hir_ty/src/method_resolution.rs
@@ -9,7 +9,7 @@ use base_db::CrateId;
9use chalk_ir::Mutability; 9use chalk_ir::Mutability;
10use hir_def::{ 10use hir_def::{
11 lang_item::LangItemTarget, AssocContainerId, AssocItemId, FunctionId, GenericDefId, HasModule, 11 lang_item::LangItemTarget, AssocContainerId, AssocItemId, FunctionId, GenericDefId, HasModule,
12 ImplId, Lookup, ModuleId, TraitId, TypeAliasId, 12 ImplId, Lookup, ModuleId, TraitId,
13}; 13};
14use hir_expand::name::Name; 14use hir_expand::name::Name;
15use rustc_hash::{FxHashMap, FxHashSet}; 15use rustc_hash::{FxHashMap, FxHashSet};
@@ -17,10 +17,11 @@ use rustc_hash::{FxHashMap, FxHashSet};
17use crate::{ 17use crate::{
18 autoderef, 18 autoderef,
19 db::HirDatabase, 19 db::HirDatabase,
20 from_foreign_def_id,
20 primitive::{self, FloatTy, IntTy, UintTy}, 21 primitive::{self, FloatTy, IntTy, UintTy},
21 utils::all_super_traits, 22 utils::all_super_traits,
22 AdtId, Canonical, DebruijnIndex, FnPointer, FnSig, InEnvironment, Interner, Scalar, Substs, 23 AdtId, Canonical, DebruijnIndex, FnPointer, FnSig, ForeignDefId, InEnvironment, Interner,
23 TraitEnvironment, TraitRef, Ty, TyKind, TypeWalk, 24 Scalar, Substs, TraitEnvironment, TraitRef, Ty, TyKind, TypeWalk,
24}; 25};
25 26
26/// This is used as a key for indexing impls. 27/// This is used as a key for indexing impls.
@@ -35,7 +36,7 @@ pub enum TyFingerprint {
35 Adt(hir_def::AdtId), 36 Adt(hir_def::AdtId),
36 Dyn(TraitId), 37 Dyn(TraitId),
37 Tuple(usize), 38 Tuple(usize),
38 ForeignType(TypeAliasId), 39 ForeignType(ForeignDefId),
39 FnPtr(usize, FnSig), 40 FnPtr(usize, FnSig),
40} 41}
41 42
@@ -236,8 +237,10 @@ impl Ty {
236 TyKind::Adt(AdtId(def_id), _) => { 237 TyKind::Adt(AdtId(def_id), _) => {
237 return mod_to_crate_ids(def_id.module(db.upcast())); 238 return mod_to_crate_ids(def_id.module(db.upcast()));
238 } 239 }
239 TyKind::ForeignType(type_alias_id) => { 240 TyKind::ForeignType(id) => {
240 return mod_to_crate_ids(type_alias_id.lookup(db.upcast()).module(db.upcast())); 241 return mod_to_crate_ids(
242 from_foreign_def_id(*id).lookup(db.upcast()).module(db.upcast()),
243 );
241 } 244 }
242 TyKind::Scalar(Scalar::Bool) => lang_item_crate!("bool"), 245 TyKind::Scalar(Scalar::Bool) => lang_item_crate!("bool"),
243 TyKind::Scalar(Scalar::Char) => lang_item_crate!("char"), 246 TyKind::Scalar(Scalar::Char) => lang_item_crate!("char"),
diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs
index 55181cc49..bb92d8e2a 100644
--- a/crates/hir_ty/src/traits/chalk.rs
+++ b/crates/hir_ty/src/traits/chalk.rs
@@ -17,14 +17,15 @@ use super::ChalkContext;
17use crate::{ 17use 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, FnDefId, GenericPredicate,
23 ProjectionTy, Substs, TraitRef, Ty, TyKind, 25 ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TyKind,
24}; 26};
25use mapping::{ 27use 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
30pub use self::interner::Interner; 31pub 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/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
13pub(crate) type AssocTypeId = chalk_ir::AssocTypeId<Interner>; 13pub(crate) type AssocTypeId = chalk_ir::AssocTypeId<Interner>;
14pub(crate) type AssociatedTyDatum = chalk_solve::rust_ir::AssociatedTyDatum<Interner>; 14pub(crate) type AssociatedTyDatum = chalk_solve::rust_ir::AssociatedTyDatum<Interner>;
15pub(crate) type ForeignDefId = chalk_ir::ForeignDefId<Interner>;
16pub(crate) type TraitId = chalk_ir::TraitId<Interner>; 15pub(crate) type TraitId = chalk_ir::TraitId<Interner>;
17pub(crate) type TraitDatum = chalk_solve::rust_ir::TraitDatum<Interner>; 16pub(crate) type TraitDatum = chalk_solve::rust_ir::TraitDatum<Interner>;
18pub(crate) type AdtId = chalk_ir::AdtId<Interner>; 17pub(crate) type AdtId = chalk_ir::AdtId<Interner>;
@@ -21,7 +20,6 @@ pub(crate) type ImplId = chalk_ir::ImplId<Interner>;
21pub(crate) type ImplDatum = chalk_solve::rust_ir::ImplDatum<Interner>; 20pub(crate) type ImplDatum = chalk_solve::rust_ir::ImplDatum<Interner>;
22pub(crate) type AssociatedTyValueId = chalk_solve::rust_ir::AssociatedTyValueId<Interner>; 21pub(crate) type AssociatedTyValueId = chalk_solve::rust_ir::AssociatedTyValueId<Interner>;
23pub(crate) type AssociatedTyValue = chalk_solve::rust_ir::AssociatedTyValue<Interner>; 22pub(crate) type AssociatedTyValue = chalk_solve::rust_ir::AssociatedTyValue<Interner>;
24pub(crate) type FnDefId = chalk_ir::FnDefId<Interner>;
25pub(crate) type FnDefDatum = chalk_solve::rust_ir::FnDefDatum<Interner>; 23pub(crate) type FnDefDatum = chalk_solve::rust_ir::FnDefDatum<Interner>;
26pub(crate) type OpaqueTyId = chalk_ir::OpaqueTyId<Interner>; 24pub(crate) type OpaqueTyId = chalk_ir::OpaqueTyId<Interner>;
27pub(crate) type OpaqueTyDatum = chalk_solve::rust_ir::OpaqueTyDatum<Interner>; 25pub(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 44cfb9359..23ef07d77 100644
--- a/crates/hir_ty/src/traits/chalk/mapping.rs
+++ b/crates/hir_ty/src/traits/chalk/mapping.rs
@@ -14,6 +14,7 @@ use hir_def::{AssocContainerId, GenericDefId, Lookup, TypeAliasId};
14 14
15use crate::{ 15use crate::{
16 db::HirDatabase, 16 db::HirDatabase,
17 from_assoc_type_id,
17 primitive::UintTy, 18 primitive::UintTy,
18 traits::{Canonical, Obligation}, 19 traits::{Canonical, Obligation},
19 AliasTy, CallableDefId, FnPointer, FnSig, GenericPredicate, InEnvironment, OpaqueTy, 20 AliasTy, CallableDefId, FnPointer, FnSig, GenericPredicate, InEnvironment, OpaqueTy,
@@ -38,9 +39,7 @@ impl ToChalk for Ty {
38 }) 39 })
39 .intern(&Interner) 40 .intern(&Interner)
40 } 41 }
41 TyKind::AssociatedType(type_alias, substs) => { 42 TyKind::AssociatedType(assoc_type_id, substs) => {
42 let assoc_type = TypeAliasAsAssocType(type_alias);
43 let assoc_type_id = assoc_type.to_chalk(db);
44 let substitution = substs.to_chalk(db); 43 let substitution = substs.to_chalk(db);
45 chalk_ir::TyKind::AssociatedType(assoc_type_id, substitution).intern(&Interner) 44 chalk_ir::TyKind::AssociatedType(assoc_type_id, substitution).intern(&Interner)
46 } 45 }
@@ -51,11 +50,7 @@ impl ToChalk for Ty {
51 chalk_ir::TyKind::OpaqueType(id, substitution).intern(&Interner) 50 chalk_ir::TyKind::OpaqueType(id, substitution).intern(&Interner)
52 } 51 }
53 52
54 TyKind::ForeignType(type_alias) => { 53 TyKind::ForeignType(id) => chalk_ir::TyKind::Foreign(id).intern(&Interner),
55 let foreign_type = TypeAliasAsForeignType(type_alias);
56 let foreign_type_id = foreign_type.to_chalk(db);
57 chalk_ir::TyKind::Foreign(foreign_type_id).intern(&Interner)
58 }
59 54
60 TyKind::Scalar(scalar) => chalk_ir::TyKind::Scalar(scalar).intern(&Interner), 55 TyKind::Scalar(scalar) => chalk_ir::TyKind::Scalar(scalar).intern(&Interner),
61 56
@@ -71,8 +66,7 @@ impl ToChalk for Ty {
71 chalk_ir::TyKind::Slice(substs[0].clone().to_chalk(db)).intern(&Interner) 66 chalk_ir::TyKind::Slice(substs[0].clone().to_chalk(db)).intern(&Interner)
72 } 67 }
73 TyKind::Str => chalk_ir::TyKind::Str.intern(&Interner), 68 TyKind::Str => chalk_ir::TyKind::Str.intern(&Interner),
74 TyKind::FnDef(callable_def, substs) => { 69 TyKind::FnDef(id, substs) => {
75 let id = callable_def.to_chalk(db);
76 let substitution = substs.to_chalk(db); 70 let substitution = substs.to_chalk(db);
77 chalk_ir::TyKind::FnDef(id, substitution).intern(&Interner) 71 chalk_ir::TyKind::FnDef(id, substitution).intern(&Interner)
78 } 72 }
@@ -89,7 +83,7 @@ impl ToChalk for Ty {
89 chalk_ir::TyKind::Adt(adt_id, substitution).intern(&Interner) 83 chalk_ir::TyKind::Adt(adt_id, substitution).intern(&Interner)
90 } 84 }
91 TyKind::Alias(AliasTy::Projection(proj_ty)) => { 85 TyKind::Alias(AliasTy::Projection(proj_ty)) => {
92 let associated_ty_id = TypeAliasAsAssocType(proj_ty.associated_ty).to_chalk(db); 86 let associated_ty_id = proj_ty.associated_ty;
93 let substitution = proj_ty.parameters.to_chalk(db); 87 let substitution = proj_ty.parameters.to_chalk(db);
94 chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy { 88 chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy {
95 associated_ty_id, 89 associated_ty_id,
@@ -143,8 +137,7 @@ impl ToChalk for Ty {
143 TyKind::Placeholder(db.lookup_intern_type_param_id(interned_id)) 137 TyKind::Placeholder(db.lookup_intern_type_param_id(interned_id))
144 } 138 }
145 chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => { 139 chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => {
146 let associated_ty = 140 let associated_ty = proj.associated_ty_id;
147 from_chalk::<TypeAliasAsAssocType, _>(db, proj.associated_ty_id).0;
148 let parameters = from_chalk(db, proj.substitution); 141 let parameters = from_chalk(db, proj.substitution);
149 TyKind::Alias(AliasTy::Projection(ProjectionTy { associated_ty, parameters })) 142 TyKind::Alias(AliasTy::Projection(ProjectionTy { associated_ty, parameters }))
150 } 143 }
@@ -184,10 +177,9 @@ impl ToChalk for Ty {
184 } 177 }
185 178
186 chalk_ir::TyKind::Adt(adt_id, subst) => TyKind::Adt(adt_id, from_chalk(db, subst)), 179 chalk_ir::TyKind::Adt(adt_id, subst) => TyKind::Adt(adt_id, from_chalk(db, subst)),
187 chalk_ir::TyKind::AssociatedType(type_id, subst) => TyKind::AssociatedType( 180 chalk_ir::TyKind::AssociatedType(type_id, subst) => {
188 from_chalk::<TypeAliasAsAssocType, _>(db, type_id).0, 181 TyKind::AssociatedType(type_id, from_chalk(db, subst))
189 from_chalk(db, subst), 182 }
190 ),
191 183
192 chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => { 184 chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => {
193 TyKind::OpaqueType(from_chalk(db, opaque_type_id), from_chalk(db, subst)) 185 TyKind::OpaqueType(from_chalk(db, opaque_type_id), from_chalk(db, subst))
@@ -208,7 +200,7 @@ impl ToChalk for Ty {
208 chalk_ir::TyKind::Never => TyKind::Never, 200 chalk_ir::TyKind::Never => TyKind::Never,
209 201
210 chalk_ir::TyKind::FnDef(fn_def_id, subst) => { 202 chalk_ir::TyKind::FnDef(fn_def_id, subst) => {
211 TyKind::FnDef(from_chalk(db, fn_def_id), from_chalk(db, subst)) 203 TyKind::FnDef(fn_def_id, from_chalk(db, subst))
212 } 204 }
213 205
214 chalk_ir::TyKind::Closure(id, subst) => { 206 chalk_ir::TyKind::Closure(id, subst) => {
@@ -217,9 +209,7 @@ impl ToChalk for Ty {
217 TyKind::Closure(def, expr, from_chalk(db, subst)) 209 TyKind::Closure(def, expr, from_chalk(db, subst))
218 } 210 }
219 211
220 chalk_ir::TyKind::Foreign(foreign_def_id) => { 212 chalk_ir::TyKind::Foreign(foreign_def_id) => TyKind::ForeignType(foreign_def_id),
221 TyKind::ForeignType(from_chalk::<TypeAliasAsForeignType, _>(db, foreign_def_id).0)
222 }
223 chalk_ir::TyKind::Generator(_, _) => unimplemented!(), // FIXME 213 chalk_ir::TyKind::Generator(_, _) => unimplemented!(), // FIXME
224 chalk_ir::TyKind::GeneratorWitness(_, _) => unimplemented!(), // FIXME 214 chalk_ir::TyKind::GeneratorWitness(_, _) => unimplemented!(), // FIXME
225 } 215 }
@@ -338,34 +328,6 @@ impl ToChalk for CallableDefId {
338 } 328 }
339} 329}
340 330
341pub(crate) struct TypeAliasAsAssocType(pub(crate) TypeAliasId);
342
343impl ToChalk for TypeAliasAsAssocType {
344 type Chalk = AssocTypeId;
345
346 fn to_chalk(self, _db: &dyn HirDatabase) -> AssocTypeId {
347 chalk_ir::AssocTypeId(self.0.as_intern_id())
348 }
349
350 fn from_chalk(_db: &dyn HirDatabase, assoc_type_id: AssocTypeId) -> TypeAliasAsAssocType {
351 TypeAliasAsAssocType(InternKey::from_intern_id(assoc_type_id.0))
352 }
353}
354
355pub(crate) struct TypeAliasAsForeignType(pub(crate) TypeAliasId);
356
357impl ToChalk for TypeAliasAsForeignType {
358 type Chalk = ForeignDefId;
359
360 fn to_chalk(self, _db: &dyn HirDatabase) -> ForeignDefId {
361 chalk_ir::ForeignDefId(self.0.as_intern_id())
362 }
363
364 fn from_chalk(_db: &dyn HirDatabase, foreign_def_id: ForeignDefId) -> TypeAliasAsForeignType {
365 TypeAliasAsForeignType(InternKey::from_intern_id(foreign_def_id.0))
366 }
367}
368
369pub(crate) struct TypeAliasAsValue(pub(crate) TypeAliasId); 331pub(crate) struct TypeAliasAsValue(pub(crate) TypeAliasId);
370 332
371impl ToChalk for TypeAliasAsValue { 333impl ToChalk for TypeAliasAsValue {
@@ -447,7 +409,7 @@ impl ToChalk for ProjectionTy {
447 409
448 fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::ProjectionTy<Interner> { 410 fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::ProjectionTy<Interner> {
449 chalk_ir::ProjectionTy { 411 chalk_ir::ProjectionTy {
450 associated_ty_id: TypeAliasAsAssocType(self.associated_ty).to_chalk(db), 412 associated_ty_id: self.associated_ty,
451 substitution: self.parameters.to_chalk(db), 413 substitution: self.parameters.to_chalk(db),
452 } 414 }
453 } 415 }
@@ -457,11 +419,7 @@ impl ToChalk for ProjectionTy {
457 projection_ty: chalk_ir::ProjectionTy<Interner>, 419 projection_ty: chalk_ir::ProjectionTy<Interner>,
458 ) -> ProjectionTy { 420 ) -> ProjectionTy {
459 ProjectionTy { 421 ProjectionTy {
460 associated_ty: from_chalk::<TypeAliasAsAssocType, _>( 422 associated_ty: projection_ty.associated_ty_id,
461 db,
462 projection_ty.associated_ty_id,
463 )
464 .0,
465 parameters: from_chalk(db, projection_ty.substitution), 423 parameters: from_chalk(db, projection_ty.substitution),
466 } 424 }
467 } 425 }
@@ -615,7 +573,10 @@ pub(super) fn generic_predicate_to_inline_bound(
615 if &proj.projection_ty.parameters[0] != self_ty { 573 if &proj.projection_ty.parameters[0] != self_ty {
616 return None; 574 return None;
617 } 575 }
618 let trait_ = match proj.projection_ty.associated_ty.lookup(db.upcast()).container { 576 let trait_ = match from_assoc_type_id(proj.projection_ty.associated_ty)
577 .lookup(db.upcast())
578 .container
579 {
619 AssocContainerId::TraitId(t) => t, 580 AssocContainerId::TraitId(t) => t,
620 _ => panic!("associated type not in trait"), 581 _ => panic!("associated type not in trait"),
621 }; 582 };
@@ -626,8 +587,7 @@ pub(super) fn generic_predicate_to_inline_bound(
626 let alias_eq_bound = rust_ir::AliasEqBound { 587 let alias_eq_bound = rust_ir::AliasEqBound {
627 value: proj.ty.clone().to_chalk(db), 588 value: proj.ty.clone().to_chalk(db),
628 trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self }, 589 trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self },
629 associated_ty_id: TypeAliasAsAssocType(proj.projection_ty.associated_ty) 590 associated_ty_id: proj.projection_ty.associated_ty,
630 .to_chalk(db),
631 parameters: Vec::new(), // FIXME we don't support generic associated types yet 591 parameters: Vec::new(), // FIXME we don't support generic associated types yet
632 }; 592 };
633 Some(rust_ir::InlineBound::AliasEqBound(alias_eq_bound)) 593 Some(rust_ir::InlineBound::AliasEqBound(alias_eq_bound))
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;
4use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplication}; 4use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplication};
5use itertools::Itertools; 5use itertools::Itertools;
6 6
7use super::{from_chalk, Interner, TypeAliasAsAssocType}; 7use super::{from_chalk, Interner};
8use crate::{db::HirDatabase, CallableDefId}; 8use crate::{db::HirDatabase, from_assoc_type_id, CallableDefId};
9use hir_def::{AdtId, AssocContainerId, Lookup, TypeAliasId}; 9use hir_def::{AdtId, AssocContainerId, Lookup, TypeAliasId};
10 10
11pub(crate) use unsafe_tls::{set_current_program, with_current_program}; 11pub(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,