aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2021-04-05 20:14:49 +0100
committerFlorian Diebold <[email protected]>2021-04-05 20:58:01 +0100
commit788533d38091472ed05c8b55fd7a2002c49f4eb7 (patch)
tree25d131cb108e52f8abc585642730c4f29abb3727 /crates/hir_ty/src
parent8c96a7d81e29061537d78b5670a3474cb136422d (diff)
Move ProjectionTy methods to extension trait
Diffstat (limited to 'crates/hir_ty/src')
-rw-r--r--crates/hir_ty/src/autoderef.rs2
-rw-r--r--crates/hir_ty/src/chalk_ext.rs28
-rw-r--r--crates/hir_ty/src/display.rs3
-rw-r--r--crates/hir_ty/src/infer/expr.rs4
-rw-r--r--crates/hir_ty/src/lib.rs26
-rw-r--r--crates/hir_ty/src/traits/chalk/mapping.rs6
-rw-r--r--crates/hir_ty/src/types.rs6
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
14use crate::{ 14use 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
19const AUTODEREF_RECURSION_LIMIT: usize = 10; 19const 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
3use crate::{Interner, Ty, TyKind}; 3use hir_def::{AssocContainerId, Lookup, TraitId};
4
5use crate::{
6 db::HirDatabase, from_assoc_type_id, to_chalk_trait_id, Interner, ProjectionTy, TraitRef, Ty,
7 TyKind,
8};
4 9
5pub trait TyExt { 10pub 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
20pub trait ProjectionTyExt {
21 fn trait_ref(&self, db: &dyn HirDatabase) -> TraitRef;
22 fn trait_(&self, db: &dyn HirDatabase) -> TraitId;
23}
24
25impl 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
28pub struct HirFormatter<'a> { 29pub 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
29use super::{ 29use 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
44pub use autoderef::autoderef; 44pub use autoderef::autoderef;
45pub use builder::TyBuilder; 45pub use builder::TyBuilder;
46pub use chalk_ext::TyExt; 46pub use chalk_ext::{ProjectionTyExt, TyExt};
47pub use infer::{could_unify, InferenceResult, InferenceVar}; 47pub use infer::{could_unify, InferenceResult, InferenceVar};
48pub use lower::{ 48pub 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};
52pub use traits::TraitEnvironment; 52pub use traits::{chalk::Interner, TraitEnvironment};
53pub use types::*; 53pub use types::*;
54pub use walk::TypeWalk; 54pub 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
60pub use crate::traits::chalk::Interner;
61
62pub type ForeignDefId = chalk_ir::ForeignDefId<Interner>; 60pub type ForeignDefId = chalk_ir::ForeignDefId<Interner>;
63pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>; 61pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>;
64pub type FnDefId = chalk_ir::FnDefId<Interner>; 62pub type FnDefId = chalk_ir::FnDefId<Interner>;
@@ -76,26 +74,6 @@ pub type LifetimeOutlives = chalk_ir::LifetimeOutlives<Interner>;
76 74
77pub type ChalkTraitId = chalk_ir::TraitId<Interner>; 75pub type ChalkTraitId = chalk_ir::TraitId<Interner>;
78 76
79impl 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
99pub type FnSig = chalk_ir::FnSig<Interner>; 77pub type FnSig = chalk_ir::FnSig<Interner>;
100 78
101impl Substitution { 79impl 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;
10use hir_def::{GenericDefId, TypeAliasId}; 10use hir_def::{GenericDefId, TypeAliasId};
11 11
12use crate::{ 12use 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
18use super::interner::*; 18use 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
32impl 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)]
33pub struct DynTy { 39pub struct DynTy {
34 /// The unknown self type. 40 /// The unknown self type.