1 //! The type system. We currently use this to infer types for completion, hover
2 //! information and various assists.
7 macro_rules! eprintln {
8 ($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
14 pub mod method_resolution;
38 expr::ExprId, type_ref::Rawness, ConstParamId, LifetimeParamId, TraitId, TypeAliasId,
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};
49 associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode,
50 TyDefId, TyLoweringContext, ValueTyDefId,
52 pub use traits::{chalk::Interner, TraitEnvironment};
53 pub use walk::TypeWalk;
56 cast::Cast, AdtId, BoundVar, DebruijnIndex, Mutability, Safety, Scalar, TyVariableKind,
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;
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>;
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;
85 pub type Lifetime = chalk_ir::Lifetime<Interner>;
86 pub type LifetimeData = chalk_ir::LifetimeData<Interner>;
87 pub type LifetimeOutlives = chalk_ir::LifetimeOutlives<Interner>;
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>;
94 pub type ChalkTraitId = chalk_ir::TraitId<Interner>;
96 pub type FnSig = chalk_ir::FnSig<Interner>;
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>;
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())
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)
116 pub fn wrap_empty_binders<T>(value: T) -> Binders<T>
120 Binders::empty(&Interner, value.shifted_in_from(DebruijnIndex::ONE))
123 pub fn make_only_type_binders<T>(num_vars: usize, value: T) -> Binders<T> {
125 VariableKinds::from_iter(
127 std::iter::repeat(chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General))
134 // FIXME: get rid of this
135 pub fn make_canonical<T>(
137 kinds: impl IntoIterator<Item = TyVariableKind>,
139 let kinds = kinds.into_iter().map(|tk| {
140 chalk_ir::CanonicalVarKind::new(
141 chalk_ir::VariableKind::Ty(tk),
142 chalk_ir::UniverseIndex::ROOT,
145 Canonical { value, binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds) }
148 pub type TraitRef = chalk_ir::TraitRef<Interner>;
150 pub type QuantifiedWhereClause = Binders<WhereClause>;
152 pub type QuantifiedWhereClauses = chalk_ir::QuantifiedWhereClauses<Interner>;
154 pub type Canonical<T> = chalk_ir::Canonical<T>;
156 /// A function signature as seen by type inference: Several parameter types and
158 #[derive(Clone, PartialEq, Eq, Debug)]
159 pub struct CallableSig {
160 params_and_return: Arc<[Ty]>,
164 /// A polymorphic function signature.
165 pub type PolyFnSig = Binders<CallableSig>;
168 pub fn from_params_and_return(mut params: Vec<Ty>, ret: Ty, is_varargs: bool) -> CallableSig {
170 CallableSig { params_and_return: params.into(), is_varargs }
173 pub fn from_fn_ptr(fn_ptr: &FnPointer) -> CallableSig {
175 // FIXME: what to do about lifetime params? -> return PolyFnSig
176 params_and_return: fn_ptr
179 .shifted_out_to(DebruijnIndex::ONE)
180 .expect("unexpected lifetime vars in fn ptr")
184 .map(|arg| arg.assert_ty_ref(&Interner).clone())
186 is_varargs: fn_ptr.sig.variadic,
190 pub fn params(&self) -> &[Ty] {
191 &self.params_and_return[0..self.params_and_return.len() - 1]
194 pub fn ret(&self) -> &Ty {
195 &self.params_and_return[self.params_and_return.len() - 1]
199 #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
200 pub enum ImplTraitId {
201 ReturnTypeImplTrait(hir_def::FunctionId, u16),
202 AsyncBlockTypeImplTrait(hir_def::DefWithBodyId, ExprId),
205 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
206 pub struct ReturnTypeImplTraits {
207 pub(crate) impl_traits: Vec<ReturnTypeImplTrait>,
210 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
211 pub(crate) struct ReturnTypeImplTrait {
212 pub(crate) bounds: Binders<Vec<QuantifiedWhereClause>>,
215 pub fn to_foreign_def_id(id: TypeAliasId) -> ForeignDefId {
216 chalk_ir::ForeignDefId(salsa::InternKey::as_intern_id(&id))
219 pub fn from_foreign_def_id(id: ForeignDefId) -> TypeAliasId {
220 salsa::InternKey::from_intern_id(id.0)
223 pub fn to_assoc_type_id(id: TypeAliasId) -> AssocTypeId {
224 chalk_ir::AssocTypeId(salsa::InternKey::as_intern_id(&id))
227 pub fn from_assoc_type_id(id: AssocTypeId) -> TypeAliasId {
228 salsa::InternKey::from_intern_id(id.0)
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)
237 pub fn to_placeholder_idx(db: &dyn HirDatabase, id: TypeParamId) -> PlaceholderIndex {
238 let interned_id = db.intern_type_param_id(id);
240 ui: chalk_ir::UniverseIndex::ROOT,
241 idx: salsa::InternKey::as_intern_id(&interned_id).as_usize(),
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)
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)
257 pub fn to_chalk_trait_id(id: TraitId) -> ChalkTraitId {
258 chalk_ir::TraitId(salsa::InternKey::as_intern_id(&id))
261 pub fn from_chalk_trait_id(id: ChalkTraitId) -> TraitId {
262 salsa::InternKey::from_intern_id(id.0)
265 pub fn static_lifetime() -> Lifetime {
266 LifetimeData::Static.intern(&Interner)
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 {
273 value: chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst { interned: () }),