]> git.lizzy.rs Git - rust.git/blob - crates/hir_ty/src/lib.rs
Allow unused
[rust.git] / crates / hir_ty / src / lib.rs
1 //! The type system. We currently use this to infer types for completion, hover
2 //! information and various assists.
3
4 #![allow(unused)]
5
6 #[allow(unused)]
7 macro_rules! eprintln {
8     ($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
9 }
10
11 mod autoderef;
12 pub mod primitive;
13 pub mod traits;
14 pub mod method_resolution;
15 mod op;
16 mod lower;
17 pub(crate) mod infer;
18 pub(crate) mod utils;
19 mod chalk_cast;
20 mod chalk_ext;
21 mod builder;
22 mod walk;
23
24 pub mod display;
25 pub mod db;
26 pub mod diagnostics;
27
28 #[cfg(test)]
29 mod tests;
30 #[cfg(test)]
31 mod test_db;
32
33 use std::sync::Arc;
34
35 use base_db::salsa;
36 use chalk_ir::UintTy;
37 use hir_def::{
38     expr::ExprId, type_ref::Rawness, ConstParamId, LifetimeParamId, TraitId, TypeAliasId,
39     TypeParamId,
40 };
41
42 use crate::{db::HirDatabase, display::HirDisplay, utils::generics};
43
44 pub use autoderef::autoderef;
45 pub use builder::TyBuilder;
46 pub use chalk_ext::*;
47 pub use infer::{could_unify, InferenceResult};
48 pub use lower::{
49     associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode,
50     TyDefId, TyLoweringContext, ValueTyDefId,
51 };
52 pub use traits::{chalk::Interner, TraitEnvironment};
53 pub use walk::TypeWalk;
54
55 pub use chalk_ir::{
56     cast::Cast, AdtId, BoundVar, DebruijnIndex, Mutability, Safety, Scalar, TyVariableKind,
57 };
58
59 pub type ForeignDefId = chalk_ir::ForeignDefId<Interner>;
60 pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>;
61 pub type FnDefId = chalk_ir::FnDefId<Interner>;
62 pub type ClosureId = chalk_ir::ClosureId<Interner>;
63 pub type OpaqueTyId = chalk_ir::OpaqueTyId<Interner>;
64 pub type PlaceholderIndex = chalk_ir::PlaceholderIndex;
65
66 pub type VariableKind = chalk_ir::VariableKind<Interner>;
67 pub type VariableKinds = chalk_ir::VariableKinds<Interner>;
68 pub type CanonicalVarKinds = chalk_ir::CanonicalVarKinds<Interner>;
69 pub type Binders<T> = chalk_ir::Binders<T>;
70 pub type Substitution = chalk_ir::Substitution<Interner>;
71 pub type GenericArg = chalk_ir::GenericArg<Interner>;
72 pub type GenericArgData = chalk_ir::GenericArgData<Interner>;
73
74 pub type Ty = chalk_ir::Ty<Interner>;
75 pub type TyKind = chalk_ir::TyKind<Interner>;
76 pub type DynTy = chalk_ir::DynTy<Interner>;
77 pub type FnPointer = chalk_ir::FnPointer<Interner>;
78 // pub type FnSubst = chalk_ir::FnSubst<Interner>;
79 pub use chalk_ir::FnSubst;
80 pub type ProjectionTy = chalk_ir::ProjectionTy<Interner>;
81 pub type AliasTy = chalk_ir::AliasTy<Interner>;
82 pub type OpaqueTy = chalk_ir::OpaqueTy<Interner>;
83 pub type InferenceVar = chalk_ir::InferenceVar;
84
85 pub type Lifetime = chalk_ir::Lifetime<Interner>;
86 pub type LifetimeData = chalk_ir::LifetimeData<Interner>;
87 pub type LifetimeOutlives = chalk_ir::LifetimeOutlives<Interner>;
88
89 pub type Const = chalk_ir::Const<Interner>;
90 pub type ConstData = chalk_ir::ConstData<Interner>;
91 pub type ConstValue = chalk_ir::ConstValue<Interner>;
92 pub type ConcreteConst = chalk_ir::ConcreteConst<Interner>;
93
94 pub type ChalkTraitId = chalk_ir::TraitId<Interner>;
95
96 pub type FnSig = chalk_ir::FnSig<Interner>;
97
98 pub type InEnvironment<T> = chalk_ir::InEnvironment<T>;
99 pub type DomainGoal = chalk_ir::DomainGoal<Interner>;
100 pub type AliasEq = chalk_ir::AliasEq<Interner>;
101 pub type Solution = chalk_solve::Solution<Interner>;
102 pub type ConstrainedSubst = chalk_ir::ConstrainedSubst<Interner>;
103 pub type Guidance = chalk_solve::Guidance<Interner>;
104 pub type WhereClause = chalk_ir::WhereClause<Interner>;
105
106 // FIXME: get rid of this
107 pub fn subst_prefix(s: &Substitution, n: usize) -> Substitution {
108     Substitution::intern(s.interned()[..std::cmp::min(s.len(&Interner), n)].into())
109 }
110
111 /// Return an index of a parameter in the generic type parameter list by it's id.
112 pub fn param_idx(db: &dyn HirDatabase, id: TypeParamId) -> Option<usize> {
113     generics(db.upcast(), id.parent).param_idx(id)
114 }
115
116 pub fn wrap_empty_binders<T>(value: T) -> Binders<T>
117 where
118     T: TypeWalk,
119 {
120     Binders::empty(&Interner, value.shifted_in_from(DebruijnIndex::ONE))
121 }
122
123 pub fn make_only_type_binders<T>(num_vars: usize, value: T) -> Binders<T> {
124     Binders::new(
125         VariableKinds::from_iter(
126             &Interner,
127             std::iter::repeat(chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General))
128                 .take(num_vars),
129         ),
130         value,
131     )
132 }
133
134 // FIXME: get rid of this
135 pub fn make_canonical<T>(
136     value: T,
137     kinds: impl IntoIterator<Item = TyVariableKind>,
138 ) -> Canonical<T> {
139     let kinds = kinds.into_iter().map(|tk| {
140         chalk_ir::CanonicalVarKind::new(
141             chalk_ir::VariableKind::Ty(tk),
142             chalk_ir::UniverseIndex::ROOT,
143         )
144     });
145     Canonical { value, binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds) }
146 }
147
148 pub type TraitRef = chalk_ir::TraitRef<Interner>;
149
150 pub type QuantifiedWhereClause = Binders<WhereClause>;
151
152 pub type QuantifiedWhereClauses = chalk_ir::QuantifiedWhereClauses<Interner>;
153
154 pub type Canonical<T> = chalk_ir::Canonical<T>;
155
156 /// A function signature as seen by type inference: Several parameter types and
157 /// one return type.
158 #[derive(Clone, PartialEq, Eq, Debug)]
159 pub struct CallableSig {
160     params_and_return: Arc<[Ty]>,
161     is_varargs: bool,
162 }
163
164 /// A polymorphic function signature.
165 pub type PolyFnSig = Binders<CallableSig>;
166
167 impl CallableSig {
168     pub fn from_params_and_return(mut params: Vec<Ty>, ret: Ty, is_varargs: bool) -> CallableSig {
169         params.push(ret);
170         CallableSig { params_and_return: params.into(), is_varargs }
171     }
172
173     pub fn from_fn_ptr(fn_ptr: &FnPointer) -> CallableSig {
174         CallableSig {
175             // FIXME: what to do about lifetime params? -> return PolyFnSig
176             params_and_return: fn_ptr
177                 .substitution
178                 .clone()
179                 .shifted_out_to(DebruijnIndex::ONE)
180                 .expect("unexpected lifetime vars in fn ptr")
181                 .0
182                 .interned()
183                 .iter()
184                 .map(|arg| arg.assert_ty_ref(&Interner).clone())
185                 .collect(),
186             is_varargs: fn_ptr.sig.variadic,
187         }
188     }
189
190     pub fn params(&self) -> &[Ty] {
191         &self.params_and_return[0..self.params_and_return.len() - 1]
192     }
193
194     pub fn ret(&self) -> &Ty {
195         &self.params_and_return[self.params_and_return.len() - 1]
196     }
197 }
198
199 #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
200 pub enum ImplTraitId {
201     ReturnTypeImplTrait(hir_def::FunctionId, u16),
202     AsyncBlockTypeImplTrait(hir_def::DefWithBodyId, ExprId),
203 }
204
205 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
206 pub struct ReturnTypeImplTraits {
207     pub(crate) impl_traits: Vec<ReturnTypeImplTrait>,
208 }
209
210 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
211 pub(crate) struct ReturnTypeImplTrait {
212     pub(crate) bounds: Binders<Vec<QuantifiedWhereClause>>,
213 }
214
215 pub fn to_foreign_def_id(id: TypeAliasId) -> ForeignDefId {
216     chalk_ir::ForeignDefId(salsa::InternKey::as_intern_id(&id))
217 }
218
219 pub fn from_foreign_def_id(id: ForeignDefId) -> TypeAliasId {
220     salsa::InternKey::from_intern_id(id.0)
221 }
222
223 pub fn to_assoc_type_id(id: TypeAliasId) -> AssocTypeId {
224     chalk_ir::AssocTypeId(salsa::InternKey::as_intern_id(&id))
225 }
226
227 pub fn from_assoc_type_id(id: AssocTypeId) -> TypeAliasId {
228     salsa::InternKey::from_intern_id(id.0)
229 }
230
231 pub fn from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> TypeParamId {
232     assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT);
233     let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx));
234     db.lookup_intern_type_param_id(interned_id)
235 }
236
237 pub fn to_placeholder_idx(db: &dyn HirDatabase, id: TypeParamId) -> PlaceholderIndex {
238     let interned_id = db.intern_type_param_id(id);
239     PlaceholderIndex {
240         ui: chalk_ir::UniverseIndex::ROOT,
241         idx: salsa::InternKey::as_intern_id(&interned_id).as_usize(),
242     }
243 }
244
245 pub fn lt_from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> LifetimeParamId {
246     assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT);
247     let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx));
248     db.lookup_intern_lifetime_param_id(interned_id)
249 }
250
251 pub fn const_from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> ConstParamId {
252     assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT);
253     let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx));
254     db.lookup_intern_const_param_id(interned_id)
255 }
256
257 pub fn to_chalk_trait_id(id: TraitId) -> ChalkTraitId {
258     chalk_ir::TraitId(salsa::InternKey::as_intern_id(&id))
259 }
260
261 pub fn from_chalk_trait_id(id: ChalkTraitId) -> TraitId {
262     salsa::InternKey::from_intern_id(id.0)
263 }
264
265 pub fn static_lifetime() -> Lifetime {
266     LifetimeData::Static.intern(&Interner)
267 }
268
269 pub fn dummy_usize_const() -> Const {
270     let usize_ty = chalk_ir::TyKind::Scalar(Scalar::Uint(UintTy::Usize)).intern(&Interner);
271     chalk_ir::ConstData {
272         ty: usize_ty,
273         value: chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst { interned: () }),
274     }
275     .intern(&Interner)
276 }