aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/traits
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src/traits')
-rw-r--r--crates/hir_ty/src/traits/chalk.rs17
-rw-r--r--crates/hir_ty/src/traits/chalk/mapping.rs47
-rw-r--r--crates/hir_ty/src/traits/chalk/tls.rs9
3 files changed, 26 insertions, 47 deletions
diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs
index 55181cc49..2d3cd6719 100644
--- a/crates/hir_ty/src/traits/chalk.rs
+++ b/crates/hir_ty/src/traits/chalk.rs
@@ -17,14 +17,15 @@ use super::ChalkContext;
17use crate::{ 17use crate::{
18 db::HirDatabase, 18 db::HirDatabase,
19 display::HirDisplay, 19 display::HirDisplay,
20 from_assoc_type_id,
20 method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS}, 21 method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS},
22 to_assoc_type_id,
21 utils::generics, 23 utils::generics,
22 BoundVar, CallableDefId, CallableSig, DebruijnIndex, GenericPredicate, ProjectionPredicate, 24 BoundVar, CallableDefId, CallableSig, DebruijnIndex, GenericPredicate, ProjectionPredicate,
23 ProjectionTy, Substs, TraitRef, Ty, TyKind, 25 ProjectionTy, Substs, TraitRef, Ty, TyKind,
24}; 26};
25use mapping::{ 27use mapping::{
26 convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsAssocType, 28 convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue,
27 TypeAliasAsValue,
28}; 29};
29 30
30pub use self::interner::Interner; 31pub use self::interner::Interner;
@@ -234,7 +235,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
234 ty: TyKind::BoundVar(BoundVar { debruijn: DebruijnIndex::ONE, index: 0 }) 235 ty: TyKind::BoundVar(BoundVar { debruijn: DebruijnIndex::ONE, index: 0 })
235 .intern(&Interner), 236 .intern(&Interner),
236 projection_ty: ProjectionTy { 237 projection_ty: ProjectionTy {
237 associated_ty: future_output, 238 associated_ty: to_assoc_type_id(future_output),
238 // Self type as the first parameter. 239 // Self type as the first parameter.
239 parameters: Substs::single( 240 parameters: Substs::single(
240 TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)) 241 TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0))
@@ -383,7 +384,7 @@ pub(crate) fn associated_ty_data_query(
383 id: AssocTypeId, 384 id: AssocTypeId,
384) -> Arc<AssociatedTyDatum> { 385) -> Arc<AssociatedTyDatum> {
385 debug!("associated_ty_data {:?}", id); 386 debug!("associated_ty_data {:?}", id);
386 let type_alias: TypeAliasId = from_chalk::<TypeAliasAsAssocType, _>(db, id).0; 387 let type_alias: TypeAliasId = from_assoc_type_id(id);
387 let trait_ = match type_alias.lookup(db.upcast()).container { 388 let trait_ = match type_alias.lookup(db.upcast()).container {
388 AssocContainerId::TraitId(t) => t, 389 AssocContainerId::TraitId(t) => t,
389 _ => panic!("associated type not in trait"), 390 _ => panic!("associated type not in trait"),
@@ -438,10 +439,8 @@ pub(crate) fn trait_datum_query(
438 fundamental: false, 439 fundamental: false,
439 }; 440 };
440 let where_clauses = convert_where_clauses(db, trait_.into(), &bound_vars); 441 let where_clauses = convert_where_clauses(db, trait_.into(), &bound_vars);
441 let associated_ty_ids = trait_data 442 let associated_ty_ids =
442 .associated_types() 443 trait_data.associated_types().map(|type_alias| to_assoc_type_id(type_alias)).collect();
443 .map(|type_alias| TypeAliasAsAssocType(type_alias).to_chalk(db))
444 .collect();
445 let trait_datum_bound = rust_ir::TraitDatumBound { where_clauses }; 444 let trait_datum_bound = rust_ir::TraitDatumBound { where_clauses };
446 let well_known = 445 let well_known =
447 lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name)); 446 lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name));
@@ -623,7 +622,7 @@ fn type_alias_associated_ty_value(
623 let value_bound = rust_ir::AssociatedTyValueBound { ty: ty.value.to_chalk(db) }; 622 let value_bound = rust_ir::AssociatedTyValueBound { ty: ty.value.to_chalk(db) };
624 let value = rust_ir::AssociatedTyValue { 623 let value = rust_ir::AssociatedTyValue {
625 impl_id: impl_id.to_chalk(db), 624 impl_id: impl_id.to_chalk(db),
626 associated_ty_id: TypeAliasAsAssocType(assoc_ty).to_chalk(db), 625 associated_ty_id: to_assoc_type_id(assoc_ty),
627 value: make_binders(value_bound, ty.num_binders), 626 value: make_binders(value_bound, ty.num_binders),
628 }; 627 };
629 Arc::new(value) 628 Arc::new(value)
diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs
index cf490f9c5..67ac95a0e 100644
--- a/crates/hir_ty/src/traits/chalk/mapping.rs
+++ b/crates/hir_ty/src/traits/chalk/mapping.rs
@@ -14,6 +14,7 @@ use hir_def::{AssocContainerId, GenericDefId, Lookup, TypeAliasId};
14 14
15use crate::{ 15use crate::{
16 db::HirDatabase, 16 db::HirDatabase,
17 from_assoc_type_id,
17 primitive::UintTy, 18 primitive::UintTy,
18 traits::{Canonical, Obligation}, 19 traits::{Canonical, Obligation},
19 AliasTy, CallableDefId, FnPointer, FnSig, GenericPredicate, InEnvironment, OpaqueTy, 20 AliasTy, CallableDefId, FnPointer, FnSig, GenericPredicate, InEnvironment, OpaqueTy,
@@ -38,9 +39,7 @@ impl ToChalk for Ty {
38 }) 39 })
39 .intern(&Interner) 40 .intern(&Interner)
40 } 41 }
41 TyKind::AssociatedType(type_alias, substs) => { 42 TyKind::AssociatedType(assoc_type_id, substs) => {
42 let assoc_type = TypeAliasAsAssocType(type_alias);
43 let assoc_type_id = assoc_type.to_chalk(db);
44 let substitution = substs.to_chalk(db); 43 let substitution = substs.to_chalk(db);
45 chalk_ir::TyKind::AssociatedType(assoc_type_id, substitution).intern(&Interner) 44 chalk_ir::TyKind::AssociatedType(assoc_type_id, substitution).intern(&Interner)
46 } 45 }
@@ -85,7 +84,7 @@ impl ToChalk for Ty {
85 chalk_ir::TyKind::Adt(adt_id, substitution).intern(&Interner) 84 chalk_ir::TyKind::Adt(adt_id, substitution).intern(&Interner)
86 } 85 }
87 TyKind::Alias(AliasTy::Projection(proj_ty)) => { 86 TyKind::Alias(AliasTy::Projection(proj_ty)) => {
88 let associated_ty_id = TypeAliasAsAssocType(proj_ty.associated_ty).to_chalk(db); 87 let associated_ty_id = proj_ty.associated_ty;
89 let substitution = proj_ty.parameters.to_chalk(db); 88 let substitution = proj_ty.parameters.to_chalk(db);
90 chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy { 89 chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy {
91 associated_ty_id, 90 associated_ty_id,
@@ -139,8 +138,7 @@ impl ToChalk for Ty {
139 TyKind::Placeholder(db.lookup_intern_type_param_id(interned_id)) 138 TyKind::Placeholder(db.lookup_intern_type_param_id(interned_id))
140 } 139 }
141 chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => { 140 chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => {
142 let associated_ty = 141 let associated_ty = proj.associated_ty_id;
143 from_chalk::<TypeAliasAsAssocType, _>(db, proj.associated_ty_id).0;
144 let parameters = from_chalk(db, proj.substitution); 142 let parameters = from_chalk(db, proj.substitution);
145 TyKind::Alias(AliasTy::Projection(ProjectionTy { associated_ty, parameters })) 143 TyKind::Alias(AliasTy::Projection(ProjectionTy { associated_ty, parameters }))
146 } 144 }
@@ -180,10 +178,9 @@ impl ToChalk for Ty {
180 } 178 }
181 179
182 chalk_ir::TyKind::Adt(adt_id, subst) => TyKind::Adt(adt_id, from_chalk(db, subst)), 180 chalk_ir::TyKind::Adt(adt_id, subst) => TyKind::Adt(adt_id, from_chalk(db, subst)),
183 chalk_ir::TyKind::AssociatedType(type_id, subst) => TyKind::AssociatedType( 181 chalk_ir::TyKind::AssociatedType(type_id, subst) => {
184 from_chalk::<TypeAliasAsAssocType, _>(db, type_id).0, 182 TyKind::AssociatedType(type_id, from_chalk(db, subst))
185 from_chalk(db, subst), 183 }
186 ),
187 184
188 chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => { 185 chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => {
189 TyKind::OpaqueType(from_chalk(db, opaque_type_id), from_chalk(db, subst)) 186 TyKind::OpaqueType(from_chalk(db, opaque_type_id), from_chalk(db, subst))
@@ -332,20 +329,6 @@ impl ToChalk for CallableDefId {
332 } 329 }
333} 330}
334 331
335pub(crate) struct TypeAliasAsAssocType(pub(crate) TypeAliasId);
336
337impl ToChalk for TypeAliasAsAssocType {
338 type Chalk = AssocTypeId;
339
340 fn to_chalk(self, _db: &dyn HirDatabase) -> AssocTypeId {
341 chalk_ir::AssocTypeId(self.0.as_intern_id())
342 }
343
344 fn from_chalk(_db: &dyn HirDatabase, assoc_type_id: AssocTypeId) -> TypeAliasAsAssocType {
345 TypeAliasAsAssocType(InternKey::from_intern_id(assoc_type_id.0))
346 }
347}
348
349pub(crate) struct TypeAliasAsValue(pub(crate) TypeAliasId); 332pub(crate) struct TypeAliasAsValue(pub(crate) TypeAliasId);
350 333
351impl ToChalk for TypeAliasAsValue { 334impl ToChalk for TypeAliasAsValue {
@@ -427,7 +410,7 @@ impl ToChalk for ProjectionTy {
427 410
428 fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::ProjectionTy<Interner> { 411 fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::ProjectionTy<Interner> {
429 chalk_ir::ProjectionTy { 412 chalk_ir::ProjectionTy {
430 associated_ty_id: TypeAliasAsAssocType(self.associated_ty).to_chalk(db), 413 associated_ty_id: self.associated_ty,
431 substitution: self.parameters.to_chalk(db), 414 substitution: self.parameters.to_chalk(db),
432 } 415 }
433 } 416 }
@@ -437,11 +420,7 @@ impl ToChalk for ProjectionTy {
437 projection_ty: chalk_ir::ProjectionTy<Interner>, 420 projection_ty: chalk_ir::ProjectionTy<Interner>,
438 ) -> ProjectionTy { 421 ) -> ProjectionTy {
439 ProjectionTy { 422 ProjectionTy {
440 associated_ty: from_chalk::<TypeAliasAsAssocType, _>( 423 associated_ty: projection_ty.associated_ty_id,
441 db,
442 projection_ty.associated_ty_id,
443 )
444 .0,
445 parameters: from_chalk(db, projection_ty.substitution), 424 parameters: from_chalk(db, projection_ty.substitution),
446 } 425 }
447 } 426 }
@@ -595,7 +574,10 @@ pub(super) fn generic_predicate_to_inline_bound(
595 if &proj.projection_ty.parameters[0] != self_ty { 574 if &proj.projection_ty.parameters[0] != self_ty {
596 return None; 575 return None;
597 } 576 }
598 let trait_ = match proj.projection_ty.associated_ty.lookup(db.upcast()).container { 577 let trait_ = match from_assoc_type_id(proj.projection_ty.associated_ty)
578 .lookup(db.upcast())
579 .container
580 {
599 AssocContainerId::TraitId(t) => t, 581 AssocContainerId::TraitId(t) => t,
600 _ => panic!("associated type not in trait"), 582 _ => panic!("associated type not in trait"),
601 }; 583 };
@@ -606,8 +588,7 @@ pub(super) fn generic_predicate_to_inline_bound(
606 let alias_eq_bound = rust_ir::AliasEqBound { 588 let alias_eq_bound = rust_ir::AliasEqBound {
607 value: proj.ty.clone().to_chalk(db), 589 value: proj.ty.clone().to_chalk(db),
608 trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self }, 590 trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self },
609 associated_ty_id: TypeAliasAsAssocType(proj.projection_ty.associated_ty) 591 associated_ty_id: proj.projection_ty.associated_ty,
610 .to_chalk(db),
611 parameters: Vec::new(), // FIXME we don't support generic associated types yet 592 parameters: Vec::new(), // FIXME we don't support generic associated types yet
612 }; 593 };
613 Some(rust_ir::InlineBound::AliasEqBound(alias_eq_bound)) 594 Some(rust_ir::InlineBound::AliasEqBound(alias_eq_bound))
diff --git a/crates/hir_ty/src/traits/chalk/tls.rs b/crates/hir_ty/src/traits/chalk/tls.rs
index 75b16172e..8892a63a9 100644
--- a/crates/hir_ty/src/traits/chalk/tls.rs
+++ b/crates/hir_ty/src/traits/chalk/tls.rs
@@ -4,8 +4,8 @@ use std::fmt;
4use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplication}; 4use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplication};
5use itertools::Itertools; 5use itertools::Itertools;
6 6
7use super::{from_chalk, Interner, TypeAliasAsAssocType}; 7use super::{from_chalk, Interner};
8use crate::{db::HirDatabase, CallableDefId}; 8use crate::{db::HirDatabase, from_assoc_type_id, CallableDefId};
9use hir_def::{AdtId, AssocContainerId, Lookup, TypeAliasId}; 9use hir_def::{AdtId, AssocContainerId, Lookup, TypeAliasId};
10 10
11pub(crate) use unsafe_tls::{set_current_program, with_current_program}; 11pub(crate) use unsafe_tls::{set_current_program, with_current_program};
@@ -41,7 +41,7 @@ impl DebugContext<'_> {
41 id: super::AssocTypeId, 41 id: super::AssocTypeId,
42 fmt: &mut fmt::Formatter<'_>, 42 fmt: &mut fmt::Formatter<'_>,
43 ) -> Result<(), fmt::Error> { 43 ) -> Result<(), fmt::Error> {
44 let type_alias: TypeAliasId = from_chalk::<TypeAliasAsAssocType, _>(self.0, id).0; 44 let type_alias: TypeAliasId = from_assoc_type_id(id);
45 let type_alias_data = self.0.type_alias_data(type_alias); 45 let type_alias_data = self.0.type_alias_data(type_alias);
46 let trait_ = match type_alias.lookup(self.0.upcast()).container { 46 let trait_ = match type_alias.lookup(self.0.upcast()).container {
47 AssocContainerId::TraitId(t) => t, 47 AssocContainerId::TraitId(t) => t,
@@ -75,8 +75,7 @@ impl DebugContext<'_> {
75 projection_ty: &chalk_ir::ProjectionTy<Interner>, 75 projection_ty: &chalk_ir::ProjectionTy<Interner>,
76 fmt: &mut fmt::Formatter<'_>, 76 fmt: &mut fmt::Formatter<'_>,
77 ) -> Result<(), fmt::Error> { 77 ) -> Result<(), fmt::Error> {
78 let type_alias: TypeAliasId = 78 let type_alias = from_assoc_type_id(projection_ty.associated_ty_id);
79 from_chalk::<TypeAliasAsAssocType, _>(self.0, projection_ty.associated_ty_id).0;
80 let type_alias_data = self.0.type_alias_data(type_alias); 79 let type_alias_data = self.0.type_alias_data(type_alias);
81 let trait_ = match type_alias.lookup(self.0.upcast()).container { 80 let trait_ = match type_alias.lookup(self.0.upcast()).container {
82 AssocContainerId::TraitId(t) => t, 81 AssocContainerId::TraitId(t) => t,