diff options
author | Florian Diebold <[email protected]> | 2021-04-05 20:14:49 +0100 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2021-04-05 20:58:01 +0100 |
commit | 788533d38091472ed05c8b55fd7a2002c49f4eb7 (patch) | |
tree | 25d131cb108e52f8abc585642730c4f29abb3727 /crates/hir_ty/src | |
parent | 8c96a7d81e29061537d78b5670a3474cb136422d (diff) |
Move ProjectionTy methods to extension trait
Diffstat (limited to 'crates/hir_ty/src')
-rw-r--r-- | crates/hir_ty/src/autoderef.rs | 2 | ||||
-rw-r--r-- | crates/hir_ty/src/chalk_ext.rs | 28 | ||||
-rw-r--r-- | crates/hir_ty/src/display.rs | 3 | ||||
-rw-r--r-- | crates/hir_ty/src/infer/expr.rs | 4 | ||||
-rw-r--r-- | crates/hir_ty/src/lib.rs | 26 | ||||
-rw-r--r-- | crates/hir_ty/src/traits/chalk/mapping.rs | 6 | ||||
-rw-r--r-- | crates/hir_ty/src/types.rs | 6 |
7 files changed, 43 insertions, 32 deletions
diff --git a/crates/hir_ty/src/autoderef.rs b/crates/hir_ty/src/autoderef.rs index 7ca4af80e..c5890e24d 100644 --- a/crates/hir_ty/src/autoderef.rs +++ b/crates/hir_ty/src/autoderef.rs | |||
@@ -13,7 +13,7 @@ use log::{info, warn}; | |||
13 | 13 | ||
14 | use crate::{ | 14 | use crate::{ |
15 | db::HirDatabase, AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, | 15 | db::HirDatabase, AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, |
16 | InEnvironment, Interner, Solution, Ty, TyBuilder, TyKind, | 16 | InEnvironment, Interner, ProjectionTyExt, Solution, Ty, TyBuilder, TyKind, |
17 | }; | 17 | }; |
18 | 18 | ||
19 | const AUTODEREF_RECURSION_LIMIT: usize = 10; | 19 | const AUTODEREF_RECURSION_LIMIT: usize = 10; |
diff --git a/crates/hir_ty/src/chalk_ext.rs b/crates/hir_ty/src/chalk_ext.rs index b7463366b..0f4cb43e9 100644 --- a/crates/hir_ty/src/chalk_ext.rs +++ b/crates/hir_ty/src/chalk_ext.rs | |||
@@ -1,6 +1,11 @@ | |||
1 | //! Various extensions traits for Chalk types. | 1 | //! Various extensions traits for Chalk types. |
2 | 2 | ||
3 | use crate::{Interner, Ty, TyKind}; | 3 | use hir_def::{AssocContainerId, Lookup, TraitId}; |
4 | |||
5 | use crate::{ | ||
6 | db::HirDatabase, from_assoc_type_id, to_chalk_trait_id, Interner, ProjectionTy, TraitRef, Ty, | ||
7 | TyKind, | ||
8 | }; | ||
4 | 9 | ||
5 | pub trait TyExt { | 10 | pub trait TyExt { |
6 | fn is_unit(&self) -> bool; | 11 | fn is_unit(&self) -> bool; |
@@ -11,3 +16,24 @@ impl TyExt for Ty { | |||
11 | matches!(self.kind(&Interner), TyKind::Tuple(0, _)) | 16 | matches!(self.kind(&Interner), TyKind::Tuple(0, _)) |
12 | } | 17 | } |
13 | } | 18 | } |
19 | |||
20 | pub trait ProjectionTyExt { | ||
21 | fn trait_ref(&self, db: &dyn HirDatabase) -> TraitRef; | ||
22 | fn trait_(&self, db: &dyn HirDatabase) -> TraitId; | ||
23 | } | ||
24 | |||
25 | impl ProjectionTyExt for ProjectionTy { | ||
26 | fn trait_ref(&self, db: &dyn HirDatabase) -> TraitRef { | ||
27 | TraitRef { | ||
28 | trait_id: to_chalk_trait_id(self.trait_(db)), | ||
29 | substitution: self.substitution.clone(), | ||
30 | } | ||
31 | } | ||
32 | |||
33 | fn trait_(&self, db: &dyn HirDatabase) -> TraitId { | ||
34 | match from_assoc_type_id(self.associated_ty_id).lookup(db.upcast()).container { | ||
35 | AssocContainerId::TraitId(it) => it, | ||
36 | _ => panic!("projection ty without parent trait"), | ||
37 | } | ||
38 | } | ||
39 | } | ||
diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 01c7ef91f..91d657ce2 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs | |||
@@ -22,7 +22,8 @@ use crate::{ | |||
22 | lt_from_placeholder_idx, primitive, to_assoc_type_id, traits::chalk::from_chalk, | 22 | lt_from_placeholder_idx, primitive, to_assoc_type_id, traits::chalk::from_chalk, |
23 | utils::generics, AdtId, AliasEq, AliasTy, CallableDefId, CallableSig, DomainGoal, GenericArg, | 23 | utils::generics, AdtId, AliasEq, AliasTy, CallableDefId, CallableSig, DomainGoal, GenericArg, |
24 | ImplTraitId, Interner, Lifetime, LifetimeData, LifetimeOutlives, Mutability, OpaqueTy, | 24 | ImplTraitId, Interner, Lifetime, LifetimeData, LifetimeOutlives, Mutability, OpaqueTy, |
25 | ProjectionTy, QuantifiedWhereClause, Scalar, TraitRef, Ty, TyExt, TyKind, WhereClause, | 25 | ProjectionTy, ProjectionTyExt, QuantifiedWhereClause, Scalar, TraitRef, Ty, TyExt, TyKind, |
26 | WhereClause, | ||
26 | }; | 27 | }; |
27 | 28 | ||
28 | pub struct HirFormatter<'a> { | 29 | pub struct HirFormatter<'a> { |
diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 6966d26e7..56a9365cb 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs | |||
@@ -22,8 +22,8 @@ use crate::{ | |||
22 | to_chalk_trait_id, | 22 | to_chalk_trait_id, |
23 | traits::{chalk::from_chalk, FnTrait}, | 23 | traits::{chalk::from_chalk, FnTrait}, |
24 | utils::{generics, variant_data, Generics}, | 24 | utils::{generics, variant_data, Generics}, |
25 | AdtId, Binders, CallableDefId, FnPointer, FnSig, InEnvironment, Interner, Rawness, Scalar, | 25 | AdtId, Binders, CallableDefId, FnPointer, FnSig, InEnvironment, Interner, ProjectionTyExt, |
26 | Substitution, TraitRef, Ty, TyBuilder, TyKind, | 26 | Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyKind, |
27 | }; | 27 | }; |
28 | 28 | ||
29 | use super::{ | 29 | use super::{ |
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index a3addc8e9..928dd76e5 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs | |||
@@ -43,13 +43,13 @@ use crate::{db::HirDatabase, display::HirDisplay, utils::generics}; | |||
43 | 43 | ||
44 | pub use autoderef::autoderef; | 44 | pub use autoderef::autoderef; |
45 | pub use builder::TyBuilder; | 45 | pub use builder::TyBuilder; |
46 | pub use chalk_ext::TyExt; | 46 | pub use chalk_ext::{ProjectionTyExt, TyExt}; |
47 | pub use infer::{could_unify, InferenceResult, InferenceVar}; | 47 | pub use infer::{could_unify, InferenceResult, InferenceVar}; |
48 | pub use lower::{ | 48 | pub use lower::{ |
49 | associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode, | 49 | associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode, |
50 | TyDefId, TyLoweringContext, ValueTyDefId, | 50 | TyDefId, TyLoweringContext, ValueTyDefId, |
51 | }; | 51 | }; |
52 | pub use traits::TraitEnvironment; | 52 | pub use traits::{chalk::Interner, TraitEnvironment}; |
53 | pub use types::*; | 53 | pub use types::*; |
54 | pub use walk::TypeWalk; | 54 | pub use walk::TypeWalk; |
55 | 55 | ||
@@ -57,8 +57,6 @@ pub use chalk_ir::{ | |||
57 | cast::Cast, AdtId, BoundVar, DebruijnIndex, Mutability, Safety, Scalar, TyVariableKind, | 57 | cast::Cast, AdtId, BoundVar, DebruijnIndex, Mutability, Safety, Scalar, TyVariableKind, |
58 | }; | 58 | }; |
59 | 59 | ||
60 | pub use crate::traits::chalk::Interner; | ||
61 | |||
62 | pub type ForeignDefId = chalk_ir::ForeignDefId<Interner>; | 60 | pub type ForeignDefId = chalk_ir::ForeignDefId<Interner>; |
63 | pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>; | 61 | pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>; |
64 | pub type FnDefId = chalk_ir::FnDefId<Interner>; | 62 | pub type FnDefId = chalk_ir::FnDefId<Interner>; |
@@ -76,26 +74,6 @@ pub type LifetimeOutlives = chalk_ir::LifetimeOutlives<Interner>; | |||
76 | 74 | ||
77 | pub type ChalkTraitId = chalk_ir::TraitId<Interner>; | 75 | pub type ChalkTraitId = chalk_ir::TraitId<Interner>; |
78 | 76 | ||
79 | impl ProjectionTy { | ||
80 | pub fn trait_ref(&self, db: &dyn HirDatabase) -> TraitRef { | ||
81 | TraitRef { | ||
82 | trait_id: to_chalk_trait_id(self.trait_(db)), | ||
83 | substitution: self.substitution.clone(), | ||
84 | } | ||
85 | } | ||
86 | |||
87 | pub fn self_type_parameter(&self, interner: &Interner) -> &Ty { | ||
88 | &self.substitution.interned()[0].assert_ty_ref(interner) | ||
89 | } | ||
90 | |||
91 | fn trait_(&self, db: &dyn HirDatabase) -> TraitId { | ||
92 | match from_assoc_type_id(self.associated_ty_id).lookup(db.upcast()).container { | ||
93 | AssocContainerId::TraitId(it) => it, | ||
94 | _ => panic!("projection ty without parent trait"), | ||
95 | } | ||
96 | } | ||
97 | } | ||
98 | |||
99 | pub type FnSig = chalk_ir::FnSig<Interner>; | 77 | pub type FnSig = chalk_ir::FnSig<Interner>; |
100 | 78 | ||
101 | impl Substitution { | 79 | impl Substitution { |
diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index c3b148cab..9f10b889f 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs | |||
@@ -10,9 +10,9 @@ use base_db::salsa::InternKey; | |||
10 | use hir_def::{GenericDefId, TypeAliasId}; | 10 | use hir_def::{GenericDefId, TypeAliasId}; |
11 | 11 | ||
12 | use crate::{ | 12 | use crate::{ |
13 | db::HirDatabase, primitive::UintTy, AliasTy, CallableDefId, Canonical, DomainGoal, FnPointer, | 13 | chalk_ext::ProjectionTyExt, db::HirDatabase, primitive::UintTy, AliasTy, CallableDefId, |
14 | GenericArg, InEnvironment, OpaqueTy, ProjectionTy, QuantifiedWhereClause, Scalar, Substitution, | 14 | Canonical, DomainGoal, FnPointer, GenericArg, InEnvironment, OpaqueTy, ProjectionTy, |
15 | TraitRef, Ty, TypeWalk, WhereClause, | 15 | QuantifiedWhereClause, Scalar, Substitution, TraitRef, Ty, TypeWalk, WhereClause, |
16 | }; | 16 | }; |
17 | 17 | ||
18 | use super::interner::*; | 18 | use super::interner::*; |
diff --git a/crates/hir_ty/src/types.rs b/crates/hir_ty/src/types.rs index dc64e6e2b..bd89991dc 100644 --- a/crates/hir_ty/src/types.rs +++ b/crates/hir_ty/src/types.rs | |||
@@ -29,6 +29,12 @@ pub struct ProjectionTy { | |||
29 | pub substitution: Substitution, | 29 | pub substitution: Substitution, |
30 | } | 30 | } |
31 | 31 | ||
32 | impl ProjectionTy { | ||
33 | pub fn self_type_parameter(&self, interner: &Interner) -> &Ty { | ||
34 | &self.substitution.interned()[0].assert_ty_ref(interner) | ||
35 | } | ||
36 | } | ||
37 | |||
32 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] | 38 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
33 | pub struct DynTy { | 39 | pub struct DynTy { |
34 | /// The unknown self type. | 40 | /// The unknown self type. |