diff options
Diffstat (limited to 'crates/ra_hir_ty/src/traits/chalk.rs')
-rw-r--r-- | crates/ra_hir_ty/src/traits/chalk.rs | 87 |
1 files changed, 59 insertions, 28 deletions
diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs index e2f2a9ccb..61de3cc30 100644 --- a/crates/ra_hir_ty/src/traits/chalk.rs +++ b/crates/ra_hir_ty/src/traits/chalk.rs | |||
@@ -4,6 +4,7 @@ use std::sync::Arc; | |||
4 | use log::debug; | 4 | use log::debug; |
5 | 5 | ||
6 | use chalk_ir::{fold::shift::Shift, GenericArg, TypeName}; | 6 | use chalk_ir::{fold::shift::Shift, GenericArg, TypeName}; |
7 | use chalk_solve::rust_ir::{self, WellKnownTrait}; | ||
7 | 8 | ||
8 | use hir_def::{ | 9 | use hir_def::{ |
9 | lang_item::{lang_attr, LangItemTarget}, | 10 | lang_item::{lang_attr, LangItemTarget}, |
@@ -14,9 +15,8 @@ use ra_db::{salsa::InternKey, CrateId}; | |||
14 | use super::{builtin, AssocTyValue, ChalkContext, Impl}; | 15 | use super::{builtin, AssocTyValue, ChalkContext, Impl}; |
15 | use crate::{ | 16 | use crate::{ |
16 | db::HirDatabase, display::HirDisplay, method_resolution::TyFingerprint, utils::generics, | 17 | db::HirDatabase, display::HirDisplay, method_resolution::TyFingerprint, utils::generics, |
17 | DebruijnIndex, GenericPredicate, Substs, Ty, TypeCtor, | 18 | CallableDef, DebruijnIndex, GenericPredicate, Substs, Ty, TypeCtor, |
18 | }; | 19 | }; |
19 | use chalk_rust_ir::WellKnownTrait; | ||
20 | use mapping::{convert_where_clauses, generic_predicate_to_inline_bound, make_binders}; | 20 | use mapping::{convert_where_clauses, generic_predicate_to_inline_bound, make_binders}; |
21 | 21 | ||
22 | pub use self::interner::*; | 22 | pub use self::interner::*; |
@@ -54,10 +54,9 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
54 | 54 | ||
55 | fn fn_def_datum( | 55 | fn fn_def_datum( |
56 | &self, | 56 | &self, |
57 | _fn_def_id: chalk_ir::FnDefId<Interner>, | 57 | fn_def_id: chalk_ir::FnDefId<Interner>, |
58 | ) -> Arc<chalk_rust_ir::FnDefDatum<Interner>> { | 58 | ) -> Arc<rust_ir::FnDefDatum<Interner>> { |
59 | // We don't yet provide any FnDefs to Chalk | 59 | self.db.fn_def_datum(self.krate, fn_def_id) |
60 | unimplemented!() | ||
61 | } | 60 | } |
62 | 61 | ||
63 | fn impls_for_trait( | 62 | fn impls_for_trait( |
@@ -113,7 +112,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
113 | } | 112 | } |
114 | fn well_known_trait_id( | 113 | fn well_known_trait_id( |
115 | &self, | 114 | &self, |
116 | well_known_trait: chalk_rust_ir::WellKnownTrait, | 115 | well_known_trait: rust_ir::WellKnownTrait, |
117 | ) -> Option<chalk_ir::TraitId<Interner>> { | 116 | ) -> Option<chalk_ir::TraitId<Interner>> { |
118 | let lang_attr = lang_attr_from_well_known_trait(well_known_trait); | 117 | let lang_attr = lang_attr_from_well_known_trait(well_known_trait); |
119 | let lang_items = self.db.crate_lang_items(self.krate); | 118 | let lang_items = self.db.crate_lang_items(self.krate); |
@@ -134,13 +133,13 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
134 | fn opaque_ty_data( | 133 | fn opaque_ty_data( |
135 | &self, | 134 | &self, |
136 | _id: chalk_ir::OpaqueTyId<Interner>, | 135 | _id: chalk_ir::OpaqueTyId<Interner>, |
137 | ) -> Arc<chalk_rust_ir::OpaqueTyDatum<Interner>> { | 136 | ) -> Arc<rust_ir::OpaqueTyDatum<Interner>> { |
138 | unimplemented!() | 137 | unimplemented!() |
139 | } | 138 | } |
140 | 139 | ||
141 | fn force_impl_for( | 140 | fn force_impl_for( |
142 | &self, | 141 | &self, |
143 | _well_known: chalk_rust_ir::WellKnownTrait, | 142 | _well_known: rust_ir::WellKnownTrait, |
144 | _ty: &chalk_ir::TyData<Interner>, | 143 | _ty: &chalk_ir::TyData<Interner>, |
145 | ) -> Option<bool> { | 144 | ) -> Option<bool> { |
146 | // this method is mostly for rustc | 145 | // this method is mostly for rustc |
@@ -151,6 +150,10 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
151 | // FIXME: implement actual object safety | 150 | // FIXME: implement actual object safety |
152 | true | 151 | true |
153 | } | 152 | } |
153 | |||
154 | fn hidden_opaque_type(&self, _id: chalk_ir::OpaqueTyId<Interner>) -> chalk_ir::Ty<Interner> { | ||
155 | Ty::Unknown.to_chalk(self.db) | ||
156 | } | ||
154 | } | 157 | } |
155 | 158 | ||
156 | pub(crate) fn program_clauses_for_chalk_env_query( | 159 | pub(crate) fn program_clauses_for_chalk_env_query( |
@@ -189,7 +192,7 @@ pub(crate) fn associated_ty_data_query( | |||
189 | .collect(); | 192 | .collect(); |
190 | 193 | ||
191 | let where_clauses = convert_where_clauses(db, type_alias.into(), &bound_vars); | 194 | let where_clauses = convert_where_clauses(db, type_alias.into(), &bound_vars); |
192 | let bound_data = chalk_rust_ir::AssociatedTyDatumBound { bounds, where_clauses }; | 195 | let bound_data = rust_ir::AssociatedTyDatumBound { bounds, where_clauses }; |
193 | let datum = AssociatedTyDatum { | 196 | let datum = AssociatedTyDatum { |
194 | trait_id: trait_.to_chalk(db), | 197 | trait_id: trait_.to_chalk(db), |
195 | id, | 198 | id, |
@@ -210,7 +213,7 @@ pub(crate) fn trait_datum_query( | |||
210 | debug!("trait {:?} = {:?}", trait_id, trait_data.name); | 213 | debug!("trait {:?} = {:?}", trait_id, trait_data.name); |
211 | let generic_params = generics(db.upcast(), trait_.into()); | 214 | let generic_params = generics(db.upcast(), trait_.into()); |
212 | let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST); | 215 | let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST); |
213 | let flags = chalk_rust_ir::TraitFlags { | 216 | let flags = rust_ir::TraitFlags { |
214 | auto: trait_data.auto, | 217 | auto: trait_data.auto, |
215 | upstream: trait_.lookup(db.upcast()).container.module(db.upcast()).krate != krate, | 218 | upstream: trait_.lookup(db.upcast()).container.module(db.upcast()).krate != krate, |
216 | non_enumerable: true, | 219 | non_enumerable: true, |
@@ -222,7 +225,7 @@ pub(crate) fn trait_datum_query( | |||
222 | let where_clauses = convert_where_clauses(db, trait_.into(), &bound_vars); | 225 | let where_clauses = convert_where_clauses(db, trait_.into(), &bound_vars); |
223 | let associated_ty_ids = | 226 | let associated_ty_ids = |
224 | trait_data.associated_types().map(|type_alias| type_alias.to_chalk(db)).collect(); | 227 | trait_data.associated_types().map(|type_alias| type_alias.to_chalk(db)).collect(); |
225 | let trait_datum_bound = chalk_rust_ir::TraitDatumBound { where_clauses }; | 228 | let trait_datum_bound = rust_ir::TraitDatumBound { where_clauses }; |
226 | let well_known = | 229 | let well_known = |
227 | lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name)); | 230 | lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name)); |
228 | let trait_datum = TraitDatum { | 231 | let trait_datum = TraitDatum { |
@@ -272,12 +275,12 @@ pub(crate) fn struct_datum_query( | |||
272 | convert_where_clauses(db, generic_def, &bound_vars) | 275 | convert_where_clauses(db, generic_def, &bound_vars) |
273 | }) | 276 | }) |
274 | .unwrap_or_else(Vec::new); | 277 | .unwrap_or_else(Vec::new); |
275 | let flags = chalk_rust_ir::AdtFlags { | 278 | let flags = rust_ir::AdtFlags { |
276 | upstream, | 279 | upstream, |
277 | // FIXME set fundamental flag correctly | 280 | // FIXME set fundamental flag correctly |
278 | fundamental: false, | 281 | fundamental: false, |
279 | }; | 282 | }; |
280 | let struct_datum_bound = chalk_rust_ir::AdtDatumBound { | 283 | let struct_datum_bound = rust_ir::AdtDatumBound { |
281 | fields: Vec::new(), // FIXME add fields (only relevant for auto traits) | 284 | fields: Vec::new(), // FIXME add fields (only relevant for auto traits) |
282 | where_clauses, | 285 | where_clauses, |
283 | }; | 286 | }; |
@@ -317,9 +320,9 @@ fn impl_def_datum( | |||
317 | let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST); | 320 | let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST); |
318 | let trait_ = trait_ref.trait_; | 321 | let trait_ = trait_ref.trait_; |
319 | let impl_type = if impl_id.lookup(db.upcast()).container.module(db.upcast()).krate == krate { | 322 | let impl_type = if impl_id.lookup(db.upcast()).container.module(db.upcast()).krate == krate { |
320 | chalk_rust_ir::ImplType::Local | 323 | rust_ir::ImplType::Local |
321 | } else { | 324 | } else { |
322 | chalk_rust_ir::ImplType::External | 325 | rust_ir::ImplType::External |
323 | }; | 326 | }; |
324 | let where_clauses = convert_where_clauses(db, impl_id.into(), &bound_vars); | 327 | let where_clauses = convert_where_clauses(db, impl_id.into(), &bound_vars); |
325 | let negative = impl_data.is_negative; | 328 | let negative = impl_data.is_negative; |
@@ -332,13 +335,9 @@ fn impl_def_datum( | |||
332 | ); | 335 | ); |
333 | let trait_ref = trait_ref.to_chalk(db); | 336 | let trait_ref = trait_ref.to_chalk(db); |
334 | 337 | ||
335 | let polarity = if negative { | 338 | let polarity = if negative { rust_ir::Polarity::Negative } else { rust_ir::Polarity::Positive }; |
336 | chalk_rust_ir::Polarity::Negative | ||
337 | } else { | ||
338 | chalk_rust_ir::Polarity::Positive | ||
339 | }; | ||
340 | 339 | ||
341 | let impl_datum_bound = chalk_rust_ir::ImplDatumBound { trait_ref, where_clauses }; | 340 | let impl_datum_bound = rust_ir::ImplDatumBound { trait_ref, where_clauses }; |
342 | let trait_data = db.trait_data(trait_); | 341 | let trait_data = db.trait_data(trait_); |
343 | let associated_ty_value_ids = impl_data | 342 | let associated_ty_value_ids = impl_data |
344 | .items | 343 | .items |
@@ -396,8 +395,8 @@ fn type_alias_associated_ty_value( | |||
396 | .associated_type_by_name(&type_alias_data.name) | 395 | .associated_type_by_name(&type_alias_data.name) |
397 | .expect("assoc ty value should not exist"); // validated when building the impl data as well | 396 | .expect("assoc ty value should not exist"); // validated when building the impl data as well |
398 | let ty = db.ty(type_alias.into()); | 397 | let ty = db.ty(type_alias.into()); |
399 | let value_bound = chalk_rust_ir::AssociatedTyValueBound { ty: ty.value.to_chalk(db) }; | 398 | let value_bound = rust_ir::AssociatedTyValueBound { ty: ty.value.to_chalk(db) }; |
400 | let value = chalk_rust_ir::AssociatedTyValue { | 399 | let value = rust_ir::AssociatedTyValue { |
401 | impl_id: Impl::ImplDef(impl_id).to_chalk(db), | 400 | impl_id: Impl::ImplDef(impl_id).to_chalk(db), |
402 | associated_ty_id: assoc_ty.to_chalk(db), | 401 | associated_ty_id: assoc_ty.to_chalk(db), |
403 | value: make_binders(value_bound, ty.num_binders), | 402 | value: make_binders(value_bound, ty.num_binders), |
@@ -405,6 +404,26 @@ fn type_alias_associated_ty_value( | |||
405 | Arc::new(value) | 404 | Arc::new(value) |
406 | } | 405 | } |
407 | 406 | ||
407 | pub(crate) fn fn_def_datum_query( | ||
408 | db: &dyn HirDatabase, | ||
409 | _krate: CrateId, | ||
410 | fn_def_id: FnDefId, | ||
411 | ) -> Arc<FnDefDatum> { | ||
412 | let callable_def: CallableDef = from_chalk(db, fn_def_id); | ||
413 | let generic_params = generics(db.upcast(), callable_def.into()); | ||
414 | let sig = db.callable_item_signature(callable_def); | ||
415 | let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST); | ||
416 | let where_clauses = convert_where_clauses(db, callable_def.into(), &bound_vars); | ||
417 | let bound = rust_ir::FnDefDatumBound { | ||
418 | // Note: Chalk doesn't actually use this information yet as far as I am aware, but we provide it anyway | ||
419 | argument_types: sig.value.params().iter().map(|ty| ty.clone().to_chalk(db)).collect(), | ||
420 | return_type: sig.value.ret().clone().to_chalk(db), | ||
421 | where_clauses, | ||
422 | }; | ||
423 | let datum = FnDefDatum { id: fn_def_id, binders: make_binders(bound, sig.num_binders) }; | ||
424 | Arc::new(datum) | ||
425 | } | ||
426 | |||
408 | impl From<AdtId> for crate::TypeCtorId { | 427 | impl From<AdtId> for crate::TypeCtorId { |
409 | fn from(struct_id: AdtId) -> Self { | 428 | fn from(struct_id: AdtId) -> Self { |
410 | struct_id.0 | 429 | struct_id.0 |
@@ -417,6 +436,18 @@ impl From<crate::TypeCtorId> for AdtId { | |||
417 | } | 436 | } |
418 | } | 437 | } |
419 | 438 | ||
439 | impl From<FnDefId> for crate::CallableDefId { | ||
440 | fn from(fn_def_id: FnDefId) -> Self { | ||
441 | InternKey::from_intern_id(fn_def_id.0) | ||
442 | } | ||
443 | } | ||
444 | |||
445 | impl From<crate::CallableDefId> for FnDefId { | ||
446 | fn from(callable_def_id: crate::CallableDefId) -> Self { | ||
447 | chalk_ir::FnDefId(callable_def_id.as_intern_id()) | ||
448 | } | ||
449 | } | ||
450 | |||
420 | impl From<ImplId> for crate::traits::GlobalImplId { | 451 | impl From<ImplId> for crate::traits::GlobalImplId { |
421 | fn from(impl_id: ImplId) -> Self { | 452 | fn from(impl_id: ImplId) -> Self { |
422 | InternKey::from_intern_id(impl_id.0) | 453 | InternKey::from_intern_id(impl_id.0) |
@@ -429,14 +460,14 @@ impl From<crate::traits::GlobalImplId> for ImplId { | |||
429 | } | 460 | } |
430 | } | 461 | } |
431 | 462 | ||
432 | impl From<chalk_rust_ir::AssociatedTyValueId<Interner>> for crate::traits::AssocTyValueId { | 463 | impl From<rust_ir::AssociatedTyValueId<Interner>> for crate::traits::AssocTyValueId { |
433 | fn from(id: chalk_rust_ir::AssociatedTyValueId<Interner>) -> Self { | 464 | fn from(id: rust_ir::AssociatedTyValueId<Interner>) -> Self { |
434 | Self::from_intern_id(id.0) | 465 | Self::from_intern_id(id.0) |
435 | } | 466 | } |
436 | } | 467 | } |
437 | 468 | ||
438 | impl From<crate::traits::AssocTyValueId> for chalk_rust_ir::AssociatedTyValueId<Interner> { | 469 | impl From<crate::traits::AssocTyValueId> for rust_ir::AssociatedTyValueId<Interner> { |
439 | fn from(assoc_ty_value_id: crate::traits::AssocTyValueId) -> Self { | 470 | fn from(assoc_ty_value_id: crate::traits::AssocTyValueId) -> Self { |
440 | chalk_rust_ir::AssociatedTyValueId(assoc_ty_value_id.as_intern_id()) | 471 | rust_ir::AssociatedTyValueId(assoc_ty_value_id.as_intern_id()) |
441 | } | 472 | } |
442 | } | 473 | } |