1 //! The type system. We currently use this to infer types for completion, hover
2 //! information and various assists.
4 macro_rules! eprintln {
5 ($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
11 pub mod method_resolution;
36 expr::ExprId, type_ref::Rawness, ConstParamId, LifetimeParamId, TraitId, TypeAliasId,
40 use crate::{db::HirDatabase, display::HirDisplay, utils::generics};
42 pub use autoderef::autoderef;
43 pub use builder::TyBuilder;
45 pub use infer::{could_unify, InferenceResult};
47 associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode,
48 TyDefId, TyLoweringContext, ValueTyDefId,
50 pub use traits::{chalk::Interner, TraitEnvironment};
52 pub use walk::TypeWalk;
55 cast::Cast, AdtId, BoundVar, DebruijnIndex, Mutability, Safety, Scalar, TyVariableKind,
58 pub type ForeignDefId = chalk_ir::ForeignDefId<Interner>;
59 pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>;
60 pub type FnDefId = chalk_ir::FnDefId<Interner>;
61 pub type ClosureId = chalk_ir::ClosureId<Interner>;
62 pub type OpaqueTyId = chalk_ir::OpaqueTyId<Interner>;
63 pub type PlaceholderIndex = chalk_ir::PlaceholderIndex;
65 pub type VariableKind = chalk_ir::VariableKind<Interner>;
66 pub type VariableKinds = chalk_ir::VariableKinds<Interner>;
67 pub type CanonicalVarKinds = chalk_ir::CanonicalVarKinds<Interner>;
69 pub type Lifetime = chalk_ir::Lifetime<Interner>;
70 pub type LifetimeData = chalk_ir::LifetimeData<Interner>;
71 pub type LifetimeOutlives = chalk_ir::LifetimeOutlives<Interner>;
73 pub type Const = chalk_ir::Const<Interner>;
74 pub type ConstData = chalk_ir::ConstData<Interner>;
75 pub type ConstValue = chalk_ir::ConstValue<Interner>;
76 pub type ConcreteConst = chalk_ir::ConcreteConst<Interner>;
78 pub type ChalkTraitId = chalk_ir::TraitId<Interner>;
80 pub type FnSig = chalk_ir::FnSig<Interner>;
82 // FIXME: get rid of this
83 pub fn subst_prefix(s: &Substitution, n: usize) -> Substitution {
84 Substitution::intern(s.interned()[..std::cmp::min(s.len(&Interner), n)].into())
87 /// Return an index of a parameter in the generic type parameter list by it's id.
88 pub fn param_idx(db: &dyn HirDatabase, id: TypeParamId) -> Option<usize> {
89 generics(db.upcast(), id.parent).param_idx(id)
92 pub fn wrap_empty_binders<T>(value: T) -> Binders<T>
96 Binders::empty(&Interner, value.shifted_in_from(DebruijnIndex::ONE))
99 pub fn make_only_type_binders<T>(num_vars: usize, value: T) -> Binders<T> {
101 VariableKinds::from_iter(
103 std::iter::repeat(chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General))
110 // FIXME: get rid of this
111 pub fn make_canonical<T>(
113 kinds: impl IntoIterator<Item = TyVariableKind>,
115 let kinds = kinds.into_iter().map(|tk| {
116 chalk_ir::CanonicalVarKind::new(
117 chalk_ir::VariableKind::Ty(tk),
118 chalk_ir::UniverseIndex::ROOT,
121 Canonical { value, binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds) }
124 /// A function signature as seen by type inference: Several parameter types and
126 #[derive(Clone, PartialEq, Eq, Debug)]
127 pub struct CallableSig {
128 params_and_return: Arc<[Ty]>,
132 /// A polymorphic function signature.
133 pub type PolyFnSig = Binders<CallableSig>;
136 pub fn from_params_and_return(mut params: Vec<Ty>, ret: Ty, is_varargs: bool) -> CallableSig {
138 CallableSig { params_and_return: params.into(), is_varargs }
141 pub fn from_fn_ptr(fn_ptr: &FnPointer) -> CallableSig {
143 // FIXME: what to do about lifetime params? -> return PolyFnSig
144 params_and_return: fn_ptr
147 .shifted_out_to(DebruijnIndex::ONE)
148 .expect("unexpected lifetime vars in fn ptr")
152 .map(|arg| arg.assert_ty_ref(&Interner).clone())
154 is_varargs: fn_ptr.sig.variadic,
158 pub fn params(&self) -> &[Ty] {
159 &self.params_and_return[0..self.params_and_return.len() - 1]
162 pub fn ret(&self) -> &Ty {
163 &self.params_and_return[self.params_and_return.len() - 1]
169 #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
170 pub enum ImplTraitId {
171 ReturnTypeImplTrait(hir_def::FunctionId, u16),
172 AsyncBlockTypeImplTrait(hir_def::DefWithBodyId, ExprId),
175 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
176 pub struct ReturnTypeImplTraits {
177 pub(crate) impl_traits: Vec<ReturnTypeImplTrait>,
180 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
181 pub(crate) struct ReturnTypeImplTrait {
182 pub(crate) bounds: Binders<Vec<QuantifiedWhereClause>>,
185 pub fn to_foreign_def_id(id: TypeAliasId) -> ForeignDefId {
186 chalk_ir::ForeignDefId(salsa::InternKey::as_intern_id(&id))
189 pub fn from_foreign_def_id(id: ForeignDefId) -> TypeAliasId {
190 salsa::InternKey::from_intern_id(id.0)
193 pub fn to_assoc_type_id(id: TypeAliasId) -> AssocTypeId {
194 chalk_ir::AssocTypeId(salsa::InternKey::as_intern_id(&id))
197 pub fn from_assoc_type_id(id: AssocTypeId) -> TypeAliasId {
198 salsa::InternKey::from_intern_id(id.0)
201 pub fn from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> TypeParamId {
202 assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT);
203 let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx));
204 db.lookup_intern_type_param_id(interned_id)
207 pub fn to_placeholder_idx(db: &dyn HirDatabase, id: TypeParamId) -> PlaceholderIndex {
208 let interned_id = db.intern_type_param_id(id);
210 ui: chalk_ir::UniverseIndex::ROOT,
211 idx: salsa::InternKey::as_intern_id(&interned_id).as_usize(),
215 pub fn lt_from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> LifetimeParamId {
216 assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT);
217 let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx));
218 db.lookup_intern_lifetime_param_id(interned_id)
221 pub fn const_from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> ConstParamId {
222 assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT);
223 let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx));
224 db.lookup_intern_const_param_id(interned_id)
227 pub fn to_chalk_trait_id(id: TraitId) -> ChalkTraitId {
228 chalk_ir::TraitId(salsa::InternKey::as_intern_id(&id))
231 pub fn from_chalk_trait_id(id: ChalkTraitId) -> TraitId {
232 salsa::InternKey::from_intern_id(id.0)
235 pub fn static_lifetime() -> Lifetime {
236 LifetimeData::Static.intern(&Interner)
239 pub fn dummy_usize_const() -> Const {
240 let usize_ty = chalk_ir::TyKind::Scalar(Scalar::Uint(UintTy::Usize)).intern(&Interner);
241 chalk_ir::ConstData {
243 value: chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst { interned: () }),