aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2019-05-01 16:06:11 +0100
committerFlorian Diebold <[email protected]>2019-05-04 17:18:30 +0100
commitc8a643f090ed88289c7bc17b48078e39b932c8a4 (patch)
tree8ac8541e91a2d7e0413e4275c2e299a295a84790
parent99492278ac8c8a9caf3981b4406dcac6724a6a93 (diff)
Move Chalk conversion code to its own module
-rw-r--r--crates/ra_hir/src/ids.rs50
-rw-r--r--crates/ra_hir/src/ty/traits.rs283
-rw-r--r--crates/ra_hir/src/ty/traits/chalk.rs327
3 files changed, 339 insertions, 321 deletions
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs
index 6875b006d..ff4a81e59 100644
--- a/crates/ra_hir/src/ids.rs
+++ b/crates/ra_hir/src/ids.rs
@@ -323,25 +323,6 @@ impl AstItemDef<ast::TraitDef> for TraitId {
323 } 323 }
324} 324}
325 325
326fn from_chalk<T: salsa::InternKey>(chalk_id: chalk_ir::RawId) -> T {
327 T::from_intern_id(salsa::InternId::from(chalk_id.index))
328}
329fn to_chalk<T: salsa::InternKey>(salsa_id: T) -> chalk_ir::RawId {
330 chalk_ir::RawId { index: salsa_id.as_intern_id().as_u32() }
331}
332
333impl From<chalk_ir::TraitId> for TraitId {
334 fn from(trait_id: chalk_ir::TraitId) -> Self {
335 from_chalk(trait_id.0)
336 }
337}
338
339impl From<TraitId> for chalk_ir::TraitId {
340 fn from(trait_id: TraitId) -> Self {
341 chalk_ir::TraitId(to_chalk(trait_id))
342 }
343}
344
345#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 326#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
346pub struct TypeAliasId(salsa::InternId); 327pub struct TypeAliasId(salsa::InternId);
347impl_intern_key!(TypeAliasId); 328impl_intern_key!(TypeAliasId);
@@ -374,37 +355,14 @@ impl MacroCallId {
374 } 355 }
375} 356}
376 357
377/// This exists just for chalk, because chalk doesn't differentiate between 358/// This exists just for Chalk, because Chalk just has a single `StructId` where
378/// enums and structs. 359/// we have different kinds of ADTs, primitive types and special type
360/// constructors like tuples and function pointers.
379#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 361#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
380pub struct TypeCtorId(salsa::InternId); 362pub struct TypeCtorId(salsa::InternId);
381impl_intern_key!(TypeCtorId); 363impl_intern_key!(TypeCtorId);
382 364
383impl From<chalk_ir::StructId> for TypeCtorId { 365/// This exists just for Chalk, because our ImplIds are only unique per module.
384 fn from(struct_id: chalk_ir::StructId) -> Self {
385 from_chalk(struct_id.0)
386 }
387}
388
389impl From<TypeCtorId> for chalk_ir::StructId {
390 fn from(adt_id: TypeCtorId) -> Self {
391 chalk_ir::StructId(to_chalk(adt_id))
392 }
393}
394
395/// This exists just for chalk, because our ImplIds are only unique per module.
396#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 366#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
397pub struct GlobalImplId(salsa::InternId); 367pub struct GlobalImplId(salsa::InternId);
398impl_intern_key!(GlobalImplId); 368impl_intern_key!(GlobalImplId);
399
400impl From<chalk_ir::ImplId> for GlobalImplId {
401 fn from(impl_id: chalk_ir::ImplId) -> Self {
402 from_chalk(impl_id.0)
403 }
404}
405
406impl From<GlobalImplId> for chalk_ir::ImplId {
407 fn from(impl_id: GlobalImplId) -> Self {
408 chalk_ir::ImplId(to_chalk(impl_id))
409 }
410}
diff --git a/crates/ra_hir/src/ty/traits.rs b/crates/ra_hir/src/ty/traits.rs
index e1193c402..acb69683c 100644
--- a/crates/ra_hir/src/ty/traits.rs
+++ b/crates/ra_hir/src/ty/traits.rs
@@ -1,11 +1,14 @@
1//! Chalk integration. 1//! Trait solving using Chalk.
2use std::sync::{Arc, Mutex}; 2use std::sync::{Arc, Mutex};
3 3
4use chalk_ir::{TypeId, TraitId, StructId, ImplId, TypeKindId, ProjectionTy, Parameter, Identifier, cast::Cast}; 4use chalk_ir::cast::Cast;
5use chalk_rust_ir::{AssociatedTyDatum, TraitDatum, StructDatum, ImplDatum};
6 5
7use crate::{Crate, Trait, db::HirDatabase, HasGenericParams, ImplBlock}; 6use crate::{Crate, Trait, db::HirDatabase, ImplBlock};
8use super::{TraitRef, Ty, ApplicationTy, TypeCtor, Substs, infer::Canonical}; 7use super::{TraitRef, Ty, infer::Canonical};
8
9use self::chalk::{ToChalk, from_chalk};
10
11mod chalk;
9 12
10#[derive(Debug, Copy, Clone)] 13#[derive(Debug, Copy, Clone)]
11struct ChalkContext<'a, DB> { 14struct ChalkContext<'a, DB> {
@@ -13,276 +16,6 @@ struct ChalkContext<'a, DB> {
13 krate: Crate, 16 krate: Crate,
14} 17}
15 18
16pub(crate) trait ToChalk {
17 type Chalk;
18 fn to_chalk(self, db: &impl HirDatabase) -> Self::Chalk;
19 fn from_chalk(db: &impl HirDatabase, chalk: Self::Chalk) -> Self;
20}
21
22pub(crate) fn from_chalk<T, ChalkT>(db: &impl HirDatabase, chalk: ChalkT) -> T
23where
24 T: ToChalk<Chalk = ChalkT>,
25{
26 T::from_chalk(db, chalk)
27}
28
29impl ToChalk for Ty {
30 type Chalk = chalk_ir::Ty;
31 fn to_chalk(self, db: &impl HirDatabase) -> chalk_ir::Ty {
32 match self {
33 Ty::Apply(apply_ty) => chalk_ir::Ty::Apply(apply_ty.to_chalk(db)),
34 Ty::Param { idx, .. } => {
35 chalk_ir::PlaceholderIndex { ui: chalk_ir::UniverseIndex::ROOT, idx: idx as usize }
36 .to_ty()
37 }
38 Ty::Bound(idx) => chalk_ir::Ty::BoundVar(idx as usize),
39 Ty::Infer(_infer_ty) => panic!("uncanonicalized infer ty"),
40 Ty::Unknown => unimplemented!(), // TODO turn into placeholder?
41 }
42 }
43 fn from_chalk(db: &impl HirDatabase, chalk: chalk_ir::Ty) -> Self {
44 match chalk {
45 chalk_ir::Ty::Apply(apply_ty) => {
46 match apply_ty.name {
47 // FIXME handle TypeKindId::Trait/Type here
48 chalk_ir::TypeName::TypeKindId(_) => Ty::Apply(from_chalk(db, apply_ty)),
49 chalk_ir::TypeName::AssociatedType(_) => unimplemented!(),
50 chalk_ir::TypeName::Placeholder(idx) => {
51 assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT);
52 Ty::Param { idx: idx.idx as u32, name: crate::Name::missing() }
53 }
54 }
55 }
56 chalk_ir::Ty::Projection(_) => unimplemented!(),
57 chalk_ir::Ty::UnselectedProjection(_) => unimplemented!(),
58 chalk_ir::Ty::ForAll(_) => unimplemented!(),
59 chalk_ir::Ty::BoundVar(idx) => Ty::Bound(idx as u32),
60 chalk_ir::Ty::InferenceVar(_iv) => panic!("unexpected chalk infer ty"),
61 }
62 }
63}
64
65impl ToChalk for ApplicationTy {
66 type Chalk = chalk_ir::ApplicationTy;
67
68 fn to_chalk(self: ApplicationTy, db: &impl HirDatabase) -> chalk_ir::ApplicationTy {
69 let struct_id = self.ctor.to_chalk(db);
70 let name = chalk_ir::TypeName::TypeKindId(struct_id.into());
71 let parameters = self.parameters.to_chalk(db);
72 chalk_ir::ApplicationTy { name, parameters }
73 }
74
75 fn from_chalk(db: &impl HirDatabase, apply_ty: chalk_ir::ApplicationTy) -> ApplicationTy {
76 let ctor = match apply_ty.name {
77 chalk_ir::TypeName::TypeKindId(chalk_ir::TypeKindId::StructId(struct_id)) => {
78 from_chalk(db, struct_id)
79 }
80 chalk_ir::TypeName::TypeKindId(_) => unimplemented!(),
81 chalk_ir::TypeName::Placeholder(_) => unimplemented!(),
82 chalk_ir::TypeName::AssociatedType(_) => unimplemented!(),
83 };
84 let parameters = from_chalk(db, apply_ty.parameters);
85 ApplicationTy { ctor, parameters }
86 }
87}
88
89impl ToChalk for Substs {
90 type Chalk = Vec<chalk_ir::Parameter>;
91
92 fn to_chalk(self, db: &impl HirDatabase) -> Vec<chalk_ir::Parameter> {
93 self.iter().map(|ty| ty.clone().to_chalk(db).cast()).collect()
94 }
95
96 fn from_chalk(db: &impl HirDatabase, parameters: Vec<chalk_ir::Parameter>) -> Substs {
97 parameters
98 .into_iter()
99 .map(|p| match p {
100 chalk_ir::Parameter(chalk_ir::ParameterKind::Ty(ty)) => from_chalk(db, ty),
101 chalk_ir::Parameter(chalk_ir::ParameterKind::Lifetime(_)) => unimplemented!(),
102 })
103 .collect::<Vec<_>>()
104 .into()
105 }
106}
107
108impl ToChalk for TraitRef {
109 type Chalk = chalk_ir::TraitRef;
110
111 fn to_chalk(self: TraitRef, db: &impl HirDatabase) -> chalk_ir::TraitRef {
112 let trait_id = self.trait_.to_chalk(db);
113 let parameters = self.substs.to_chalk(db);
114 chalk_ir::TraitRef { trait_id, parameters }
115 }
116
117 fn from_chalk(db: &impl HirDatabase, trait_ref: chalk_ir::TraitRef) -> Self {
118 let trait_ = from_chalk(db, trait_ref.trait_id);
119 let substs = from_chalk(db, trait_ref.parameters);
120 TraitRef { trait_, substs }
121 }
122}
123
124impl ToChalk for Trait {
125 type Chalk = TraitId;
126
127 fn to_chalk(self, _db: &impl HirDatabase) -> TraitId {
128 self.id.into()
129 }
130
131 fn from_chalk(_db: &impl HirDatabase, trait_id: TraitId) -> Trait {
132 Trait { id: trait_id.into() }
133 }
134}
135
136impl ToChalk for TypeCtor {
137 type Chalk = chalk_ir::StructId;
138
139 fn to_chalk(self, db: &impl HirDatabase) -> chalk_ir::StructId {
140 db.intern_type_ctor(self).into()
141 }
142
143 fn from_chalk(db: &impl HirDatabase, struct_id: chalk_ir::StructId) -> TypeCtor {
144 db.lookup_intern_type_ctor(struct_id.into())
145 }
146}
147
148impl ToChalk for ImplBlock {
149 type Chalk = chalk_ir::ImplId;
150
151 fn to_chalk(self, db: &impl HirDatabase) -> chalk_ir::ImplId {
152 db.intern_impl_block(self).into()
153 }
154
155 fn from_chalk(db: &impl HirDatabase, impl_id: chalk_ir::ImplId) -> ImplBlock {
156 db.lookup_intern_impl_block(impl_id.into())
157 }
158}
159
160fn make_binders<T>(value: T, num_vars: usize) -> chalk_ir::Binders<T> {
161 chalk_ir::Binders {
162 value,
163 binders: std::iter::repeat(chalk_ir::ParameterKind::Ty(())).take(num_vars).collect(),
164 }
165}
166
167impl<'a, DB> chalk_solve::RustIrDatabase for ChalkContext<'a, DB>
168where
169 DB: HirDatabase,
170{
171 fn associated_ty_data(&self, _ty: TypeId) -> Arc<AssociatedTyDatum> {
172 unimplemented!()
173 }
174 fn trait_datum(&self, trait_id: TraitId) -> Arc<TraitDatum> {
175 eprintln!("trait_datum {:?}", trait_id);
176 let trait_: Trait = from_chalk(self.db, trait_id);
177 let generic_params = trait_.generic_params(self.db);
178 let bound_vars = Substs::bound_vars(&generic_params);
179 let trait_ref = trait_.trait_ref(self.db).subst(&bound_vars).to_chalk(self.db);
180 let flags = chalk_rust_ir::TraitFlags {
181 // FIXME set these flags correctly
182 auto: false,
183 marker: false,
184 upstream: trait_.module(self.db).krate(self.db) != Some(self.krate),
185 fundamental: false,
186 };
187 let where_clauses = Vec::new(); // FIXME add where clauses
188 let trait_datum_bound = chalk_rust_ir::TraitDatumBound { trait_ref, where_clauses, flags };
189 let trait_datum = TraitDatum { binders: make_binders(trait_datum_bound, bound_vars.len()) };
190 Arc::new(trait_datum)
191 }
192 fn struct_datum(&self, struct_id: StructId) -> Arc<StructDatum> {
193 eprintln!("struct_datum {:?}", struct_id);
194 let type_ctor = from_chalk(self.db, struct_id);
195 // TODO might be nicer if we can create a fake GenericParams for the TypeCtor
196 let (num_params, upstream) = match type_ctor {
197 TypeCtor::Bool
198 | TypeCtor::Char
199 | TypeCtor::Int(_)
200 | TypeCtor::Float(_)
201 | TypeCtor::Never
202 | TypeCtor::Str => (0, true),
203 TypeCtor::Slice | TypeCtor::Array | TypeCtor::RawPtr(_) | TypeCtor::Ref(_) => (1, true),
204 TypeCtor::FnPtr | TypeCtor::Tuple => unimplemented!(), // FIXME tuples and FnPtr are currently variadic... we need to make the parameter number explicit
205 TypeCtor::FnDef(_) => unimplemented!(),
206 TypeCtor::Adt(adt) => {
207 let generic_params = adt.generic_params(self.db);
208 (
209 generic_params.count_params_including_parent(),
210 adt.krate(self.db) != Some(self.krate),
211 )
212 }
213 };
214 let flags = chalk_rust_ir::StructFlags {
215 upstream,
216 // FIXME set fundamental flag correctly
217 fundamental: false,
218 };
219 let where_clauses = Vec::new(); // FIXME add where clauses
220 let ty = ApplicationTy {
221 ctor: type_ctor,
222 parameters: (0..num_params).map(|i| Ty::Bound(i as u32)).collect::<Vec<_>>().into(),
223 };
224 let struct_datum_bound = chalk_rust_ir::StructDatumBound {
225 self_ty: ty.to_chalk(self.db),
226 fields: Vec::new(), // FIXME add fields (only relevant for auto traits)
227 where_clauses,
228 flags,
229 };
230 let struct_datum = StructDatum { binders: make_binders(struct_datum_bound, num_params) };
231 Arc::new(struct_datum)
232 }
233 fn impl_datum(&self, impl_id: ImplId) -> Arc<ImplDatum> {
234 eprintln!("impl_datum {:?}", impl_id);
235 let impl_block: ImplBlock = from_chalk(self.db, impl_id);
236 let generic_params = impl_block.generic_params(self.db);
237 let bound_vars = Substs::bound_vars(&generic_params);
238 let trait_ref = impl_block
239 .target_trait_ref(self.db)
240 .expect("FIXME handle unresolved impl block trait ref")
241 .subst(&bound_vars);
242 let impl_type = if impl_block.module().krate(self.db) == Some(self.krate) {
243 chalk_rust_ir::ImplType::Local
244 } else {
245 chalk_rust_ir::ImplType::External
246 };
247 let impl_datum_bound = chalk_rust_ir::ImplDatumBound {
248 // FIXME handle negative impls (impl !Sync for Foo)
249 trait_ref: chalk_rust_ir::PolarizedTraitRef::Positive(trait_ref.to_chalk(self.db)),
250 where_clauses: Vec::new(), // FIXME add where clauses
251 associated_ty_values: Vec::new(), // FIXME add associated type values
252 impl_type,
253 };
254 let impl_datum = ImplDatum { binders: make_binders(impl_datum_bound, bound_vars.len()) };
255 Arc::new(impl_datum)
256 }
257 fn impls_for_trait(&self, trait_id: TraitId) -> Vec<ImplId> {
258 eprintln!("impls_for_trait {:?}", trait_id);
259 let trait_ = from_chalk(self.db, trait_id);
260 self.db
261 .impls_for_trait(self.krate, trait_)
262 .iter()
263 // FIXME temporary hack -- as long as we're not lowering where clauses
264 // correctly, ignore impls with them completely so as to not treat
265 // impl<T> Trait for T where T: ... as a blanket impl on all types
266 .filter(|impl_block| impl_block.generic_params(self.db).where_predicates.is_empty())
267 .map(|impl_block| impl_block.to_chalk(self.db))
268 .collect()
269 }
270 fn impl_provided_for(&self, auto_trait_id: TraitId, struct_id: StructId) -> bool {
271 eprintln!("impl_provided_for {:?}, {:?}", auto_trait_id, struct_id);
272 false // FIXME
273 }
274 fn type_name(&self, _id: TypeKindId) -> Identifier {
275 unimplemented!()
276 }
277 fn split_projection<'p>(
278 &self,
279 projection: &'p ProjectionTy,
280 ) -> (Arc<AssociatedTyDatum>, &'p [Parameter], &'p [Parameter]) {
281 eprintln!("split_projection {:?}", projection);
282 unimplemented!()
283 }
284}
285
286pub(crate) fn solver(_db: &impl HirDatabase, _krate: Crate) -> Arc<Mutex<chalk_solve::Solver>> { 19pub(crate) fn solver(_db: &impl HirDatabase, _krate: Crate) -> Arc<Mutex<chalk_solve::Solver>> {
287 // krate parameter is just so we cache a unique solver per crate 20 // krate parameter is just so we cache a unique solver per crate
288 let solver_choice = chalk_solve::SolverChoice::SLG { max_size: 10 }; 21 let solver_choice = chalk_solve::SolverChoice::SLG { max_size: 10 };
diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs
new file mode 100644
index 000000000..36cc52704
--- /dev/null
+++ b/crates/ra_hir/src/ty/traits/chalk.rs
@@ -0,0 +1,327 @@
1//! Conversion code from/to Chalk.
2use std::sync::Arc;
3
4use chalk_ir::{TypeId, ImplId, TypeKindId, ProjectionTy, Parameter, Identifier, cast::Cast, PlaceholderIndex, UniverseIndex, TypeName};
5use chalk_rust_ir::{AssociatedTyDatum, TraitDatum, StructDatum, ImplDatum};
6
7use ra_db::salsa::{InternId, InternKey};
8
9use crate::{
10 Trait, HasGenericParams, ImplBlock,
11 db::HirDatabase,
12 ty::{TraitRef, Ty, ApplicationTy, TypeCtor, Substs},
13};
14use super::ChalkContext;
15
16pub(super) trait ToChalk {
17 type Chalk;
18 fn to_chalk(self, db: &impl HirDatabase) -> Self::Chalk;
19 fn from_chalk(db: &impl HirDatabase, chalk: Self::Chalk) -> Self;
20}
21
22pub(super) fn from_chalk<T, ChalkT>(db: &impl HirDatabase, chalk: ChalkT) -> T
23where
24 T: ToChalk<Chalk = ChalkT>,
25{
26 T::from_chalk(db, chalk)
27}
28
29impl ToChalk for Ty {
30 type Chalk = chalk_ir::Ty;
31 fn to_chalk(self, db: &impl HirDatabase) -> chalk_ir::Ty {
32 match self {
33 Ty::Apply(apply_ty) => chalk_ir::Ty::Apply(apply_ty.to_chalk(db)),
34 Ty::Param { idx, .. } => {
35 PlaceholderIndex { ui: UniverseIndex::ROOT, idx: idx as usize }
36 .to_ty()
37 }
38 Ty::Bound(idx) => chalk_ir::Ty::BoundVar(idx as usize),
39 Ty::Infer(_infer_ty) => panic!("uncanonicalized infer ty"),
40 Ty::Unknown => unimplemented!(), // TODO turn into placeholder?
41 }
42 }
43 fn from_chalk(db: &impl HirDatabase, chalk: chalk_ir::Ty) -> Self {
44 match chalk {
45 chalk_ir::Ty::Apply(apply_ty) => {
46 match apply_ty.name {
47 // FIXME handle TypeKindId::Trait/Type here
48 TypeName::TypeKindId(_) => Ty::Apply(from_chalk(db, apply_ty)),
49 TypeName::AssociatedType(_) => unimplemented!(),
50 TypeName::Placeholder(idx) => {
51 assert_eq!(idx.ui, UniverseIndex::ROOT);
52 Ty::Param { idx: idx.idx as u32, name: crate::Name::missing() }
53 }
54 }
55 }
56 chalk_ir::Ty::Projection(_) => unimplemented!(),
57 chalk_ir::Ty::UnselectedProjection(_) => unimplemented!(),
58 chalk_ir::Ty::ForAll(_) => unimplemented!(),
59 chalk_ir::Ty::BoundVar(idx) => Ty::Bound(idx as u32),
60 chalk_ir::Ty::InferenceVar(_iv) => panic!("unexpected chalk infer ty"),
61 }
62 }
63}
64
65impl ToChalk for ApplicationTy {
66 type Chalk = chalk_ir::ApplicationTy;
67
68 fn to_chalk(self: ApplicationTy, db: &impl HirDatabase) -> chalk_ir::ApplicationTy {
69 let struct_id = self.ctor.to_chalk(db);
70 let name = TypeName::TypeKindId(struct_id.into());
71 let parameters = self.parameters.to_chalk(db);
72 chalk_ir::ApplicationTy { name, parameters }
73 }
74
75 fn from_chalk(db: &impl HirDatabase, apply_ty: chalk_ir::ApplicationTy) -> ApplicationTy {
76 let ctor = match apply_ty.name {
77 TypeName::TypeKindId(TypeKindId::StructId(struct_id)) => {
78 from_chalk(db, struct_id)
79 }
80 TypeName::TypeKindId(_) => unimplemented!(),
81 TypeName::Placeholder(_) => unimplemented!(),
82 TypeName::AssociatedType(_) => unimplemented!(),
83 };
84 let parameters = from_chalk(db, apply_ty.parameters);
85 ApplicationTy { ctor, parameters }
86 }
87}
88
89impl ToChalk for Substs {
90 type Chalk = Vec<chalk_ir::Parameter>;
91
92 fn to_chalk(self, db: &impl HirDatabase) -> Vec<Parameter> {
93 self.iter().map(|ty| ty.clone().to_chalk(db).cast()).collect()
94 }
95
96 fn from_chalk(db: &impl HirDatabase, parameters: Vec<chalk_ir::Parameter>) -> Substs {
97 parameters
98 .into_iter()
99 .map(|p| match p {
100 chalk_ir::Parameter(chalk_ir::ParameterKind::Ty(ty)) => from_chalk(db, ty),
101 chalk_ir::Parameter(chalk_ir::ParameterKind::Lifetime(_)) => unimplemented!(),
102 })
103 .collect::<Vec<_>>()
104 .into()
105 }
106}
107
108impl ToChalk for TraitRef {
109 type Chalk = chalk_ir::TraitRef;
110
111 fn to_chalk(self: TraitRef, db: &impl HirDatabase) -> chalk_ir::TraitRef {
112 let trait_id = self.trait_.to_chalk(db);
113 let parameters = self.substs.to_chalk(db);
114 chalk_ir::TraitRef { trait_id, parameters }
115 }
116
117 fn from_chalk(db: &impl HirDatabase, trait_ref: chalk_ir::TraitRef) -> Self {
118 let trait_ = from_chalk(db, trait_ref.trait_id);
119 let substs = from_chalk(db, trait_ref.parameters);
120 TraitRef { trait_, substs }
121 }
122}
123
124impl ToChalk for Trait {
125 type Chalk = chalk_ir::TraitId;
126
127 fn to_chalk(self, _db: &impl HirDatabase) -> chalk_ir::TraitId {
128 self.id.into()
129 }
130
131 fn from_chalk(_db: &impl HirDatabase, trait_id: chalk_ir::TraitId) -> Trait {
132 Trait { id: trait_id.into() }
133 }
134}
135
136impl ToChalk for TypeCtor {
137 type Chalk = chalk_ir::StructId;
138
139 fn to_chalk(self, db: &impl HirDatabase) -> chalk_ir::StructId {
140 db.intern_type_ctor(self).into()
141 }
142
143 fn from_chalk(db: &impl HirDatabase, struct_id: chalk_ir::StructId) -> TypeCtor {
144 db.lookup_intern_type_ctor(struct_id.into())
145 }
146}
147
148impl ToChalk for ImplBlock {
149 type Chalk = chalk_ir::ImplId;
150
151 fn to_chalk(self, db: &impl HirDatabase) -> chalk_ir::ImplId {
152 db.intern_impl_block(self).into()
153 }
154
155 fn from_chalk(db: &impl HirDatabase, impl_id: chalk_ir::ImplId) -> ImplBlock {
156 db.lookup_intern_impl_block(impl_id.into())
157 }
158}
159
160fn make_binders<T>(value: T, num_vars: usize) -> chalk_ir::Binders<T> {
161 chalk_ir::Binders {
162 value,
163 binders: std::iter::repeat(chalk_ir::ParameterKind::Ty(())).take(num_vars).collect(),
164 }
165}
166
167impl<'a, DB> chalk_solve::RustIrDatabase for ChalkContext<'a, DB>
168where
169 DB: HirDatabase,
170{
171 fn associated_ty_data(&self, _ty: TypeId) -> Arc<AssociatedTyDatum> {
172 unimplemented!()
173 }
174 fn trait_datum(&self, trait_id: chalk_ir::TraitId) -> Arc<TraitDatum> {
175 eprintln!("trait_datum {:?}", trait_id);
176 let trait_: Trait = from_chalk(self.db, trait_id);
177 let generic_params = trait_.generic_params(self.db);
178 let bound_vars = Substs::bound_vars(&generic_params);
179 let trait_ref = trait_.trait_ref(self.db).subst(&bound_vars).to_chalk(self.db);
180 let flags = chalk_rust_ir::TraitFlags {
181 // FIXME set these flags correctly
182 auto: false,
183 marker: false,
184 upstream: trait_.module(self.db).krate(self.db) != Some(self.krate),
185 fundamental: false,
186 };
187 let where_clauses = Vec::new(); // FIXME add where clauses
188 let trait_datum_bound = chalk_rust_ir::TraitDatumBound { trait_ref, where_clauses, flags };
189 let trait_datum = TraitDatum { binders: make_binders(trait_datum_bound, bound_vars.len()) };
190 Arc::new(trait_datum)
191 }
192 fn struct_datum(&self, struct_id: chalk_ir::StructId) -> Arc<StructDatum> {
193 eprintln!("struct_datum {:?}", struct_id);
194 let type_ctor = from_chalk(self.db, struct_id);
195 // TODO might be nicer if we can create a fake GenericParams for the TypeCtor
196 let (num_params, upstream) = match type_ctor {
197 TypeCtor::Bool
198 | TypeCtor::Char
199 | TypeCtor::Int(_)
200 | TypeCtor::Float(_)
201 | TypeCtor::Never
202 | TypeCtor::Str => (0, true),
203 TypeCtor::Slice | TypeCtor::Array | TypeCtor::RawPtr(_) | TypeCtor::Ref(_) => (1, true),
204 TypeCtor::FnPtr | TypeCtor::Tuple => unimplemented!(), // FIXME tuples and FnPtr are currently variadic... we need to make the parameter number explicit
205 TypeCtor::FnDef(_) => unimplemented!(),
206 TypeCtor::Adt(adt) => {
207 let generic_params = adt.generic_params(self.db);
208 (
209 generic_params.count_params_including_parent(),
210 adt.krate(self.db) != Some(self.krate),
211 )
212 }
213 };
214 let flags = chalk_rust_ir::StructFlags {
215 upstream,
216 // FIXME set fundamental flag correctly
217 fundamental: false,
218 };
219 let where_clauses = Vec::new(); // FIXME add where clauses
220 let ty = ApplicationTy {
221 ctor: type_ctor,
222 parameters: (0..num_params).map(|i| Ty::Bound(i as u32)).collect::<Vec<_>>().into(),
223 };
224 let struct_datum_bound = chalk_rust_ir::StructDatumBound {
225 self_ty: ty.to_chalk(self.db),
226 fields: Vec::new(), // FIXME add fields (only relevant for auto traits)
227 where_clauses,
228 flags,
229 };
230 let struct_datum = StructDatum { binders: make_binders(struct_datum_bound, num_params) };
231 Arc::new(struct_datum)
232 }
233 fn impl_datum(&self, impl_id: ImplId) -> Arc<ImplDatum> {
234 eprintln!("impl_datum {:?}", impl_id);
235 let impl_block: ImplBlock = from_chalk(self.db, impl_id);
236 let generic_params = impl_block.generic_params(self.db);
237 let bound_vars = Substs::bound_vars(&generic_params);
238 let trait_ref = impl_block
239 .target_trait_ref(self.db)
240 .expect("FIXME handle unresolved impl block trait ref")
241 .subst(&bound_vars);
242 let impl_type = if impl_block.module().krate(self.db) == Some(self.krate) {
243 chalk_rust_ir::ImplType::Local
244 } else {
245 chalk_rust_ir::ImplType::External
246 };
247 let impl_datum_bound = chalk_rust_ir::ImplDatumBound {
248 // FIXME handle negative impls (impl !Sync for Foo)
249 trait_ref: chalk_rust_ir::PolarizedTraitRef::Positive(trait_ref.to_chalk(self.db)),
250 where_clauses: Vec::new(), // FIXME add where clauses
251 associated_ty_values: Vec::new(), // FIXME add associated type values
252 impl_type,
253 };
254 let impl_datum = ImplDatum { binders: make_binders(impl_datum_bound, bound_vars.len()) };
255 Arc::new(impl_datum)
256 }
257 fn impls_for_trait(&self, trait_id: chalk_ir::TraitId) -> Vec<ImplId> {
258 eprintln!("impls_for_trait {:?}", trait_id);
259 let trait_ = from_chalk(self.db, trait_id);
260 self.db
261 .impls_for_trait(self.krate, trait_)
262 .iter()
263 // FIXME temporary hack -- as long as we're not lowering where clauses
264 // correctly, ignore impls with them completely so as to not treat
265 // impl<T> Trait for T where T: ... as a blanket impl on all types
266 .filter(|impl_block| impl_block.generic_params(self.db).where_predicates.is_empty())
267 .map(|impl_block| impl_block.to_chalk(self.db))
268 .collect()
269 }
270 fn impl_provided_for(&self, auto_trait_id: chalk_ir::TraitId, struct_id: chalk_ir::StructId) -> bool {
271 eprintln!("impl_provided_for {:?}, {:?}", auto_trait_id, struct_id);
272 false // FIXME
273 }
274 fn type_name(&self, _id: TypeKindId) -> Identifier {
275 unimplemented!()
276 }
277 fn split_projection<'p>(
278 &self,
279 projection: &'p ProjectionTy,
280 ) -> (Arc<AssociatedTyDatum>, &'p [Parameter], &'p [Parameter]) {
281 eprintln!("split_projection {:?}", projection);
282 unimplemented!()
283 }
284}
285
286fn id_from_chalk<T: InternKey>(chalk_id: chalk_ir::RawId) -> T {
287 T::from_intern_id(InternId::from(chalk_id.index))
288}
289fn id_to_chalk<T: InternKey>(salsa_id: T) -> chalk_ir::RawId {
290 chalk_ir::RawId { index: salsa_id.as_intern_id().as_u32() }
291}
292
293impl From<chalk_ir::TraitId> for crate::ids::TraitId {
294 fn from(trait_id: chalk_ir::TraitId) -> Self {
295 id_from_chalk(trait_id.0)
296 }
297}
298
299impl From<crate::ids::TraitId> for chalk_ir::TraitId {
300 fn from(trait_id: crate::ids::TraitId) -> Self {
301 chalk_ir::TraitId(id_to_chalk(trait_id))
302 }
303}
304
305impl From<chalk_ir::StructId> for crate::ids::TypeCtorId {
306 fn from(struct_id: chalk_ir::StructId) -> Self {
307 id_from_chalk(struct_id.0)
308 }
309}
310
311impl From<crate::ids::TypeCtorId> for chalk_ir::StructId {
312 fn from(type_ctor_id: crate::ids::TypeCtorId) -> Self {
313 chalk_ir::StructId(id_to_chalk(type_ctor_id))
314 }
315}
316
317impl From<chalk_ir::ImplId> for crate::ids::GlobalImplId {
318 fn from(impl_id: chalk_ir::ImplId) -> Self {
319 id_from_chalk(impl_id.0)
320 }
321}
322
323impl From<crate::ids::GlobalImplId> for chalk_ir::ImplId {
324 fn from(impl_id: crate::ids::GlobalImplId) -> Self {
325 chalk_ir::ImplId(id_to_chalk(impl_id))
326 }
327}