diff options
Diffstat (limited to 'crates/hir_ty/src/traits/chalk.rs')
-rw-r--r-- | crates/hir_ty/src/traits/chalk.rs | 586 |
1 files changed, 586 insertions, 0 deletions
diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs new file mode 100644 index 000000000..b33653417 --- /dev/null +++ b/crates/hir_ty/src/traits/chalk.rs | |||
@@ -0,0 +1,586 @@ | |||
1 | //! Conversion code from/to Chalk. | ||
2 | use std::sync::Arc; | ||
3 | |||
4 | use log::debug; | ||
5 | |||
6 | use chalk_ir::{fold::shift::Shift, CanonicalVarKinds, GenericArg, TypeName}; | ||
7 | use chalk_solve::rust_ir::{self, OpaqueTyDatumBound, WellKnownTrait}; | ||
8 | |||
9 | use base_db::{salsa::InternKey, CrateId}; | ||
10 | use hir_def::{ | ||
11 | lang_item::{lang_attr, LangItemTarget}, | ||
12 | AssocContainerId, AssocItemId, HasModule, Lookup, TypeAliasId, | ||
13 | }; | ||
14 | |||
15 | use super::ChalkContext; | ||
16 | use crate::{ | ||
17 | db::HirDatabase, | ||
18 | display::HirDisplay, | ||
19 | method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS}, | ||
20 | utils::generics, | ||
21 | CallableDefId, DebruijnIndex, FnSig, GenericPredicate, Substs, Ty, TypeCtor, | ||
22 | }; | ||
23 | use mapping::{ | ||
24 | convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue, | ||
25 | }; | ||
26 | |||
27 | pub use self::interner::*; | ||
28 | |||
29 | pub(super) mod tls; | ||
30 | mod interner; | ||
31 | mod mapping; | ||
32 | |||
33 | pub(super) trait ToChalk { | ||
34 | type Chalk; | ||
35 | fn to_chalk(self, db: &dyn HirDatabase) -> Self::Chalk; | ||
36 | fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self; | ||
37 | } | ||
38 | |||
39 | pub(super) fn from_chalk<T, ChalkT>(db: &dyn HirDatabase, chalk: ChalkT) -> T | ||
40 | where | ||
41 | T: ToChalk<Chalk = ChalkT>, | ||
42 | { | ||
43 | T::from_chalk(db, chalk) | ||
44 | } | ||
45 | |||
46 | impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | ||
47 | fn associated_ty_data(&self, id: AssocTypeId) -> Arc<AssociatedTyDatum> { | ||
48 | self.db.associated_ty_data(id) | ||
49 | } | ||
50 | fn trait_datum(&self, trait_id: TraitId) -> Arc<TraitDatum> { | ||
51 | self.db.trait_datum(self.krate, trait_id) | ||
52 | } | ||
53 | fn adt_datum(&self, struct_id: AdtId) -> Arc<StructDatum> { | ||
54 | self.db.struct_datum(self.krate, struct_id) | ||
55 | } | ||
56 | fn adt_repr(&self, _struct_id: AdtId) -> rust_ir::AdtRepr { | ||
57 | rust_ir::AdtRepr { repr_c: false, repr_packed: false } | ||
58 | } | ||
59 | fn impl_datum(&self, impl_id: ImplId) -> Arc<ImplDatum> { | ||
60 | self.db.impl_datum(self.krate, impl_id) | ||
61 | } | ||
62 | |||
63 | fn fn_def_datum( | ||
64 | &self, | ||
65 | fn_def_id: chalk_ir::FnDefId<Interner>, | ||
66 | ) -> Arc<rust_ir::FnDefDatum<Interner>> { | ||
67 | self.db.fn_def_datum(self.krate, fn_def_id) | ||
68 | } | ||
69 | |||
70 | fn impls_for_trait( | ||
71 | &self, | ||
72 | trait_id: TraitId, | ||
73 | parameters: &[GenericArg<Interner>], | ||
74 | binders: &CanonicalVarKinds<Interner>, | ||
75 | ) -> Vec<ImplId> { | ||
76 | debug!("impls_for_trait {:?}", trait_id); | ||
77 | let trait_: hir_def::TraitId = from_chalk(self.db, trait_id); | ||
78 | |||
79 | let ty: Ty = from_chalk(self.db, parameters[0].assert_ty_ref(&Interner).clone()); | ||
80 | |||
81 | fn binder_kind(ty: &Ty, binders: &CanonicalVarKinds<Interner>) -> Option<chalk_ir::TyKind> { | ||
82 | if let Ty::Bound(bv) = ty { | ||
83 | let binders = binders.as_slice(&Interner); | ||
84 | if bv.debruijn == DebruijnIndex::INNERMOST { | ||
85 | if let chalk_ir::VariableKind::Ty(tk) = binders[bv.index].kind { | ||
86 | return Some(tk); | ||
87 | } | ||
88 | } | ||
89 | } | ||
90 | None | ||
91 | } | ||
92 | |||
93 | let self_ty_fp = TyFingerprint::for_impl(&ty); | ||
94 | let fps: &[TyFingerprint] = match binder_kind(&ty, binders) { | ||
95 | Some(chalk_ir::TyKind::Integer) => &ALL_INT_FPS, | ||
96 | Some(chalk_ir::TyKind::Float) => &ALL_FLOAT_FPS, | ||
97 | _ => self_ty_fp.as_ref().map(std::slice::from_ref).unwrap_or(&[]), | ||
98 | }; | ||
99 | |||
100 | // Note: Since we're using impls_for_trait, only impls where the trait | ||
101 | // can be resolved should ever reach Chalk. `impl_datum` relies on that | ||
102 | // and will panic if the trait can't be resolved. | ||
103 | let in_deps = self.db.trait_impls_in_deps(self.krate); | ||
104 | let in_self = self.db.trait_impls_in_crate(self.krate); | ||
105 | let impl_maps = [in_deps, in_self]; | ||
106 | |||
107 | let id_to_chalk = |id: hir_def::ImplId| id.to_chalk(self.db); | ||
108 | |||
109 | let result: Vec<_> = if fps.is_empty() { | ||
110 | debug!("Unrestricted search for {:?} impls...", trait_); | ||
111 | impl_maps | ||
112 | .iter() | ||
113 | .flat_map(|crate_impl_defs| crate_impl_defs.for_trait(trait_).map(id_to_chalk)) | ||
114 | .collect() | ||
115 | } else { | ||
116 | impl_maps | ||
117 | .iter() | ||
118 | .flat_map(|crate_impl_defs| { | ||
119 | fps.iter().flat_map(move |fp| { | ||
120 | crate_impl_defs.for_trait_and_self_ty(trait_, *fp).map(id_to_chalk) | ||
121 | }) | ||
122 | }) | ||
123 | .collect() | ||
124 | }; | ||
125 | |||
126 | debug!("impls_for_trait returned {} impls", result.len()); | ||
127 | result | ||
128 | } | ||
129 | fn impl_provided_for(&self, auto_trait_id: TraitId, struct_id: AdtId) -> bool { | ||
130 | debug!("impl_provided_for {:?}, {:?}", auto_trait_id, struct_id); | ||
131 | false // FIXME | ||
132 | } | ||
133 | fn associated_ty_value(&self, id: AssociatedTyValueId) -> Arc<AssociatedTyValue> { | ||
134 | self.db.associated_ty_value(self.krate, id) | ||
135 | } | ||
136 | |||
137 | fn custom_clauses(&self) -> Vec<chalk_ir::ProgramClause<Interner>> { | ||
138 | vec![] | ||
139 | } | ||
140 | fn local_impls_to_coherence_check(&self, _trait_id: TraitId) -> Vec<ImplId> { | ||
141 | // We don't do coherence checking (yet) | ||
142 | unimplemented!() | ||
143 | } | ||
144 | fn interner(&self) -> &Interner { | ||
145 | &Interner | ||
146 | } | ||
147 | fn well_known_trait_id( | ||
148 | &self, | ||
149 | well_known_trait: rust_ir::WellKnownTrait, | ||
150 | ) -> Option<chalk_ir::TraitId<Interner>> { | ||
151 | let lang_attr = lang_attr_from_well_known_trait(well_known_trait); | ||
152 | let trait_ = match self.db.lang_item(self.krate, lang_attr.into()) { | ||
153 | Some(LangItemTarget::TraitId(trait_)) => trait_, | ||
154 | _ => return None, | ||
155 | }; | ||
156 | Some(trait_.to_chalk(self.db)) | ||
157 | } | ||
158 | |||
159 | fn program_clauses_for_env( | ||
160 | &self, | ||
161 | environment: &chalk_ir::Environment<Interner>, | ||
162 | ) -> chalk_ir::ProgramClauses<Interner> { | ||
163 | self.db.program_clauses_for_chalk_env(self.krate, environment.clone()) | ||
164 | } | ||
165 | |||
166 | fn opaque_ty_data(&self, id: chalk_ir::OpaqueTyId<Interner>) -> Arc<OpaqueTyDatum> { | ||
167 | let interned_id = crate::db::InternedOpaqueTyId::from(id); | ||
168 | let full_id = self.db.lookup_intern_impl_trait_id(interned_id); | ||
169 | let (func, idx) = match full_id { | ||
170 | crate::OpaqueTyId::ReturnTypeImplTrait(func, idx) => (func, idx), | ||
171 | }; | ||
172 | let datas = | ||
173 | self.db.return_type_impl_traits(func).expect("impl trait id without impl traits"); | ||
174 | let data = &datas.value.impl_traits[idx as usize]; | ||
175 | let bound = OpaqueTyDatumBound { | ||
176 | bounds: make_binders( | ||
177 | data.bounds | ||
178 | .value | ||
179 | .iter() | ||
180 | .cloned() | ||
181 | .filter(|b| !b.is_error()) | ||
182 | .map(|b| b.to_chalk(self.db)) | ||
183 | .collect(), | ||
184 | 1, | ||
185 | ), | ||
186 | where_clauses: make_binders(vec![], 0), | ||
187 | }; | ||
188 | let num_vars = datas.num_binders; | ||
189 | Arc::new(OpaqueTyDatum { opaque_ty_id: id, bound: make_binders(bound, num_vars) }) | ||
190 | } | ||
191 | |||
192 | fn hidden_opaque_type(&self, _id: chalk_ir::OpaqueTyId<Interner>) -> chalk_ir::Ty<Interner> { | ||
193 | // FIXME: actually provide the hidden type; it is relevant for auto traits | ||
194 | Ty::Unknown.to_chalk(self.db) | ||
195 | } | ||
196 | |||
197 | fn is_object_safe(&self, _trait_id: chalk_ir::TraitId<Interner>) -> bool { | ||
198 | // FIXME: implement actual object safety | ||
199 | true | ||
200 | } | ||
201 | |||
202 | fn closure_kind( | ||
203 | &self, | ||
204 | _closure_id: chalk_ir::ClosureId<Interner>, | ||
205 | _substs: &chalk_ir::Substitution<Interner>, | ||
206 | ) -> rust_ir::ClosureKind { | ||
207 | // Fn is the closure kind that implements all three traits | ||
208 | rust_ir::ClosureKind::Fn | ||
209 | } | ||
210 | fn closure_inputs_and_output( | ||
211 | &self, | ||
212 | _closure_id: chalk_ir::ClosureId<Interner>, | ||
213 | substs: &chalk_ir::Substitution<Interner>, | ||
214 | ) -> chalk_ir::Binders<rust_ir::FnDefInputsAndOutputDatum<Interner>> { | ||
215 | let sig_ty: Ty = | ||
216 | from_chalk(self.db, substs.at(&Interner, 0).assert_ty_ref(&Interner).clone()); | ||
217 | let sig = FnSig::from_fn_ptr_substs( | ||
218 | &sig_ty.substs().expect("first closure param should be fn ptr"), | ||
219 | false, | ||
220 | ); | ||
221 | let io = rust_ir::FnDefInputsAndOutputDatum { | ||
222 | argument_types: sig.params().iter().map(|ty| ty.clone().to_chalk(self.db)).collect(), | ||
223 | return_type: sig.ret().clone().to_chalk(self.db), | ||
224 | }; | ||
225 | make_binders(io.shifted_in(&Interner), 0) | ||
226 | } | ||
227 | fn closure_upvars( | ||
228 | &self, | ||
229 | _closure_id: chalk_ir::ClosureId<Interner>, | ||
230 | _substs: &chalk_ir::Substitution<Interner>, | ||
231 | ) -> chalk_ir::Binders<chalk_ir::Ty<Interner>> { | ||
232 | let ty = Ty::unit().to_chalk(self.db); | ||
233 | make_binders(ty, 0) | ||
234 | } | ||
235 | fn closure_fn_substitution( | ||
236 | &self, | ||
237 | _closure_id: chalk_ir::ClosureId<Interner>, | ||
238 | _substs: &chalk_ir::Substitution<Interner>, | ||
239 | ) -> chalk_ir::Substitution<Interner> { | ||
240 | Substs::empty().to_chalk(self.db) | ||
241 | } | ||
242 | |||
243 | fn trait_name(&self, _trait_id: chalk_ir::TraitId<Interner>) -> String { | ||
244 | unimplemented!() | ||
245 | } | ||
246 | fn adt_name(&self, _struct_id: chalk_ir::AdtId<Interner>) -> String { | ||
247 | unimplemented!() | ||
248 | } | ||
249 | fn assoc_type_name(&self, _assoc_ty_id: chalk_ir::AssocTypeId<Interner>) -> String { | ||
250 | unimplemented!() | ||
251 | } | ||
252 | fn opaque_type_name(&self, _opaque_ty_id: chalk_ir::OpaqueTyId<Interner>) -> String { | ||
253 | unimplemented!() | ||
254 | } | ||
255 | fn fn_def_name(&self, _fn_def_id: chalk_ir::FnDefId<Interner>) -> String { | ||
256 | unimplemented!() | ||
257 | } | ||
258 | } | ||
259 | |||
260 | pub(crate) fn program_clauses_for_chalk_env_query( | ||
261 | db: &dyn HirDatabase, | ||
262 | krate: CrateId, | ||
263 | environment: chalk_ir::Environment<Interner>, | ||
264 | ) -> chalk_ir::ProgramClauses<Interner> { | ||
265 | chalk_solve::program_clauses_for_env(&ChalkContext { db, krate }, &environment) | ||
266 | } | ||
267 | |||
268 | pub(crate) fn associated_ty_data_query( | ||
269 | db: &dyn HirDatabase, | ||
270 | id: AssocTypeId, | ||
271 | ) -> Arc<AssociatedTyDatum> { | ||
272 | debug!("associated_ty_data {:?}", id); | ||
273 | let type_alias: TypeAliasId = from_chalk(db, id); | ||
274 | let trait_ = match type_alias.lookup(db.upcast()).container { | ||
275 | AssocContainerId::TraitId(t) => t, | ||
276 | _ => panic!("associated type not in trait"), | ||
277 | }; | ||
278 | |||
279 | // Lower bounds -- we could/should maybe move this to a separate query in `lower` | ||
280 | let type_alias_data = db.type_alias_data(type_alias); | ||
281 | let generic_params = generics(db.upcast(), type_alias.into()); | ||
282 | let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST); | ||
283 | let resolver = hir_def::resolver::HasResolver::resolver(type_alias, db.upcast()); | ||
284 | let ctx = crate::TyLoweringContext::new(db, &resolver) | ||
285 | .with_type_param_mode(crate::lower::TypeParamLoweringMode::Variable); | ||
286 | let self_ty = Ty::Bound(crate::BoundVar::new(crate::DebruijnIndex::INNERMOST, 0)); | ||
287 | let bounds = type_alias_data | ||
288 | .bounds | ||
289 | .iter() | ||
290 | .flat_map(|bound| GenericPredicate::from_type_bound(&ctx, bound, self_ty.clone())) | ||
291 | .filter_map(|pred| generic_predicate_to_inline_bound(db, &pred, &self_ty)) | ||
292 | .map(|bound| make_binders(bound.shifted_in(&Interner), 0)) | ||
293 | .collect(); | ||
294 | |||
295 | let where_clauses = convert_where_clauses(db, type_alias.into(), &bound_vars); | ||
296 | let bound_data = rust_ir::AssociatedTyDatumBound { bounds, where_clauses }; | ||
297 | let datum = AssociatedTyDatum { | ||
298 | trait_id: trait_.to_chalk(db), | ||
299 | id, | ||
300 | name: type_alias, | ||
301 | binders: make_binders(bound_data, generic_params.len()), | ||
302 | }; | ||
303 | Arc::new(datum) | ||
304 | } | ||
305 | |||
306 | pub(crate) fn trait_datum_query( | ||
307 | db: &dyn HirDatabase, | ||
308 | krate: CrateId, | ||
309 | trait_id: TraitId, | ||
310 | ) -> Arc<TraitDatum> { | ||
311 | debug!("trait_datum {:?}", trait_id); | ||
312 | let trait_: hir_def::TraitId = from_chalk(db, trait_id); | ||
313 | let trait_data = db.trait_data(trait_); | ||
314 | debug!("trait {:?} = {:?}", trait_id, trait_data.name); | ||
315 | let generic_params = generics(db.upcast(), trait_.into()); | ||
316 | let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST); | ||
317 | let flags = rust_ir::TraitFlags { | ||
318 | auto: trait_data.auto, | ||
319 | upstream: trait_.lookup(db.upcast()).container.module(db.upcast()).krate != krate, | ||
320 | non_enumerable: true, | ||
321 | coinductive: false, // only relevant for Chalk testing | ||
322 | // FIXME: set these flags correctly | ||
323 | marker: false, | ||
324 | fundamental: false, | ||
325 | }; | ||
326 | let where_clauses = convert_where_clauses(db, trait_.into(), &bound_vars); | ||
327 | let associated_ty_ids = | ||
328 | trait_data.associated_types().map(|type_alias| type_alias.to_chalk(db)).collect(); | ||
329 | let trait_datum_bound = rust_ir::TraitDatumBound { where_clauses }; | ||
330 | let well_known = | ||
331 | lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name)); | ||
332 | let trait_datum = TraitDatum { | ||
333 | id: trait_id, | ||
334 | binders: make_binders(trait_datum_bound, bound_vars.len()), | ||
335 | flags, | ||
336 | associated_ty_ids, | ||
337 | well_known, | ||
338 | }; | ||
339 | Arc::new(trait_datum) | ||
340 | } | ||
341 | |||
342 | fn well_known_trait_from_lang_attr(name: &str) -> Option<WellKnownTrait> { | ||
343 | Some(match name { | ||
344 | "sized" => WellKnownTrait::Sized, | ||
345 | "copy" => WellKnownTrait::Copy, | ||
346 | "clone" => WellKnownTrait::Clone, | ||
347 | "drop" => WellKnownTrait::Drop, | ||
348 | "fn_once" => WellKnownTrait::FnOnce, | ||
349 | "fn_mut" => WellKnownTrait::FnMut, | ||
350 | "fn" => WellKnownTrait::Fn, | ||
351 | "unsize" => WellKnownTrait::Unsize, | ||
352 | _ => return None, | ||
353 | }) | ||
354 | } | ||
355 | |||
356 | fn lang_attr_from_well_known_trait(attr: WellKnownTrait) -> &'static str { | ||
357 | match attr { | ||
358 | WellKnownTrait::Sized => "sized", | ||
359 | WellKnownTrait::Copy => "copy", | ||
360 | WellKnownTrait::Clone => "clone", | ||
361 | WellKnownTrait::Drop => "drop", | ||
362 | WellKnownTrait::FnOnce => "fn_once", | ||
363 | WellKnownTrait::FnMut => "fn_mut", | ||
364 | WellKnownTrait::Fn => "fn", | ||
365 | WellKnownTrait::Unsize => "unsize", | ||
366 | } | ||
367 | } | ||
368 | |||
369 | pub(crate) fn struct_datum_query( | ||
370 | db: &dyn HirDatabase, | ||
371 | krate: CrateId, | ||
372 | struct_id: AdtId, | ||
373 | ) -> Arc<StructDatum> { | ||
374 | debug!("struct_datum {:?}", struct_id); | ||
375 | let type_ctor: TypeCtor = from_chalk(db, TypeName::Adt(struct_id)); | ||
376 | debug!("struct {:?} = {:?}", struct_id, type_ctor); | ||
377 | let num_params = type_ctor.num_ty_params(db); | ||
378 | let upstream = type_ctor.krate(db) != Some(krate); | ||
379 | let where_clauses = type_ctor | ||
380 | .as_generic_def() | ||
381 | .map(|generic_def| { | ||
382 | let generic_params = generics(db.upcast(), generic_def); | ||
383 | let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST); | ||
384 | convert_where_clauses(db, generic_def, &bound_vars) | ||
385 | }) | ||
386 | .unwrap_or_else(Vec::new); | ||
387 | let flags = rust_ir::AdtFlags { | ||
388 | upstream, | ||
389 | // FIXME set fundamental and phantom_data flags correctly | ||
390 | fundamental: false, | ||
391 | phantom_data: false, | ||
392 | }; | ||
393 | // FIXME provide enum variants properly (for auto traits) | ||
394 | let variant = rust_ir::AdtVariantDatum { | ||
395 | fields: Vec::new(), // FIXME add fields (only relevant for auto traits), | ||
396 | }; | ||
397 | let struct_datum_bound = rust_ir::AdtDatumBound { variants: vec![variant], where_clauses }; | ||
398 | let struct_datum = StructDatum { | ||
399 | // FIXME set ADT kind | ||
400 | kind: rust_ir::AdtKind::Struct, | ||
401 | id: struct_id, | ||
402 | binders: make_binders(struct_datum_bound, num_params), | ||
403 | flags, | ||
404 | }; | ||
405 | Arc::new(struct_datum) | ||
406 | } | ||
407 | |||
408 | pub(crate) fn impl_datum_query( | ||
409 | db: &dyn HirDatabase, | ||
410 | krate: CrateId, | ||
411 | impl_id: ImplId, | ||
412 | ) -> Arc<ImplDatum> { | ||
413 | let _p = profile::span("impl_datum"); | ||
414 | debug!("impl_datum {:?}", impl_id); | ||
415 | let impl_: hir_def::ImplId = from_chalk(db, impl_id); | ||
416 | impl_def_datum(db, krate, impl_id, impl_) | ||
417 | } | ||
418 | |||
419 | fn impl_def_datum( | ||
420 | db: &dyn HirDatabase, | ||
421 | krate: CrateId, | ||
422 | chalk_id: ImplId, | ||
423 | impl_id: hir_def::ImplId, | ||
424 | ) -> Arc<ImplDatum> { | ||
425 | let trait_ref = db | ||
426 | .impl_trait(impl_id) | ||
427 | // ImplIds for impls where the trait ref can't be resolved should never reach Chalk | ||
428 | .expect("invalid impl passed to Chalk") | ||
429 | .value; | ||
430 | let impl_data = db.impl_data(impl_id); | ||
431 | |||
432 | let generic_params = generics(db.upcast(), impl_id.into()); | ||
433 | let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST); | ||
434 | let trait_ = trait_ref.trait_; | ||
435 | let impl_type = if impl_id.lookup(db.upcast()).container.module(db.upcast()).krate == krate { | ||
436 | rust_ir::ImplType::Local | ||
437 | } else { | ||
438 | rust_ir::ImplType::External | ||
439 | }; | ||
440 | let where_clauses = convert_where_clauses(db, impl_id.into(), &bound_vars); | ||
441 | let negative = impl_data.is_negative; | ||
442 | debug!( | ||
443 | "impl {:?}: {}{} where {:?}", | ||
444 | chalk_id, | ||
445 | if negative { "!" } else { "" }, | ||
446 | trait_ref.display(db), | ||
447 | where_clauses | ||
448 | ); | ||
449 | let trait_ref = trait_ref.to_chalk(db); | ||
450 | |||
451 | let polarity = if negative { rust_ir::Polarity::Negative } else { rust_ir::Polarity::Positive }; | ||
452 | |||
453 | let impl_datum_bound = rust_ir::ImplDatumBound { trait_ref, where_clauses }; | ||
454 | let trait_data = db.trait_data(trait_); | ||
455 | let associated_ty_value_ids = impl_data | ||
456 | .items | ||
457 | .iter() | ||
458 | .filter_map(|item| match item { | ||
459 | AssocItemId::TypeAliasId(type_alias) => Some(*type_alias), | ||
460 | _ => None, | ||
461 | }) | ||
462 | .filter(|&type_alias| { | ||
463 | // don't include associated types that don't exist in the trait | ||
464 | let name = &db.type_alias_data(type_alias).name; | ||
465 | trait_data.associated_type_by_name(name).is_some() | ||
466 | }) | ||
467 | .map(|type_alias| TypeAliasAsValue(type_alias).to_chalk(db)) | ||
468 | .collect(); | ||
469 | debug!("impl_datum: {:?}", impl_datum_bound); | ||
470 | let impl_datum = ImplDatum { | ||
471 | binders: make_binders(impl_datum_bound, bound_vars.len()), | ||
472 | impl_type, | ||
473 | polarity, | ||
474 | associated_ty_value_ids, | ||
475 | }; | ||
476 | Arc::new(impl_datum) | ||
477 | } | ||
478 | |||
479 | pub(crate) fn associated_ty_value_query( | ||
480 | db: &dyn HirDatabase, | ||
481 | krate: CrateId, | ||
482 | id: AssociatedTyValueId, | ||
483 | ) -> Arc<AssociatedTyValue> { | ||
484 | let type_alias: TypeAliasAsValue = from_chalk(db, id); | ||
485 | type_alias_associated_ty_value(db, krate, type_alias.0) | ||
486 | } | ||
487 | |||
488 | fn type_alias_associated_ty_value( | ||
489 | db: &dyn HirDatabase, | ||
490 | _krate: CrateId, | ||
491 | type_alias: TypeAliasId, | ||
492 | ) -> Arc<AssociatedTyValue> { | ||
493 | let type_alias_data = db.type_alias_data(type_alias); | ||
494 | let impl_id = match type_alias.lookup(db.upcast()).container { | ||
495 | AssocContainerId::ImplId(it) => it, | ||
496 | _ => panic!("assoc ty value should be in impl"), | ||
497 | }; | ||
498 | |||
499 | let trait_ref = db.impl_trait(impl_id).expect("assoc ty value should not exist").value; // we don't return any assoc ty values if the impl'd trait can't be resolved | ||
500 | |||
501 | let assoc_ty = db | ||
502 | .trait_data(trait_ref.trait_) | ||
503 | .associated_type_by_name(&type_alias_data.name) | ||
504 | .expect("assoc ty value should not exist"); // validated when building the impl data as well | ||
505 | let ty = db.ty(type_alias.into()); | ||
506 | let value_bound = rust_ir::AssociatedTyValueBound { ty: ty.value.to_chalk(db) }; | ||
507 | let value = rust_ir::AssociatedTyValue { | ||
508 | impl_id: impl_id.to_chalk(db), | ||
509 | associated_ty_id: assoc_ty.to_chalk(db), | ||
510 | value: make_binders(value_bound, ty.num_binders), | ||
511 | }; | ||
512 | Arc::new(value) | ||
513 | } | ||
514 | |||
515 | pub(crate) fn fn_def_datum_query( | ||
516 | db: &dyn HirDatabase, | ||
517 | _krate: CrateId, | ||
518 | fn_def_id: FnDefId, | ||
519 | ) -> Arc<FnDefDatum> { | ||
520 | let callable_def: CallableDefId = from_chalk(db, fn_def_id); | ||
521 | let generic_params = generics(db.upcast(), callable_def.into()); | ||
522 | let sig = db.callable_item_signature(callable_def); | ||
523 | let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST); | ||
524 | let where_clauses = convert_where_clauses(db, callable_def.into(), &bound_vars); | ||
525 | let bound = rust_ir::FnDefDatumBound { | ||
526 | // Note: Chalk doesn't actually use this information yet as far as I am aware, but we provide it anyway | ||
527 | inputs_and_output: make_binders( | ||
528 | rust_ir::FnDefInputsAndOutputDatum { | ||
529 | argument_types: sig | ||
530 | .value | ||
531 | .params() | ||
532 | .iter() | ||
533 | .map(|ty| ty.clone().to_chalk(db)) | ||
534 | .collect(), | ||
535 | return_type: sig.value.ret().clone().to_chalk(db), | ||
536 | } | ||
537 | .shifted_in(&Interner), | ||
538 | 0, | ||
539 | ), | ||
540 | where_clauses, | ||
541 | }; | ||
542 | let datum = FnDefDatum { | ||
543 | id: fn_def_id, | ||
544 | abi: (), | ||
545 | safety: chalk_ir::Safety::Safe, | ||
546 | variadic: sig.value.is_varargs, | ||
547 | binders: make_binders(bound, sig.num_binders), | ||
548 | }; | ||
549 | Arc::new(datum) | ||
550 | } | ||
551 | |||
552 | impl From<FnDefId> for crate::db::InternedCallableDefId { | ||
553 | fn from(fn_def_id: FnDefId) -> Self { | ||
554 | InternKey::from_intern_id(fn_def_id.0) | ||
555 | } | ||
556 | } | ||
557 | |||
558 | impl From<crate::db::InternedCallableDefId> for FnDefId { | ||
559 | fn from(callable_def_id: crate::db::InternedCallableDefId) -> Self { | ||
560 | chalk_ir::FnDefId(callable_def_id.as_intern_id()) | ||
561 | } | ||
562 | } | ||
563 | |||
564 | impl From<OpaqueTyId> for crate::db::InternedOpaqueTyId { | ||
565 | fn from(id: OpaqueTyId) -> Self { | ||
566 | InternKey::from_intern_id(id.0) | ||
567 | } | ||
568 | } | ||
569 | |||
570 | impl From<crate::db::InternedOpaqueTyId> for OpaqueTyId { | ||
571 | fn from(id: crate::db::InternedOpaqueTyId) -> Self { | ||
572 | chalk_ir::OpaqueTyId(id.as_intern_id()) | ||
573 | } | ||
574 | } | ||
575 | |||
576 | impl From<chalk_ir::ClosureId<Interner>> for crate::db::ClosureId { | ||
577 | fn from(id: chalk_ir::ClosureId<Interner>) -> Self { | ||
578 | Self::from_intern_id(id.0) | ||
579 | } | ||
580 | } | ||
581 | |||
582 | impl From<crate::db::ClosureId> for chalk_ir::ClosureId<Interner> { | ||
583 | fn from(id: crate::db::ClosureId) -> Self { | ||
584 | chalk_ir::ClosureId(id.as_intern_id()) | ||
585 | } | ||
586 | } | ||