1 //! The type system. We currently use this to infer types for completion, hover
2 //! information and various assists.
5 macro_rules! eprintln {
6 ($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
24 pub mod method_resolution;
37 interner::HasInterner,
40 use hir_def::{expr::ExprId, type_ref::Rawness, TypeParamId};
42 use crate::{db::HirDatabase, display::HirDisplay, utils::generics};
44 pub use autoderef::autoderef;
45 pub use builder::TyBuilder;
47 pub use infer::{could_unify, InferenceResult};
48 pub use interner::Interner;
50 associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode,
51 TyDefId, TyLoweringContext, ValueTyDefId,
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,
58 pub use traits::TraitEnvironment;
59 pub use walk::TypeWalk;
62 cast::Cast, AdtId, BoundVar, DebruijnIndex, Mutability, Safety, Scalar, TyVariableKind,
65 pub type ForeignDefId = chalk_ir::ForeignDefId<Interner>;
66 pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>;
67 pub type FnDefId = chalk_ir::FnDefId<Interner>;
68 pub type ClosureId = chalk_ir::ClosureId<Interner>;
69 pub type OpaqueTyId = chalk_ir::OpaqueTyId<Interner>;
70 pub type PlaceholderIndex = chalk_ir::PlaceholderIndex;
72 pub type VariableKind = chalk_ir::VariableKind<Interner>;
73 pub type VariableKinds = chalk_ir::VariableKinds<Interner>;
74 pub type CanonicalVarKinds = chalk_ir::CanonicalVarKinds<Interner>;
75 pub type Binders<T> = chalk_ir::Binders<T>;
76 pub type Substitution = chalk_ir::Substitution<Interner>;
77 pub type GenericArg = chalk_ir::GenericArg<Interner>;
78 pub type GenericArgData = chalk_ir::GenericArgData<Interner>;
80 pub type Ty = chalk_ir::Ty<Interner>;
81 pub type TyKind = chalk_ir::TyKind<Interner>;
82 pub type DynTy = chalk_ir::DynTy<Interner>;
83 pub type FnPointer = chalk_ir::FnPointer<Interner>;
84 // pub type FnSubst = chalk_ir::FnSubst<Interner>;
85 pub use chalk_ir::FnSubst;
86 pub type ProjectionTy = chalk_ir::ProjectionTy<Interner>;
87 pub type AliasTy = chalk_ir::AliasTy<Interner>;
88 pub type OpaqueTy = chalk_ir::OpaqueTy<Interner>;
89 pub type InferenceVar = chalk_ir::InferenceVar;
91 pub type Lifetime = chalk_ir::Lifetime<Interner>;
92 pub type LifetimeData = chalk_ir::LifetimeData<Interner>;
93 pub type LifetimeOutlives = chalk_ir::LifetimeOutlives<Interner>;
95 pub type Const = chalk_ir::Const<Interner>;
96 pub type ConstData = chalk_ir::ConstData<Interner>;
97 pub type ConstValue = chalk_ir::ConstValue<Interner>;
98 pub type ConcreteConst = chalk_ir::ConcreteConst<Interner>;
100 pub type ChalkTraitId = chalk_ir::TraitId<Interner>;
101 pub type TraitRef = chalk_ir::TraitRef<Interner>;
102 pub type QuantifiedWhereClause = Binders<WhereClause>;
103 pub type QuantifiedWhereClauses = chalk_ir::QuantifiedWhereClauses<Interner>;
104 pub type Canonical<T> = chalk_ir::Canonical<T>;
106 pub type FnSig = chalk_ir::FnSig<Interner>;
108 pub type InEnvironment<T> = chalk_ir::InEnvironment<T>;
109 pub type DomainGoal = chalk_ir::DomainGoal<Interner>;
110 pub type AliasEq = chalk_ir::AliasEq<Interner>;
111 pub type Solution = chalk_solve::Solution<Interner>;
112 pub type ConstrainedSubst = chalk_ir::ConstrainedSubst<Interner>;
113 pub type Guidance = chalk_solve::Guidance<Interner>;
114 pub type WhereClause = chalk_ir::WhereClause<Interner>;
116 // FIXME: get rid of this
117 pub fn subst_prefix(s: &Substitution, n: usize) -> Substitution {
118 Substitution::from_iter(
120 s.as_slice(&Interner)[..std::cmp::min(s.len(&Interner), n)].iter().cloned(),
124 /// Return an index of a parameter in the generic type parameter list by it's id.
125 pub fn param_idx(db: &dyn HirDatabase, id: TypeParamId) -> Option<usize> {
126 generics(db.upcast(), id.parent).param_idx(id)
129 pub(crate) fn wrap_empty_binders<T>(value: T) -> Binders<T>
131 T: Fold<Interner, Result = T> + HasInterner<Interner = Interner>,
133 Binders::empty(&Interner, value.shifted_in_from(&Interner, DebruijnIndex::ONE))
136 pub(crate) fn make_only_type_binders<T: HasInterner<Interner = Interner>>(
141 VariableKinds::from_iter(
143 std::iter::repeat(chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General))
150 // FIXME: get rid of this
151 pub fn make_canonical<T: HasInterner<Interner = Interner>>(
153 kinds: impl IntoIterator<Item = TyVariableKind>,
155 let kinds = kinds.into_iter().map(|tk| {
156 chalk_ir::CanonicalVarKind::new(
157 chalk_ir::VariableKind::Ty(tk),
158 chalk_ir::UniverseIndex::ROOT,
161 Canonical { value, binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds) }
164 /// A function signature as seen by type inference: Several parameter types and
166 #[derive(Clone, PartialEq, Eq, Debug)]
167 pub struct CallableSig {
168 params_and_return: Arc<[Ty]>,
172 has_interner!(CallableSig);
174 /// A polymorphic function signature.
175 pub type PolyFnSig = Binders<CallableSig>;
178 pub fn from_params_and_return(mut params: Vec<Ty>, ret: Ty, is_varargs: bool) -> CallableSig {
180 CallableSig { params_and_return: params.into(), is_varargs }
183 pub fn from_fn_ptr(fn_ptr: &FnPointer) -> CallableSig {
185 // FIXME: what to do about lifetime params? -> return PolyFnSig
186 params_and_return: fn_ptr
189 .shifted_out_to(&Interner, DebruijnIndex::ONE)
190 .expect("unexpected lifetime vars in fn ptr")
194 .map(|arg| arg.assert_ty_ref(&Interner).clone())
196 is_varargs: fn_ptr.sig.variadic,
200 pub fn params(&self) -> &[Ty] {
201 &self.params_and_return[0..self.params_and_return.len() - 1]
204 pub fn ret(&self) -> &Ty {
205 &self.params_and_return[self.params_and_return.len() - 1]
209 impl Fold<Interner> for CallableSig {
210 type Result = CallableSig;
214 folder: &mut dyn chalk_ir::fold::Folder<'i, Interner>,
215 outer_binder: DebruijnIndex,
216 ) -> chalk_ir::Fallible<Self::Result>
220 let vec = self.params_and_return.to_vec();
221 let folded = vec.fold_with(folder, outer_binder)?;
222 Ok(CallableSig { params_and_return: folded.into(), is_varargs: self.is_varargs })
226 #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
227 pub enum ImplTraitId {
228 ReturnTypeImplTrait(hir_def::FunctionId, u16),
229 AsyncBlockTypeImplTrait(hir_def::DefWithBodyId, ExprId),
232 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
233 pub struct ReturnTypeImplTraits {
234 pub(crate) impl_traits: Vec<ReturnTypeImplTrait>,
237 has_interner!(ReturnTypeImplTraits);
239 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
240 pub(crate) struct ReturnTypeImplTrait {
241 pub(crate) bounds: Binders<Vec<QuantifiedWhereClause>>,
244 pub fn static_lifetime() -> Lifetime {
245 LifetimeData::Static.intern(&Interner)
248 pub fn dummy_usize_const() -> Const {
249 let usize_ty = chalk_ir::TyKind::Scalar(Scalar::Uint(UintTy::Usize)).intern(&Interner);
250 chalk_ir::ConstData {
252 value: chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst { interned: () }),
257 pub(crate) fn fold_free_vars<T: HasInterner<Interner = Interner> + Fold<Interner>>(
259 f: impl FnMut(BoundVar, DebruijnIndex) -> Ty,
261 use chalk_ir::{fold::Folder, Fallible};
262 struct FreeVarFolder<F>(F);
263 impl<'i, F: FnMut(BoundVar, DebruijnIndex) -> Ty + 'i> Folder<'i, Interner> for FreeVarFolder<F> {
264 fn as_dyn(&mut self) -> &mut dyn Folder<'i, Interner> {
268 fn interner(&self) -> &'i Interner {
275 outer_binder: DebruijnIndex,
277 Ok(self.0(bound_var, outer_binder))
280 t.fold_with(&mut FreeVarFolder(f), DebruijnIndex::INNERMOST).expect("fold failed unexpectedly")
283 pub(crate) fn fold_tys<T: HasInterner<Interner = Interner> + Fold<Interner>>(
285 f: impl FnMut(Ty, DebruijnIndex) -> Ty,
286 binders: DebruijnIndex,
289 fold::{Folder, SuperFold},
292 struct TyFolder<F>(F);
293 impl<'i, F: FnMut(Ty, DebruijnIndex) -> Ty + 'i> Folder<'i, Interner> for TyFolder<F> {
294 fn as_dyn(&mut self) -> &mut dyn Folder<'i, Interner> {
298 fn interner(&self) -> &'i Interner {
302 fn fold_ty(&mut self, ty: Ty, outer_binder: DebruijnIndex) -> Fallible<Ty> {
303 let ty = ty.super_fold_with(self.as_dyn(), outer_binder)?;
304 Ok(self.0(ty, outer_binder))
307 t.fold_with(&mut TyFolder(f), binders).expect("fold failed unexpectedly")