diff options
Diffstat (limited to 'crates/hir_ty/src/lib.rs')
-rw-r--r-- | crates/hir_ty/src/lib.rs | 89 |
1 files changed, 10 insertions, 79 deletions
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index a3addc8e9..f74b22b58 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs | |||
@@ -31,7 +31,6 @@ mod test_db; | |||
31 | use std::sync::Arc; | 31 | use std::sync::Arc; |
32 | 32 | ||
33 | use itertools::Itertools; | 33 | use itertools::Itertools; |
34 | use smallvec::SmallVec; | ||
35 | 34 | ||
36 | use base_db::salsa; | 35 | use base_db::salsa; |
37 | use hir_def::{ | 36 | use hir_def::{ |
@@ -43,13 +42,13 @@ use crate::{db::HirDatabase, display::HirDisplay, utils::generics}; | |||
43 | 42 | ||
44 | pub use autoderef::autoderef; | 43 | pub use autoderef::autoderef; |
45 | pub use builder::TyBuilder; | 44 | pub use builder::TyBuilder; |
46 | pub use chalk_ext::TyExt; | 45 | pub use chalk_ext::{ProjectionTyExt, TyExt}; |
47 | pub use infer::{could_unify, InferenceResult, InferenceVar}; | 46 | pub use infer::{could_unify, InferenceResult, InferenceVar}; |
48 | pub use lower::{ | 47 | pub use lower::{ |
49 | associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode, | 48 | associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode, |
50 | TyDefId, TyLoweringContext, ValueTyDefId, | 49 | TyDefId, TyLoweringContext, ValueTyDefId, |
51 | }; | 50 | }; |
52 | pub use traits::TraitEnvironment; | 51 | pub use traits::{chalk::Interner, TraitEnvironment}; |
53 | pub use types::*; | 52 | pub use types::*; |
54 | pub use walk::TypeWalk; | 53 | pub use walk::TypeWalk; |
55 | 54 | ||
@@ -57,8 +56,6 @@ pub use chalk_ir::{ | |||
57 | cast::Cast, AdtId, BoundVar, DebruijnIndex, Mutability, Safety, Scalar, TyVariableKind, | 56 | cast::Cast, AdtId, BoundVar, DebruijnIndex, Mutability, Safety, Scalar, TyVariableKind, |
58 | }; | 57 | }; |
59 | 58 | ||
60 | pub use crate::traits::chalk::Interner; | ||
61 | |||
62 | pub type ForeignDefId = chalk_ir::ForeignDefId<Interner>; | 59 | pub type ForeignDefId = chalk_ir::ForeignDefId<Interner>; |
63 | pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>; | 60 | pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>; |
64 | pub type FnDefId = chalk_ir::FnDefId<Interner>; | 61 | pub type FnDefId = chalk_ir::FnDefId<Interner>; |
@@ -76,46 +73,11 @@ pub type LifetimeOutlives = chalk_ir::LifetimeOutlives<Interner>; | |||
76 | 73 | ||
77 | pub type ChalkTraitId = chalk_ir::TraitId<Interner>; | 74 | pub type ChalkTraitId = chalk_ir::TraitId<Interner>; |
78 | 75 | ||
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>; | 76 | pub type FnSig = chalk_ir::FnSig<Interner>; |
100 | 77 | ||
101 | impl Substitution { | 78 | // FIXME: get rid of this |
102 | pub fn single(ty: Ty) -> Substitution { | 79 | pub fn subst_prefix(s: &Substitution, n: usize) -> Substitution { |
103 | Substitution::intern({ | 80 | Substitution::intern(s.interned()[..std::cmp::min(s.len(&Interner), n)].into()) |
104 | let mut v = SmallVec::new(); | ||
105 | v.push(ty.cast(&Interner)); | ||
106 | v | ||
107 | }) | ||
108 | } | ||
109 | |||
110 | pub fn prefix(&self, n: usize) -> Substitution { | ||
111 | Substitution::intern(self.interned()[..std::cmp::min(self.len(&Interner), n)].into()) | ||
112 | } | ||
113 | |||
114 | pub fn suffix(&self, n: usize) -> Substitution { | ||
115 | Substitution::intern( | ||
116 | self.interned()[self.len(&Interner) - std::cmp::min(self.len(&Interner), n)..].into(), | ||
117 | ) | ||
118 | } | ||
119 | } | 81 | } |
120 | 82 | ||
121 | /// Return an index of a parameter in the generic type parameter list by it's id. | 83 | /// Return an index of a parameter in the generic type parameter list by it's id. |
@@ -123,22 +85,11 @@ pub fn param_idx(db: &dyn HirDatabase, id: TypeParamId) -> Option<usize> { | |||
123 | generics(db.upcast(), id.parent).param_idx(id) | 85 | generics(db.upcast(), id.parent).param_idx(id) |
124 | } | 86 | } |
125 | 87 | ||
126 | impl<T> Binders<T> { | 88 | pub fn wrap_empty_binders<T>(value: T) -> Binders<T> |
127 | pub fn wrap_empty(value: T) -> Self | 89 | where |
128 | where | 90 | T: TypeWalk, |
129 | T: TypeWalk, | 91 | { |
130 | { | 92 | Binders::empty(&Interner, value.shifted_in_from(DebruijnIndex::ONE)) |
131 | Binders::empty(&Interner, value.shifted_in_from(DebruijnIndex::ONE)) | ||
132 | } | ||
133 | } | ||
134 | |||
135 | impl<T: TypeWalk> Binders<T> { | ||
136 | /// Substitutes all variables. | ||
137 | pub fn substitute(self, interner: &Interner, subst: &Substitution) -> T { | ||
138 | let (value, binders) = self.into_value_and_skipped_binders(); | ||
139 | assert_eq!(subst.len(interner), binders.len(interner)); | ||
140 | value.subst_bound_vars(subst) | ||
141 | } | ||
142 | } | 93 | } |
143 | 94 | ||
144 | pub fn make_only_type_binders<T>(num_vars: usize, value: T) -> Binders<T> { | 95 | pub fn make_only_type_binders<T>(num_vars: usize, value: T) -> Binders<T> { |
@@ -153,31 +104,11 @@ pub fn make_only_type_binders<T>(num_vars: usize, value: T) -> Binders<T> { | |||
153 | } | 104 | } |
154 | 105 | ||
155 | impl TraitRef { | 106 | impl TraitRef { |
156 | pub fn self_type_parameter(&self, interner: &Interner) -> &Ty { | ||
157 | &self.substitution.at(interner, 0).assert_ty_ref(interner) | ||
158 | } | ||
159 | |||
160 | pub fn hir_trait_id(&self) -> TraitId { | 107 | pub fn hir_trait_id(&self) -> TraitId { |
161 | from_chalk_trait_id(self.trait_id) | 108 | from_chalk_trait_id(self.trait_id) |
162 | } | 109 | } |
163 | } | 110 | } |
164 | 111 | ||
165 | impl WhereClause { | ||
166 | pub fn is_implemented(&self) -> bool { | ||
167 | matches!(self, WhereClause::Implemented(_)) | ||
168 | } | ||
169 | |||
170 | pub fn trait_ref(&self, db: &dyn HirDatabase) -> Option<TraitRef> { | ||
171 | match self { | ||
172 | WhereClause::Implemented(tr) => Some(tr.clone()), | ||
173 | WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(proj), .. }) => { | ||
174 | Some(proj.trait_ref(db)) | ||
175 | } | ||
176 | WhereClause::AliasEq(_) => None, | ||
177 | } | ||
178 | } | ||
179 | } | ||
180 | |||
181 | impl<T> Canonical<T> { | 112 | impl<T> Canonical<T> { |
182 | pub fn new(value: T, kinds: impl IntoIterator<Item = TyVariableKind>) -> Self { | 113 | pub fn new(value: T, kinds: impl IntoIterator<Item = TyVariableKind>) -> Self { |
183 | let kinds = kinds.into_iter().map(|tk| { | 114 | let kinds = kinds.into_iter().map(|tk| { |