diff options
Diffstat (limited to 'crates/ra_hir_ty/src/traits')
-rw-r--r-- | crates/ra_hir_ty/src/traits/builtin.rs | 178 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/traits/chalk.rs | 99 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/traits/chalk/interner.rs | 14 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/traits/chalk/mapping.rs | 104 |
4 files changed, 84 insertions, 311 deletions
diff --git a/crates/ra_hir_ty/src/traits/builtin.rs b/crates/ra_hir_ty/src/traits/builtin.rs deleted file mode 100644 index 60cc9a9f5..000000000 --- a/crates/ra_hir_ty/src/traits/builtin.rs +++ /dev/null | |||
@@ -1,178 +0,0 @@ | |||
1 | //! This module provides the built-in trait implementations, e.g. to make | ||
2 | //! closures implement `Fn`. | ||
3 | use hir_def::{expr::Expr, TraitId, TypeAliasId}; | ||
4 | use hir_expand::name::name; | ||
5 | use ra_db::CrateId; | ||
6 | |||
7 | use super::{AssocTyValue, Impl}; | ||
8 | use crate::{ | ||
9 | db::HirDatabase, ApplicationTy, BoundVar, DebruijnIndex, Substs, TraitRef, Ty, TypeCtor, | ||
10 | }; | ||
11 | |||
12 | pub(super) struct BuiltinImplData { | ||
13 | pub num_vars: usize, | ||
14 | pub trait_ref: TraitRef, | ||
15 | pub where_clauses: Vec<super::GenericPredicate>, | ||
16 | pub assoc_ty_values: Vec<AssocTyValue>, | ||
17 | } | ||
18 | |||
19 | pub(super) struct BuiltinImplAssocTyValueData { | ||
20 | pub impl_: Impl, | ||
21 | pub assoc_ty_id: TypeAliasId, | ||
22 | pub num_vars: usize, | ||
23 | pub value: Ty, | ||
24 | } | ||
25 | |||
26 | pub(super) fn get_builtin_impls( | ||
27 | db: &dyn HirDatabase, | ||
28 | krate: CrateId, | ||
29 | ty: &Ty, | ||
30 | // The first argument for the trait, if present | ||
31 | _arg: &Option<Ty>, | ||
32 | trait_: TraitId, | ||
33 | mut callback: impl FnMut(Impl), | ||
34 | ) { | ||
35 | // Note: since impl_datum needs to be infallible, we need to make sure here | ||
36 | // that we have all prerequisites to build the respective impls. | ||
37 | if let Ty::Apply(ApplicationTy { ctor: TypeCtor::Closure { def, expr }, .. }) = ty { | ||
38 | for &fn_trait in [super::FnTrait::FnOnce, super::FnTrait::FnMut, super::FnTrait::Fn].iter() | ||
39 | { | ||
40 | if let Some(actual_trait) = fn_trait.get_id(db, krate) { | ||
41 | if trait_ == actual_trait { | ||
42 | let impl_ = super::ClosureFnTraitImplData { def: *def, expr: *expr, fn_trait }; | ||
43 | if check_closure_fn_trait_impl_prerequisites(db, krate, impl_) { | ||
44 | callback(Impl::ClosureFnTraitImpl(impl_)); | ||
45 | } | ||
46 | } | ||
47 | } | ||
48 | } | ||
49 | } | ||
50 | } | ||
51 | |||
52 | pub(super) fn impl_datum(db: &dyn HirDatabase, krate: CrateId, impl_: Impl) -> BuiltinImplData { | ||
53 | match impl_ { | ||
54 | Impl::ImplDef(_) => unreachable!(), | ||
55 | Impl::ClosureFnTraitImpl(data) => closure_fn_trait_impl_datum(db, krate, data), | ||
56 | } | ||
57 | } | ||
58 | |||
59 | pub(super) fn associated_ty_value( | ||
60 | db: &dyn HirDatabase, | ||
61 | krate: CrateId, | ||
62 | data: AssocTyValue, | ||
63 | ) -> BuiltinImplAssocTyValueData { | ||
64 | match data { | ||
65 | AssocTyValue::TypeAlias(_) => unreachable!(), | ||
66 | AssocTyValue::ClosureFnTraitImplOutput(data) => { | ||
67 | closure_fn_trait_output_assoc_ty_value(db, krate, data) | ||
68 | } | ||
69 | } | ||
70 | } | ||
71 | |||
72 | // Closure Fn trait impls | ||
73 | |||
74 | fn check_closure_fn_trait_impl_prerequisites( | ||
75 | db: &dyn HirDatabase, | ||
76 | krate: CrateId, | ||
77 | data: super::ClosureFnTraitImplData, | ||
78 | ) -> bool { | ||
79 | // the respective Fn/FnOnce/FnMut trait needs to exist | ||
80 | if data.fn_trait.get_id(db, krate).is_none() { | ||
81 | return false; | ||
82 | } | ||
83 | |||
84 | // FIXME: there are more assumptions that we should probably check here: | ||
85 | // the traits having no type params, FnOnce being a supertrait | ||
86 | |||
87 | // the FnOnce trait needs to exist and have an assoc type named Output | ||
88 | let fn_once_trait = match (super::FnTrait::FnOnce).get_id(db, krate) { | ||
89 | Some(t) => t, | ||
90 | None => return false, | ||
91 | }; | ||
92 | db.trait_data(fn_once_trait).associated_type_by_name(&name![Output]).is_some() | ||
93 | } | ||
94 | |||
95 | fn closure_fn_trait_impl_datum( | ||
96 | db: &dyn HirDatabase, | ||
97 | krate: CrateId, | ||
98 | data: super::ClosureFnTraitImplData, | ||
99 | ) -> BuiltinImplData { | ||
100 | // for some closure |X, Y| -> Z: | ||
101 | // impl<T, U, V> Fn<(T, U)> for closure<fn(T, U) -> V> { Output = V } | ||
102 | |||
103 | let trait_ = data | ||
104 | .fn_trait | ||
105 | .get_id(db, krate) // get corresponding fn trait | ||
106 | // the existence of the Fn trait has been checked before | ||
107 | .expect("fn trait for closure impl missing"); | ||
108 | |||
109 | let num_args: u16 = match &db.body(data.def)[data.expr] { | ||
110 | Expr::Lambda { args, .. } => args.len() as u16, | ||
111 | _ => { | ||
112 | log::warn!("closure for closure type {:?} not found", data); | ||
113 | 0 | ||
114 | } | ||
115 | }; | ||
116 | |||
117 | let arg_ty = Ty::apply( | ||
118 | TypeCtor::Tuple { cardinality: num_args }, | ||
119 | Substs::builder(num_args as usize) | ||
120 | .fill_with_bound_vars(DebruijnIndex::INNERMOST, 0) | ||
121 | .build(), | ||
122 | ); | ||
123 | let sig_ty = Ty::apply( | ||
124 | TypeCtor::FnPtr { num_args, is_varargs: false }, | ||
125 | Substs::builder(num_args as usize + 1) | ||
126 | .fill_with_bound_vars(DebruijnIndex::INNERMOST, 0) | ||
127 | .build(), | ||
128 | ); | ||
129 | |||
130 | let self_ty = Ty::apply_one(TypeCtor::Closure { def: data.def, expr: data.expr }, sig_ty); | ||
131 | |||
132 | let trait_ref = TraitRef { | ||
133 | trait_, | ||
134 | substs: Substs::build_for_def(db, trait_).push(self_ty).push(arg_ty).build(), | ||
135 | }; | ||
136 | |||
137 | let output_ty_id = AssocTyValue::ClosureFnTraitImplOutput(data); | ||
138 | |||
139 | BuiltinImplData { | ||
140 | num_vars: num_args as usize + 1, | ||
141 | trait_ref, | ||
142 | where_clauses: Vec::new(), | ||
143 | assoc_ty_values: vec![output_ty_id], | ||
144 | } | ||
145 | } | ||
146 | |||
147 | fn closure_fn_trait_output_assoc_ty_value( | ||
148 | db: &dyn HirDatabase, | ||
149 | krate: CrateId, | ||
150 | data: super::ClosureFnTraitImplData, | ||
151 | ) -> BuiltinImplAssocTyValueData { | ||
152 | let impl_ = Impl::ClosureFnTraitImpl(data); | ||
153 | |||
154 | let num_args: u16 = match &db.body(data.def)[data.expr] { | ||
155 | Expr::Lambda { args, .. } => args.len() as u16, | ||
156 | _ => { | ||
157 | log::warn!("closure for closure type {:?} not found", data); | ||
158 | 0 | ||
159 | } | ||
160 | }; | ||
161 | |||
162 | let output_ty = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, num_args.into())); | ||
163 | |||
164 | let fn_once_trait = | ||
165 | (super::FnTrait::FnOnce).get_id(db, krate).expect("assoc ty value should not exist"); | ||
166 | |||
167 | let output_ty_id = db | ||
168 | .trait_data(fn_once_trait) | ||
169 | .associated_type_by_name(&name![Output]) | ||
170 | .expect("assoc ty value should not exist"); | ||
171 | |||
172 | BuiltinImplAssocTyValueData { | ||
173 | impl_, | ||
174 | assoc_ty_id: output_ty_id, | ||
175 | num_vars: num_args as usize + 1, | ||
176 | value: output_ty, | ||
177 | } | ||
178 | } | ||
diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs index c448aea65..1ef5baa05 100644 --- a/crates/ra_hir_ty/src/traits/chalk.rs +++ b/crates/ra_hir_ty/src/traits/chalk.rs | |||
@@ -12,15 +12,17 @@ use hir_def::{ | |||
12 | }; | 12 | }; |
13 | use ra_db::{salsa::InternKey, CrateId}; | 13 | use ra_db::{salsa::InternKey, CrateId}; |
14 | 14 | ||
15 | use super::{builtin, AssocTyValue, ChalkContext, Impl}; | 15 | use super::ChalkContext; |
16 | use crate::{ | 16 | use crate::{ |
17 | db::HirDatabase, | 17 | db::HirDatabase, |
18 | display::HirDisplay, | 18 | display::HirDisplay, |
19 | method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS}, | 19 | method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS}, |
20 | utils::generics, | 20 | utils::generics, |
21 | CallableDef, DebruijnIndex, GenericPredicate, Substs, Ty, TypeCtor, | 21 | CallableDef, DebruijnIndex, FnSig, GenericPredicate, Substs, Ty, TypeCtor, |
22 | }; | ||
23 | use mapping::{ | ||
24 | convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue, | ||
22 | }; | 25 | }; |
23 | use mapping::{convert_where_clauses, generic_predicate_to_inline_bound, make_binders}; | ||
24 | 26 | ||
25 | pub use self::interner::*; | 27 | pub use self::interner::*; |
26 | 28 | ||
@@ -102,9 +104,9 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
102 | let in_self = self.db.trait_impls_in_crate(self.krate); | 104 | let in_self = self.db.trait_impls_in_crate(self.krate); |
103 | let impl_maps = [in_deps, in_self]; | 105 | let impl_maps = [in_deps, in_self]; |
104 | 106 | ||
105 | let id_to_chalk = |id: hir_def::ImplId| Impl::ImplDef(id).to_chalk(self.db); | 107 | let id_to_chalk = |id: hir_def::ImplId| id.to_chalk(self.db); |
106 | 108 | ||
107 | let mut result: Vec<_> = if fps.is_empty() { | 109 | let result: Vec<_> = if fps.is_empty() { |
108 | debug!("Unrestricted search for {:?} impls...", trait_); | 110 | debug!("Unrestricted search for {:?} impls...", trait_); |
109 | impl_maps | 111 | impl_maps |
110 | .iter() | 112 | .iter() |
@@ -121,13 +123,6 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
121 | .collect() | 123 | .collect() |
122 | }; | 124 | }; |
123 | 125 | ||
124 | let arg: Option<Ty> = | ||
125 | parameters.get(1).map(|p| from_chalk(self.db, p.assert_ty_ref(&Interner).clone())); | ||
126 | |||
127 | builtin::get_builtin_impls(self.db, self.krate, &ty, &arg, trait_, |i| { | ||
128 | result.push(i.to_chalk(self.db)) | ||
129 | }); | ||
130 | |||
131 | debug!("impls_for_trait returned {} impls", result.len()); | 126 | debug!("impls_for_trait returned {} impls", result.len()); |
132 | result | 127 | result |
133 | } | 128 | } |
@@ -217,32 +212,40 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
217 | _closure_id: chalk_ir::ClosureId<Interner>, | 212 | _closure_id: chalk_ir::ClosureId<Interner>, |
218 | _substs: &chalk_ir::Substitution<Interner>, | 213 | _substs: &chalk_ir::Substitution<Interner>, |
219 | ) -> rust_ir::ClosureKind { | 214 | ) -> rust_ir::ClosureKind { |
220 | // FIXME: implement closure support | 215 | // Fn is the closure kind that implements all three traits |
221 | unimplemented!() | 216 | rust_ir::ClosureKind::Fn |
222 | } | 217 | } |
223 | fn closure_inputs_and_output( | 218 | fn closure_inputs_and_output( |
224 | &self, | 219 | &self, |
225 | _closure_id: chalk_ir::ClosureId<Interner>, | 220 | _closure_id: chalk_ir::ClosureId<Interner>, |
226 | _substs: &chalk_ir::Substitution<Interner>, | 221 | substs: &chalk_ir::Substitution<Interner>, |
227 | ) -> chalk_ir::Binders<rust_ir::FnDefInputsAndOutputDatum<Interner>> { | 222 | ) -> chalk_ir::Binders<rust_ir::FnDefInputsAndOutputDatum<Interner>> { |
228 | // FIXME: implement closure support | 223 | let sig_ty: Ty = |
229 | unimplemented!() | 224 | from_chalk(self.db, substs.at(&Interner, 0).assert_ty_ref(&Interner).clone()); |
225 | let sig = FnSig::from_fn_ptr_substs( | ||
226 | &sig_ty.substs().expect("first closure param should be fn ptr"), | ||
227 | false, | ||
228 | ); | ||
229 | let io = rust_ir::FnDefInputsAndOutputDatum { | ||
230 | argument_types: sig.params().iter().map(|ty| ty.clone().to_chalk(self.db)).collect(), | ||
231 | return_type: sig.ret().clone().to_chalk(self.db), | ||
232 | }; | ||
233 | make_binders(io.shifted_in(&Interner), 0) | ||
230 | } | 234 | } |
231 | fn closure_upvars( | 235 | fn closure_upvars( |
232 | &self, | 236 | &self, |
233 | _closure_id: chalk_ir::ClosureId<Interner>, | 237 | _closure_id: chalk_ir::ClosureId<Interner>, |
234 | _substs: &chalk_ir::Substitution<Interner>, | 238 | _substs: &chalk_ir::Substitution<Interner>, |
235 | ) -> chalk_ir::Binders<chalk_ir::Ty<Interner>> { | 239 | ) -> chalk_ir::Binders<chalk_ir::Ty<Interner>> { |
236 | // FIXME: implement closure support | 240 | let ty = Ty::unit().to_chalk(self.db); |
237 | unimplemented!() | 241 | make_binders(ty, 0) |
238 | } | 242 | } |
239 | fn closure_fn_substitution( | 243 | fn closure_fn_substitution( |
240 | &self, | 244 | &self, |
241 | _closure_id: chalk_ir::ClosureId<Interner>, | 245 | _closure_id: chalk_ir::ClosureId<Interner>, |
242 | _substs: &chalk_ir::Substitution<Interner>, | 246 | _substs: &chalk_ir::Substitution<Interner>, |
243 | ) -> chalk_ir::Substitution<Interner> { | 247 | ) -> chalk_ir::Substitution<Interner> { |
244 | // FIXME: implement closure support | 248 | Substs::empty().to_chalk(self.db) |
245 | unimplemented!() | ||
246 | } | 249 | } |
247 | 250 | ||
248 | fn trait_name(&self, _trait_id: chalk_ir::TraitId<Interner>) -> String { | 251 | fn trait_name(&self, _trait_id: chalk_ir::TraitId<Interner>) -> String { |
@@ -417,11 +420,8 @@ pub(crate) fn impl_datum_query( | |||
417 | ) -> Arc<ImplDatum> { | 420 | ) -> Arc<ImplDatum> { |
418 | let _p = ra_prof::profile("impl_datum"); | 421 | let _p = ra_prof::profile("impl_datum"); |
419 | debug!("impl_datum {:?}", impl_id); | 422 | debug!("impl_datum {:?}", impl_id); |
420 | let impl_: Impl = from_chalk(db, impl_id); | 423 | let impl_: hir_def::ImplId = from_chalk(db, impl_id); |
421 | match impl_ { | 424 | impl_def_datum(db, krate, impl_id, impl_) |
422 | Impl::ImplDef(impl_def) => impl_def_datum(db, krate, impl_id, impl_def), | ||
423 | _ => Arc::new(builtin::impl_datum(db, krate, impl_).to_chalk(db)), | ||
424 | } | ||
425 | } | 425 | } |
426 | 426 | ||
427 | fn impl_def_datum( | 427 | fn impl_def_datum( |
@@ -472,7 +472,7 @@ fn impl_def_datum( | |||
472 | let name = &db.type_alias_data(type_alias).name; | 472 | let name = &db.type_alias_data(type_alias).name; |
473 | trait_data.associated_type_by_name(name).is_some() | 473 | trait_data.associated_type_by_name(name).is_some() |
474 | }) | 474 | }) |
475 | .map(|type_alias| AssocTyValue::TypeAlias(type_alias).to_chalk(db)) | 475 | .map(|type_alias| TypeAliasAsValue(type_alias).to_chalk(db)) |
476 | .collect(); | 476 | .collect(); |
477 | debug!("impl_datum: {:?}", impl_datum_bound); | 477 | debug!("impl_datum: {:?}", impl_datum_bound); |
478 | let impl_datum = ImplDatum { | 478 | let impl_datum = ImplDatum { |
@@ -489,13 +489,8 @@ pub(crate) fn associated_ty_value_query( | |||
489 | krate: CrateId, | 489 | krate: CrateId, |
490 | id: AssociatedTyValueId, | 490 | id: AssociatedTyValueId, |
491 | ) -> Arc<AssociatedTyValue> { | 491 | ) -> Arc<AssociatedTyValue> { |
492 | let data: AssocTyValue = from_chalk(db, id); | 492 | let type_alias: TypeAliasAsValue = from_chalk(db, id); |
493 | match data { | 493 | type_alias_associated_ty_value(db, krate, type_alias.0) |
494 | AssocTyValue::TypeAlias(type_alias) => { | ||
495 | type_alias_associated_ty_value(db, krate, type_alias) | ||
496 | } | ||
497 | _ => Arc::new(builtin::associated_ty_value(db, krate, data).to_chalk(db)), | ||
498 | } | ||
499 | } | 494 | } |
500 | 495 | ||
501 | fn type_alias_associated_ty_value( | 496 | fn type_alias_associated_ty_value( |
@@ -518,7 +513,7 @@ fn type_alias_associated_ty_value( | |||
518 | let ty = db.ty(type_alias.into()); | 513 | let ty = db.ty(type_alias.into()); |
519 | let value_bound = rust_ir::AssociatedTyValueBound { ty: ty.value.to_chalk(db) }; | 514 | let value_bound = rust_ir::AssociatedTyValueBound { ty: ty.value.to_chalk(db) }; |
520 | let value = rust_ir::AssociatedTyValue { | 515 | let value = rust_ir::AssociatedTyValue { |
521 | impl_id: Impl::ImplDef(impl_id).to_chalk(db), | 516 | impl_id: impl_id.to_chalk(db), |
522 | associated_ty_id: assoc_ty.to_chalk(db), | 517 | associated_ty_id: assoc_ty.to_chalk(db), |
523 | value: make_binders(value_bound, ty.num_binders), | 518 | value: make_binders(value_bound, ty.num_binders), |
524 | }; | 519 | }; |
@@ -557,18 +552,6 @@ pub(crate) fn fn_def_datum_query( | |||
557 | Arc::new(datum) | 552 | Arc::new(datum) |
558 | } | 553 | } |
559 | 554 | ||
560 | impl From<AdtId> for crate::TypeCtorId { | ||
561 | fn from(struct_id: AdtId) -> Self { | ||
562 | struct_id.0 | ||
563 | } | ||
564 | } | ||
565 | |||
566 | impl From<crate::TypeCtorId> for AdtId { | ||
567 | fn from(type_ctor_id: crate::TypeCtorId) -> Self { | ||
568 | chalk_ir::AdtId(type_ctor_id) | ||
569 | } | ||
570 | } | ||
571 | |||
572 | impl From<FnDefId> for crate::CallableDefId { | 555 | impl From<FnDefId> for crate::CallableDefId { |
573 | fn from(fn_def_id: FnDefId) -> Self { | 556 | fn from(fn_def_id: FnDefId) -> Self { |
574 | InternKey::from_intern_id(fn_def_id.0) | 557 | InternKey::from_intern_id(fn_def_id.0) |
@@ -581,18 +564,6 @@ impl From<crate::CallableDefId> for FnDefId { | |||
581 | } | 564 | } |
582 | } | 565 | } |
583 | 566 | ||
584 | impl From<ImplId> for crate::traits::GlobalImplId { | ||
585 | fn from(impl_id: ImplId) -> Self { | ||
586 | InternKey::from_intern_id(impl_id.0) | ||
587 | } | ||
588 | } | ||
589 | |||
590 | impl From<crate::traits::GlobalImplId> for ImplId { | ||
591 | fn from(impl_id: crate::traits::GlobalImplId) -> Self { | ||
592 | chalk_ir::ImplId(impl_id.as_intern_id()) | ||
593 | } | ||
594 | } | ||
595 | |||
596 | impl From<OpaqueTyId> for crate::db::InternedOpaqueTyId { | 567 | impl From<OpaqueTyId> for crate::db::InternedOpaqueTyId { |
597 | fn from(id: OpaqueTyId) -> Self { | 568 | fn from(id: OpaqueTyId) -> Self { |
598 | InternKey::from_intern_id(id.0) | 569 | InternKey::from_intern_id(id.0) |
@@ -605,14 +576,14 @@ impl From<crate::db::InternedOpaqueTyId> for OpaqueTyId { | |||
605 | } | 576 | } |
606 | } | 577 | } |
607 | 578 | ||
608 | impl From<rust_ir::AssociatedTyValueId<Interner>> for crate::traits::AssocTyValueId { | 579 | impl From<chalk_ir::ClosureId<Interner>> for crate::db::ClosureId { |
609 | fn from(id: rust_ir::AssociatedTyValueId<Interner>) -> Self { | 580 | fn from(id: chalk_ir::ClosureId<Interner>) -> Self { |
610 | Self::from_intern_id(id.0) | 581 | Self::from_intern_id(id.0) |
611 | } | 582 | } |
612 | } | 583 | } |
613 | 584 | ||
614 | impl From<crate::traits::AssocTyValueId> for rust_ir::AssociatedTyValueId<Interner> { | 585 | impl From<crate::db::ClosureId> for chalk_ir::ClosureId<Interner> { |
615 | fn from(assoc_ty_value_id: crate::traits::AssocTyValueId) -> Self { | 586 | fn from(id: crate::db::ClosureId) -> Self { |
616 | rust_ir::AssociatedTyValueId(assoc_ty_value_id.as_intern_id()) | 587 | chalk_ir::ClosureId(id.as_intern_id()) |
617 | } | 588 | } |
618 | } | 589 | } |
diff --git a/crates/ra_hir_ty/src/traits/chalk/interner.rs b/crates/ra_hir_ty/src/traits/chalk/interner.rs index 156b691b4..8d4c51a8f 100644 --- a/crates/ra_hir_ty/src/traits/chalk/interner.rs +++ b/crates/ra_hir_ty/src/traits/chalk/interner.rs | |||
@@ -41,7 +41,7 @@ impl chalk_ir::interner::Interner for Interner { | |||
41 | type InternedCanonicalVarKinds = Vec<chalk_ir::CanonicalVarKind<Self>>; | 41 | type InternedCanonicalVarKinds = Vec<chalk_ir::CanonicalVarKind<Self>>; |
42 | type InternedConstraints = Vec<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>>; | 42 | type InternedConstraints = Vec<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>>; |
43 | type DefId = InternId; | 43 | type DefId = InternId; |
44 | type InternedAdtId = crate::TypeCtorId; | 44 | type InternedAdtId = hir_def::AdtId; |
45 | type Identifier = TypeAliasId; | 45 | type Identifier = TypeAliasId; |
46 | type FnAbi = (); | 46 | type FnAbi = (); |
47 | 47 | ||
@@ -364,6 +364,18 @@ impl chalk_ir::interner::Interner for Interner { | |||
364 | ) -> &'a [chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>] { | 364 | ) -> &'a [chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>] { |
365 | constraints | 365 | constraints |
366 | } | 366 | } |
367 | fn debug_closure_id( | ||
368 | _fn_def_id: chalk_ir::ClosureId<Self>, | ||
369 | _fmt: &mut fmt::Formatter<'_>, | ||
370 | ) -> Option<fmt::Result> { | ||
371 | None | ||
372 | } | ||
373 | fn debug_constraints( | ||
374 | _clauses: &chalk_ir::Constraints<Self>, | ||
375 | _fmt: &mut fmt::Formatter<'_>, | ||
376 | ) -> Option<fmt::Result> { | ||
377 | None | ||
378 | } | ||
367 | } | 379 | } |
368 | 380 | ||
369 | impl chalk_ir::interner::HasInterner for Interner { | 381 | impl chalk_ir::interner::HasInterner for Interner { |
diff --git a/crates/ra_hir_ty/src/traits/chalk/mapping.rs b/crates/ra_hir_ty/src/traits/chalk/mapping.rs index 3ebb55f77..a852ce2ac 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs | |||
@@ -15,7 +15,7 @@ use ra_db::salsa::InternKey; | |||
15 | use crate::{ | 15 | use crate::{ |
16 | db::HirDatabase, | 16 | db::HirDatabase, |
17 | primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness}, | 17 | primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness}, |
18 | traits::{builtin, AssocTyValue, Canonical, Impl, Obligation}, | 18 | traits::{Canonical, Obligation}, |
19 | ApplicationTy, CallableDef, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId, | 19 | ApplicationTy, CallableDef, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId, |
20 | ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TyKind, TypeCtor, | 20 | ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TyKind, TypeCtor, |
21 | }; | 21 | }; |
@@ -311,18 +311,24 @@ impl ToChalk for TypeCtor { | |||
311 | } | 311 | } |
312 | TypeCtor::Never => TypeName::Never, | 312 | TypeCtor::Never => TypeName::Never, |
313 | 313 | ||
314 | // FIXME convert these | 314 | TypeCtor::Closure { def, expr } => { |
315 | TypeCtor::Adt(_) | TypeCtor::FnPtr { .. } | TypeCtor::Closure { .. } => { | 315 | let closure_id = db.intern_closure((def, expr)); |
316 | // other TypeCtors get interned and turned into a chalk StructId | 316 | TypeName::Closure(closure_id.into()) |
317 | let struct_id = db.intern_type_ctor(self).into(); | 317 | } |
318 | TypeName::Adt(struct_id) | 318 | |
319 | TypeCtor::Adt(adt_id) => TypeName::Adt(chalk_ir::AdtId(adt_id)), | ||
320 | |||
321 | TypeCtor::FnPtr { .. } => { | ||
322 | // This should not be reached, since Chalk doesn't represent | ||
323 | // function pointers with TypeName | ||
324 | unreachable!() | ||
319 | } | 325 | } |
320 | } | 326 | } |
321 | } | 327 | } |
322 | 328 | ||
323 | fn from_chalk(db: &dyn HirDatabase, type_name: TypeName<Interner>) -> TypeCtor { | 329 | fn from_chalk(db: &dyn HirDatabase, type_name: TypeName<Interner>) -> TypeCtor { |
324 | match type_name { | 330 | match type_name { |
325 | TypeName::Adt(struct_id) => db.lookup_intern_type_ctor(struct_id.into()), | 331 | TypeName::Adt(struct_id) => TypeCtor::Adt(struct_id.0), |
326 | TypeName::AssociatedType(type_id) => TypeCtor::AssociatedType(from_chalk(db, type_id)), | 332 | TypeName::AssociatedType(type_id) => TypeCtor::AssociatedType(from_chalk(db, type_id)), |
327 | TypeName::OpaqueType(opaque_type_id) => { | 333 | TypeName::OpaqueType(opaque_type_id) => { |
328 | TypeCtor::OpaqueType(from_chalk(db, opaque_type_id)) | 334 | TypeCtor::OpaqueType(from_chalk(db, opaque_type_id)) |
@@ -355,13 +361,16 @@ impl ToChalk for TypeCtor { | |||
355 | let callable_def = from_chalk(db, fn_def_id); | 361 | let callable_def = from_chalk(db, fn_def_id); |
356 | TypeCtor::FnDef(callable_def) | 362 | TypeCtor::FnDef(callable_def) |
357 | } | 363 | } |
364 | TypeName::Array => TypeCtor::Array, | ||
358 | 365 | ||
359 | TypeName::Array | TypeName::Error => { | 366 | TypeName::Closure(id) => { |
360 | // this should not be reached, since we don't represent TypeName::Error with TypeCtor | 367 | let id: crate::db::ClosureId = id.into(); |
361 | unreachable!() | 368 | let (def, expr) = db.lookup_intern_closure(id); |
369 | TypeCtor::Closure { def, expr } | ||
362 | } | 370 | } |
363 | TypeName::Closure(_) => { | 371 | |
364 | // FIXME: implement closure support | 372 | TypeName::Error => { |
373 | // this should not be reached, since we don't represent TypeName::Error with TypeCtor | ||
365 | unreachable!() | 374 | unreachable!() |
366 | } | 375 | } |
367 | } | 376 | } |
@@ -433,15 +442,15 @@ impl ToChalk for Mutability { | |||
433 | } | 442 | } |
434 | } | 443 | } |
435 | 444 | ||
436 | impl ToChalk for Impl { | 445 | impl ToChalk for hir_def::ImplId { |
437 | type Chalk = ImplId; | 446 | type Chalk = ImplId; |
438 | 447 | ||
439 | fn to_chalk(self, db: &dyn HirDatabase) -> ImplId { | 448 | fn to_chalk(self, _db: &dyn HirDatabase) -> ImplId { |
440 | db.intern_chalk_impl(self).into() | 449 | chalk_ir::ImplId(self.as_intern_id()) |
441 | } | 450 | } |
442 | 451 | ||
443 | fn from_chalk(db: &dyn HirDatabase, impl_id: ImplId) -> Impl { | 452 | fn from_chalk(_db: &dyn HirDatabase, impl_id: ImplId) -> hir_def::ImplId { |
444 | db.lookup_intern_chalk_impl(impl_id.into()) | 453 | InternKey::from_intern_id(impl_id.0) |
445 | } | 454 | } |
446 | } | 455 | } |
447 | 456 | ||
@@ -469,15 +478,20 @@ impl ToChalk for TypeAliasId { | |||
469 | } | 478 | } |
470 | } | 479 | } |
471 | 480 | ||
472 | impl ToChalk for AssocTyValue { | 481 | pub struct TypeAliasAsValue(pub TypeAliasId); |
482 | |||
483 | impl ToChalk for TypeAliasAsValue { | ||
473 | type Chalk = AssociatedTyValueId; | 484 | type Chalk = AssociatedTyValueId; |
474 | 485 | ||
475 | fn to_chalk(self, db: &dyn HirDatabase) -> AssociatedTyValueId { | 486 | fn to_chalk(self, _db: &dyn HirDatabase) -> AssociatedTyValueId { |
476 | db.intern_assoc_ty_value(self).into() | 487 | rust_ir::AssociatedTyValueId(self.0.as_intern_id()) |
477 | } | 488 | } |
478 | 489 | ||
479 | fn from_chalk(db: &dyn HirDatabase, assoc_ty_value_id: AssociatedTyValueId) -> AssocTyValue { | 490 | fn from_chalk( |
480 | db.lookup_intern_assoc_ty_value(assoc_ty_value_id.into()) | 491 | _db: &dyn HirDatabase, |
492 | assoc_ty_value_id: AssociatedTyValueId, | ||
493 | ) -> TypeAliasAsValue { | ||
494 | TypeAliasAsValue(TypeAliasId::from_intern_id(assoc_ty_value_id.0)) | ||
481 | } | 495 | } |
482 | } | 496 | } |
483 | 497 | ||
@@ -686,52 +700,6 @@ where | |||
686 | } | 700 | } |
687 | } | 701 | } |
688 | 702 | ||
689 | impl ToChalk for builtin::BuiltinImplData { | ||
690 | type Chalk = ImplDatum; | ||
691 | |||
692 | fn to_chalk(self, db: &dyn HirDatabase) -> ImplDatum { | ||
693 | let impl_type = rust_ir::ImplType::External; | ||
694 | let where_clauses = self.where_clauses.into_iter().map(|w| w.to_chalk(db)).collect(); | ||
695 | |||
696 | let impl_datum_bound = | ||
697 | rust_ir::ImplDatumBound { trait_ref: self.trait_ref.to_chalk(db), where_clauses }; | ||
698 | let associated_ty_value_ids = | ||
699 | self.assoc_ty_values.into_iter().map(|v| v.to_chalk(db)).collect(); | ||
700 | rust_ir::ImplDatum { | ||
701 | binders: make_binders(impl_datum_bound, self.num_vars), | ||
702 | impl_type, | ||
703 | polarity: rust_ir::Polarity::Positive, | ||
704 | associated_ty_value_ids, | ||
705 | } | ||
706 | } | ||
707 | |||
708 | fn from_chalk(_db: &dyn HirDatabase, _data: ImplDatum) -> Self { | ||
709 | unimplemented!() | ||
710 | } | ||
711 | } | ||
712 | |||
713 | impl ToChalk for builtin::BuiltinImplAssocTyValueData { | ||
714 | type Chalk = AssociatedTyValue; | ||
715 | |||
716 | fn to_chalk(self, db: &dyn HirDatabase) -> AssociatedTyValue { | ||
717 | let ty = self.value.to_chalk(db); | ||
718 | let value_bound = rust_ir::AssociatedTyValueBound { ty }; | ||
719 | |||
720 | rust_ir::AssociatedTyValue { | ||
721 | associated_ty_id: self.assoc_ty_id.to_chalk(db), | ||
722 | impl_id: self.impl_.to_chalk(db), | ||
723 | value: make_binders(value_bound, self.num_vars), | ||
724 | } | ||
725 | } | ||
726 | |||
727 | fn from_chalk( | ||
728 | _db: &dyn HirDatabase, | ||
729 | _data: AssociatedTyValue, | ||
730 | ) -> builtin::BuiltinImplAssocTyValueData { | ||
731 | unimplemented!() | ||
732 | } | ||
733 | } | ||
734 | |||
735 | pub(super) fn make_binders<T>(value: T, num_vars: usize) -> chalk_ir::Binders<T> | 703 | pub(super) fn make_binders<T>(value: T, num_vars: usize) -> chalk_ir::Binders<T> |
736 | where | 704 | where |
737 | T: HasInterner<Interner = Interner>, | 705 | T: HasInterner<Interner = Interner>, |