1 //! This module contains the implementations of the `ToChalk` trait, which
2 //! handles conversion between our data types and their corresponding types in
3 //! Chalk (in both directions); plus some helper functions for more specialized
4 //! conversions.
5
6 use chalk_solve::rust_ir;
7
8 use base_db::salsa::{self, InternKey};
9 use hir_def::{LifetimeParamId, TraitId, TypeAliasId, TypeOrConstParamId};
10
11 use crate::{
12 chalk_db, db::HirDatabase, AssocTypeId, CallableDefId, ChalkTraitId, FnDefId, ForeignDefId,
13 Interner, OpaqueTyId, PlaceholderIndex,
14 };
15
16 pub(crate) trait ToChalk {
17 type Chalk;
to_chalk(self, db: &dyn HirDatabase) -> Self::Chalk18 fn to_chalk(self, db: &dyn HirDatabase) -> Self::Chalk;
from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self19 fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self;
20 }
21
from_chalk<T, ChalkT>(db: &dyn HirDatabase, chalk: ChalkT) -> T where T: ToChalk<Chalk = ChalkT>,22 pub(crate) fn from_chalk<T, ChalkT>(db: &dyn HirDatabase, chalk: ChalkT) -> T
23 where
24 T: ToChalk<Chalk = ChalkT>,
25 {
26 T::from_chalk(db, chalk)
27 }
28
29 impl ToChalk for hir_def::ImplId {
30 type Chalk = chalk_db::ImplId;
31
to_chalk(self, _db: &dyn HirDatabase) -> chalk_db::ImplId32 fn to_chalk(self, _db: &dyn HirDatabase) -> chalk_db::ImplId {
33 chalk_ir::ImplId(self.as_intern_id())
34 }
35
from_chalk(_db: &dyn HirDatabase, impl_id: chalk_db::ImplId) -> hir_def::ImplId36 fn from_chalk(_db: &dyn HirDatabase, impl_id: chalk_db::ImplId) -> hir_def::ImplId {
37 InternKey::from_intern_id(impl_id.0)
38 }
39 }
40
41 impl ToChalk for CallableDefId {
42 type Chalk = FnDefId;
43
to_chalk(self, db: &dyn HirDatabase) -> FnDefId44 fn to_chalk(self, db: &dyn HirDatabase) -> FnDefId {
45 db.intern_callable_def(self).into()
46 }
47
from_chalk(db: &dyn HirDatabase, fn_def_id: FnDefId) -> CallableDefId48 fn from_chalk(db: &dyn HirDatabase, fn_def_id: FnDefId) -> CallableDefId {
49 db.lookup_intern_callable_def(fn_def_id.into())
50 }
51 }
52
53 pub(crate) struct TypeAliasAsValue(pub(crate) TypeAliasId);
54
55 impl ToChalk for TypeAliasAsValue {
56 type Chalk = chalk_db::AssociatedTyValueId;
57
to_chalk(self, _db: &dyn HirDatabase) -> chalk_db::AssociatedTyValueId58 fn to_chalk(self, _db: &dyn HirDatabase) -> chalk_db::AssociatedTyValueId {
59 rust_ir::AssociatedTyValueId(self.0.as_intern_id())
60 }
61
from_chalk( _db: &dyn HirDatabase, assoc_ty_value_id: chalk_db::AssociatedTyValueId, ) -> TypeAliasAsValue62 fn from_chalk(
63 _db: &dyn HirDatabase,
64 assoc_ty_value_id: chalk_db::AssociatedTyValueId,
65 ) -> TypeAliasAsValue {
66 TypeAliasAsValue(TypeAliasId::from_intern_id(assoc_ty_value_id.0))
67 }
68 }
69
70 impl From<FnDefId> for crate::db::InternedCallableDefId {
from(fn_def_id: FnDefId) -> Self71 fn from(fn_def_id: FnDefId) -> Self {
72 InternKey::from_intern_id(fn_def_id.0)
73 }
74 }
75
76 impl From<crate::db::InternedCallableDefId> for FnDefId {
from(callable_def_id: crate::db::InternedCallableDefId) -> Self77 fn from(callable_def_id: crate::db::InternedCallableDefId) -> Self {
78 chalk_ir::FnDefId(callable_def_id.as_intern_id())
79 }
80 }
81
82 impl From<OpaqueTyId> for crate::db::InternedOpaqueTyId {
from(id: OpaqueTyId) -> Self83 fn from(id: OpaqueTyId) -> Self {
84 InternKey::from_intern_id(id.0)
85 }
86 }
87
88 impl From<crate::db::InternedOpaqueTyId> for OpaqueTyId {
from(id: crate::db::InternedOpaqueTyId) -> Self89 fn from(id: crate::db::InternedOpaqueTyId) -> Self {
90 chalk_ir::OpaqueTyId(id.as_intern_id())
91 }
92 }
93
94 impl From<chalk_ir::ClosureId<Interner>> for crate::db::InternedClosureId {
from(id: chalk_ir::ClosureId<Interner>) -> Self95 fn from(id: chalk_ir::ClosureId<Interner>) -> Self {
96 Self::from_intern_id(id.0)
97 }
98 }
99
100 impl From<crate::db::InternedClosureId> for chalk_ir::ClosureId<Interner> {
from(id: crate::db::InternedClosureId) -> Self101 fn from(id: crate::db::InternedClosureId) -> Self {
102 chalk_ir::ClosureId(id.as_intern_id())
103 }
104 }
105
106 impl From<chalk_ir::GeneratorId<Interner>> for crate::db::InternedGeneratorId {
from(id: chalk_ir::GeneratorId<Interner>) -> Self107 fn from(id: chalk_ir::GeneratorId<Interner>) -> Self {
108 Self::from_intern_id(id.0)
109 }
110 }
111
112 impl From<crate::db::InternedGeneratorId> for chalk_ir::GeneratorId<Interner> {
from(id: crate::db::InternedGeneratorId) -> Self113 fn from(id: crate::db::InternedGeneratorId) -> Self {
114 chalk_ir::GeneratorId(id.as_intern_id())
115 }
116 }
117
to_foreign_def_id(id: TypeAliasId) -> ForeignDefId118 pub fn to_foreign_def_id(id: TypeAliasId) -> ForeignDefId {
119 chalk_ir::ForeignDefId(salsa::InternKey::as_intern_id(&id))
120 }
121
from_foreign_def_id(id: ForeignDefId) -> TypeAliasId122 pub fn from_foreign_def_id(id: ForeignDefId) -> TypeAliasId {
123 salsa::InternKey::from_intern_id(id.0)
124 }
125
to_assoc_type_id(id: TypeAliasId) -> AssocTypeId126 pub fn to_assoc_type_id(id: TypeAliasId) -> AssocTypeId {
127 chalk_ir::AssocTypeId(salsa::InternKey::as_intern_id(&id))
128 }
129
from_assoc_type_id(id: AssocTypeId) -> TypeAliasId130 pub fn from_assoc_type_id(id: AssocTypeId) -> TypeAliasId {
131 salsa::InternKey::from_intern_id(id.0)
132 }
133
from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> TypeOrConstParamId134 pub fn from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> TypeOrConstParamId {
135 assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT);
136 let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx));
137 db.lookup_intern_type_or_const_param_id(interned_id)
138 }
139
to_placeholder_idx(db: &dyn HirDatabase, id: TypeOrConstParamId) -> PlaceholderIndex140 pub fn to_placeholder_idx(db: &dyn HirDatabase, id: TypeOrConstParamId) -> PlaceholderIndex {
141 let interned_id = db.intern_type_or_const_param_id(id);
142 PlaceholderIndex {
143 ui: chalk_ir::UniverseIndex::ROOT,
144 idx: salsa::InternKey::as_intern_id(&interned_id).as_usize(),
145 }
146 }
147
lt_from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> LifetimeParamId148 pub fn lt_from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> LifetimeParamId {
149 assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT);
150 let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx));
151 db.lookup_intern_lifetime_param_id(interned_id)
152 }
153
to_chalk_trait_id(id: TraitId) -> ChalkTraitId154 pub fn to_chalk_trait_id(id: TraitId) -> ChalkTraitId {
155 chalk_ir::TraitId(salsa::InternKey::as_intern_id(&id))
156 }
157
from_chalk_trait_id(id: ChalkTraitId) -> TraitId158 pub fn from_chalk_trait_id(id: ChalkTraitId) -> TraitId {
159 salsa::InternKey::from_intern_id(id.0)
160 }
161