diff options
Diffstat (limited to 'crates/hir_ty/src/lib.rs')
-rw-r--r-- | crates/hir_ty/src/lib.rs | 113 |
1 files changed, 34 insertions, 79 deletions
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 874c95411..0505fa4ae 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs | |||
@@ -7,21 +7,23 @@ macro_rules! eprintln { | |||
7 | } | 7 | } |
8 | 8 | ||
9 | mod autoderef; | 9 | mod autoderef; |
10 | pub mod primitive; | ||
11 | pub mod traits; | ||
12 | pub mod method_resolution; | ||
13 | mod op; | ||
14 | mod lower; | ||
15 | pub(crate) mod infer; | ||
16 | pub(crate) mod utils; | ||
17 | mod chalk_cast; | ||
18 | mod chalk_ext; | ||
19 | mod builder; | 10 | mod builder; |
11 | mod chalk_db; | ||
12 | mod chalk_ext; | ||
13 | mod infer; | ||
14 | mod interner; | ||
15 | mod lower; | ||
16 | mod mapping; | ||
17 | mod op; | ||
18 | mod tls; | ||
19 | mod utils; | ||
20 | mod walk; | 20 | mod walk; |
21 | |||
22 | pub mod display; | ||
23 | pub mod db; | 21 | pub mod db; |
24 | pub mod diagnostics; | 22 | pub mod diagnostics; |
23 | pub mod display; | ||
24 | pub mod method_resolution; | ||
25 | pub mod primitive; | ||
26 | pub mod traits; | ||
25 | 27 | ||
26 | #[cfg(test)] | 28 | #[cfg(test)] |
27 | mod tests; | 29 | mod tests; |
@@ -30,16 +32,12 @@ mod test_db; | |||
30 | 32 | ||
31 | use std::sync::Arc; | 33 | use std::sync::Arc; |
32 | 34 | ||
33 | use base_db::salsa; | ||
34 | use chalk_ir::{ | 35 | use chalk_ir::{ |
35 | fold::{Fold, Shift}, | 36 | fold::{Fold, Shift}, |
36 | interner::HasInterner, | 37 | interner::HasInterner, |
37 | UintTy, | 38 | UintTy, |
38 | }; | 39 | }; |
39 | use hir_def::{ | 40 | use hir_def::{expr::ExprId, type_ref::Rawness, TypeParamId}; |
40 | expr::ExprId, type_ref::Rawness, ConstParamId, LifetimeParamId, TraitId, TypeAliasId, | ||
41 | TypeParamId, | ||
42 | }; | ||
43 | 41 | ||
44 | use crate::{db::HirDatabase, display::HirDisplay, utils::generics}; | 42 | use crate::{db::HirDatabase, display::HirDisplay, utils::generics}; |
45 | 43 | ||
@@ -47,11 +45,18 @@ pub use autoderef::autoderef; | |||
47 | pub use builder::TyBuilder; | 45 | pub use builder::TyBuilder; |
48 | pub use chalk_ext::*; | 46 | pub use chalk_ext::*; |
49 | pub use infer::{could_unify, InferenceResult}; | 47 | pub use infer::{could_unify, InferenceResult}; |
48 | pub use interner::Interner; | ||
50 | pub use lower::{ | 49 | pub use lower::{ |
51 | associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode, | 50 | associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode, |
52 | TyDefId, TyLoweringContext, ValueTyDefId, | 51 | TyDefId, TyLoweringContext, ValueTyDefId, |
53 | }; | 52 | }; |
54 | pub use traits::{chalk::Interner, TraitEnvironment}; | 53 | pub use mapping::{ |
54 | const_from_placeholder_idx, from_assoc_type_id, from_chalk_trait_id, from_foreign_def_id, | ||
55 | from_placeholder_idx, lt_from_placeholder_idx, to_assoc_type_id, to_chalk_trait_id, | ||
56 | to_foreign_def_id, to_placeholder_idx, | ||
57 | }; | ||
58 | pub use traits::TraitEnvironment; | ||
59 | pub use utils::all_super_traits; | ||
55 | pub use walk::TypeWalk; | 60 | pub use walk::TypeWalk; |
56 | 61 | ||
57 | pub use chalk_ir::{ | 62 | pub use chalk_ir::{ |
@@ -94,6 +99,10 @@ pub type ConstValue = chalk_ir::ConstValue<Interner>; | |||
94 | pub type ConcreteConst = chalk_ir::ConcreteConst<Interner>; | 99 | pub type ConcreteConst = chalk_ir::ConcreteConst<Interner>; |
95 | 100 | ||
96 | pub type ChalkTraitId = chalk_ir::TraitId<Interner>; | 101 | pub type ChalkTraitId = chalk_ir::TraitId<Interner>; |
102 | pub type TraitRef = chalk_ir::TraitRef<Interner>; | ||
103 | pub type QuantifiedWhereClause = Binders<WhereClause>; | ||
104 | pub type QuantifiedWhereClauses = chalk_ir::QuantifiedWhereClauses<Interner>; | ||
105 | pub type Canonical<T> = chalk_ir::Canonical<T>; | ||
97 | 106 | ||
98 | pub type FnSig = chalk_ir::FnSig<Interner>; | 107 | pub type FnSig = chalk_ir::FnSig<Interner>; |
99 | 108 | ||
@@ -109,7 +118,7 @@ pub type WhereClause = chalk_ir::WhereClause<Interner>; | |||
109 | pub fn subst_prefix(s: &Substitution, n: usize) -> Substitution { | 118 | pub fn subst_prefix(s: &Substitution, n: usize) -> Substitution { |
110 | Substitution::from_iter( | 119 | Substitution::from_iter( |
111 | &Interner, | 120 | &Interner, |
112 | s.interned()[..std::cmp::min(s.len(&Interner), n)].iter().cloned(), | 121 | s.as_slice(&Interner)[..std::cmp::min(s.len(&Interner), n)].iter().cloned(), |
113 | ) | 122 | ) |
114 | } | 123 | } |
115 | 124 | ||
@@ -118,14 +127,14 @@ pub fn param_idx(db: &dyn HirDatabase, id: TypeParamId) -> Option<usize> { | |||
118 | generics(db.upcast(), id.parent).param_idx(id) | 127 | generics(db.upcast(), id.parent).param_idx(id) |
119 | } | 128 | } |
120 | 129 | ||
121 | pub fn wrap_empty_binders<T>(value: T) -> Binders<T> | 130 | pub(crate) fn wrap_empty_binders<T>(value: T) -> Binders<T> |
122 | where | 131 | where |
123 | T: Fold<Interner, Result = T> + HasInterner<Interner = Interner>, | 132 | T: Fold<Interner, Result = T> + HasInterner<Interner = Interner>, |
124 | { | 133 | { |
125 | Binders::empty(&Interner, value.shifted_in_from(&Interner, DebruijnIndex::ONE)) | 134 | Binders::empty(&Interner, value.shifted_in_from(&Interner, DebruijnIndex::ONE)) |
126 | } | 135 | } |
127 | 136 | ||
128 | pub fn make_only_type_binders<T: HasInterner<Interner = Interner>>( | 137 | pub(crate) fn make_only_type_binders<T: HasInterner<Interner = Interner>>( |
129 | num_vars: usize, | 138 | num_vars: usize, |
130 | value: T, | 139 | value: T, |
131 | ) -> Binders<T> { | 140 | ) -> Binders<T> { |
@@ -153,14 +162,6 @@ pub fn make_canonical<T: HasInterner<Interner = Interner>>( | |||
153 | Canonical { value, binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds) } | 162 | Canonical { value, binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds) } |
154 | } | 163 | } |
155 | 164 | ||
156 | pub type TraitRef = chalk_ir::TraitRef<Interner>; | ||
157 | |||
158 | pub type QuantifiedWhereClause = Binders<WhereClause>; | ||
159 | |||
160 | pub type QuantifiedWhereClauses = chalk_ir::QuantifiedWhereClauses<Interner>; | ||
161 | |||
162 | pub type Canonical<T> = chalk_ir::Canonical<T>; | ||
163 | |||
164 | /// A function signature as seen by type inference: Several parameter types and | 165 | /// A function signature as seen by type inference: Several parameter types and |
165 | /// one return type. | 166 | /// one return type. |
166 | #[derive(Clone, PartialEq, Eq, Debug)] | 167 | #[derive(Clone, PartialEq, Eq, Debug)] |
@@ -169,6 +170,8 @@ pub struct CallableSig { | |||
169 | is_varargs: bool, | 170 | is_varargs: bool, |
170 | } | 171 | } |
171 | 172 | ||
173 | has_interner!(CallableSig); | ||
174 | |||
172 | /// A polymorphic function signature. | 175 | /// A polymorphic function signature. |
173 | pub type PolyFnSig = Binders<CallableSig>; | 176 | pub type PolyFnSig = Binders<CallableSig>; |
174 | 177 | ||
@@ -187,7 +190,7 @@ impl CallableSig { | |||
187 | .shifted_out_to(&Interner, DebruijnIndex::ONE) | 190 | .shifted_out_to(&Interner, DebruijnIndex::ONE) |
188 | .expect("unexpected lifetime vars in fn ptr") | 191 | .expect("unexpected lifetime vars in fn ptr") |
189 | .0 | 192 | .0 |
190 | .interned() | 193 | .as_slice(&Interner) |
191 | .iter() | 194 | .iter() |
192 | .map(|arg| arg.assert_ty_ref(&Interner).clone()) | 195 | .map(|arg| arg.assert_ty_ref(&Interner).clone()) |
193 | .collect(), | 196 | .collect(), |
@@ -232,61 +235,13 @@ pub struct ReturnTypeImplTraits { | |||
232 | pub(crate) impl_traits: Vec<ReturnTypeImplTrait>, | 235 | pub(crate) impl_traits: Vec<ReturnTypeImplTrait>, |
233 | } | 236 | } |
234 | 237 | ||
238 | has_interner!(ReturnTypeImplTraits); | ||
239 | |||
235 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] | 240 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
236 | pub(crate) struct ReturnTypeImplTrait { | 241 | pub(crate) struct ReturnTypeImplTrait { |
237 | pub(crate) bounds: Binders<Vec<QuantifiedWhereClause>>, | 242 | pub(crate) bounds: Binders<Vec<QuantifiedWhereClause>>, |
238 | } | 243 | } |
239 | 244 | ||
240 | pub fn to_foreign_def_id(id: TypeAliasId) -> ForeignDefId { | ||
241 | chalk_ir::ForeignDefId(salsa::InternKey::as_intern_id(&id)) | ||
242 | } | ||
243 | |||
244 | pub fn from_foreign_def_id(id: ForeignDefId) -> TypeAliasId { | ||
245 | salsa::InternKey::from_intern_id(id.0) | ||
246 | } | ||
247 | |||
248 | pub fn to_assoc_type_id(id: TypeAliasId) -> AssocTypeId { | ||
249 | chalk_ir::AssocTypeId(salsa::InternKey::as_intern_id(&id)) | ||
250 | } | ||
251 | |||
252 | pub fn from_assoc_type_id(id: AssocTypeId) -> TypeAliasId { | ||
253 | salsa::InternKey::from_intern_id(id.0) | ||
254 | } | ||
255 | |||
256 | pub fn from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> TypeParamId { | ||
257 | assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT); | ||
258 | let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx)); | ||
259 | db.lookup_intern_type_param_id(interned_id) | ||
260 | } | ||
261 | |||
262 | pub fn to_placeholder_idx(db: &dyn HirDatabase, id: TypeParamId) -> PlaceholderIndex { | ||
263 | let interned_id = db.intern_type_param_id(id); | ||
264 | PlaceholderIndex { | ||
265 | ui: chalk_ir::UniverseIndex::ROOT, | ||
266 | idx: salsa::InternKey::as_intern_id(&interned_id).as_usize(), | ||
267 | } | ||
268 | } | ||
269 | |||
270 | pub fn lt_from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> LifetimeParamId { | ||
271 | assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT); | ||
272 | let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx)); | ||
273 | db.lookup_intern_lifetime_param_id(interned_id) | ||
274 | } | ||
275 | |||
276 | pub fn const_from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> ConstParamId { | ||
277 | assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT); | ||
278 | let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx)); | ||
279 | db.lookup_intern_const_param_id(interned_id) | ||
280 | } | ||
281 | |||
282 | pub fn to_chalk_trait_id(id: TraitId) -> ChalkTraitId { | ||
283 | chalk_ir::TraitId(salsa::InternKey::as_intern_id(&id)) | ||
284 | } | ||
285 | |||
286 | pub fn from_chalk_trait_id(id: ChalkTraitId) -> TraitId { | ||
287 | salsa::InternKey::from_intern_id(id.0) | ||
288 | } | ||
289 | |||
290 | pub fn static_lifetime() -> Lifetime { | 245 | pub fn static_lifetime() -> Lifetime { |
291 | LifetimeData::Static.intern(&Interner) | 246 | LifetimeData::Static.intern(&Interner) |
292 | } | 247 | } |