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;
35 expr::ExprId, type_ref::Rawness, ConstParamId, LifetimeParamId, TraitId, TypeAliasId,
39 use crate::{db::HirDatabase, display::HirDisplay, utils::generics};
41 pub use autoderef::autoderef;
42 pub use builder::TyBuilder;
44 pub use infer::{could_unify, InferenceResult};
46 associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode,
47 TyDefId, TyLoweringContext, ValueTyDefId,
49 pub use traits::{chalk::Interner, TraitEnvironment};
50 pub use walk::TypeWalk;
53 cast::Cast, AdtId, BoundVar, DebruijnIndex, Mutability, Safety, Scalar, TyVariableKind,
56 pub type ForeignDefId = chalk_ir::ForeignDefId<Interner>;
57 pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>;
58 pub type FnDefId = chalk_ir::FnDefId<Interner>;
59 pub type ClosureId = chalk_ir::ClosureId<Interner>;
60 pub type OpaqueTyId = chalk_ir::OpaqueTyId<Interner>;
61 pub type PlaceholderIndex = chalk_ir::PlaceholderIndex;
63 pub type VariableKind = chalk_ir::VariableKind<Interner>;
64 pub type VariableKinds = chalk_ir::VariableKinds<Interner>;
65 pub type CanonicalVarKinds = chalk_ir::CanonicalVarKinds<Interner>;
66 pub type Binders<T> = chalk_ir::Binders<T>;
67 pub type Substitution = chalk_ir::Substitution<Interner>;
68 pub type GenericArg = chalk_ir::GenericArg<Interner>;
69 pub type GenericArgData = chalk_ir::GenericArgData<Interner>;
71 pub type Ty = chalk_ir::Ty<Interner>;
72 pub type TyKind = chalk_ir::TyKind<Interner>;
73 pub type DynTy = chalk_ir::DynTy<Interner>;
74 pub type FnPointer = chalk_ir::FnPointer<Interner>;
75 // pub type FnSubst = chalk_ir::FnSubst<Interner>;
76 pub use chalk_ir::FnSubst;
77 pub type ProjectionTy = chalk_ir::ProjectionTy<Interner>;
78 pub type AliasTy = chalk_ir::AliasTy<Interner>;
79 pub type OpaqueTy = chalk_ir::OpaqueTy<Interner>;
80 pub type InferenceVar = chalk_ir::InferenceVar;
82 pub type Lifetime = chalk_ir::Lifetime<Interner>;
83 pub type LifetimeData = chalk_ir::LifetimeData<Interner>;
84 pub type LifetimeOutlives = chalk_ir::LifetimeOutlives<Interner>;
86 pub type Const = chalk_ir::Const<Interner>;
87 pub type ConstData = chalk_ir::ConstData<Interner>;
88 pub type ConstValue = chalk_ir::ConstValue<Interner>;
89 pub type ConcreteConst = chalk_ir::ConcreteConst<Interner>;
91 pub type ChalkTraitId = chalk_ir::TraitId<Interner>;
93 pub type FnSig = chalk_ir::FnSig<Interner>;
95 pub type InEnvironment<T> = chalk_ir::InEnvironment<T>;
96 pub type DomainGoal = chalk_ir::DomainGoal<Interner>;
97 pub type AliasEq = chalk_ir::AliasEq<Interner>;
98 pub type Solution = chalk_solve::Solution<Interner>;
99 pub type ConstrainedSubst = chalk_ir::ConstrainedSubst<Interner>;
100 pub type Guidance = chalk_solve::Guidance<Interner>;
101 pub type WhereClause = chalk_ir::WhereClause<Interner>;
103 // FIXME: get rid of this
104 pub fn subst_prefix(s: &Substitution, n: usize) -> Substitution {
105 Substitution::intern(s.interned()[..std::cmp::min(s.len(&Interner), n)].into())
108 /// Return an index of a parameter in the generic type parameter list by it's id.
109 pub fn param_idx(db: &dyn HirDatabase, id: TypeParamId) -> Option<usize> {
110 generics(db.upcast(), id.parent).param_idx(id)
113 pub fn wrap_empty_binders<T>(value: T) -> Binders<T>
117 Binders::empty(&Interner, value.shifted_in_from(DebruijnIndex::ONE))
120 pub fn make_only_type_binders<T>(num_vars: usize, value: T) -> Binders<T> {
122 VariableKinds::from_iter(
124 std::iter::repeat(chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General))
131 // FIXME: get rid of this
132 pub fn make_canonical<T>(
134 kinds: impl IntoIterator<Item = TyVariableKind>,
136 let kinds = kinds.into_iter().map(|tk| {
137 chalk_ir::CanonicalVarKind::new(
138 chalk_ir::VariableKind::Ty(tk),
139 chalk_ir::UniverseIndex::ROOT,
142 Canonical { value, binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds) }
145 pub type TraitRef = chalk_ir::TraitRef<Interner>;
147 pub type QuantifiedWhereClause = Binders<WhereClause>;
149 pub type QuantifiedWhereClauses = chalk_ir::QuantifiedWhereClauses<Interner>;
151 pub type Canonical<T> = chalk_ir::Canonical<T>;
153 /// A function signature as seen by type inference: Several parameter types and
155 #[derive(Clone, PartialEq, Eq, Debug)]
156 pub struct CallableSig {
157 params_and_return: Arc<[Ty]>,
161 /// A polymorphic function signature.
162 pub type PolyFnSig = Binders<CallableSig>;
165 pub fn from_params_and_return(mut params: Vec<Ty>, ret: Ty, is_varargs: bool) -> CallableSig {
167 CallableSig { params_and_return: params.into(), is_varargs }
170 pub fn from_fn_ptr(fn_ptr: &FnPointer) -> CallableSig {
172 // FIXME: what to do about lifetime params? -> return PolyFnSig
173 params_and_return: fn_ptr
176 .shifted_out_to(DebruijnIndex::ONE)
177 .expect("unexpected lifetime vars in fn ptr")
181 .map(|arg| arg.assert_ty_ref(&Interner).clone())
183 is_varargs: fn_ptr.sig.variadic,
187 pub fn params(&self) -> &[Ty] {
188 &self.params_and_return[0..self.params_and_return.len() - 1]
191 pub fn ret(&self) -> &Ty {
192 &self.params_and_return[self.params_and_return.len() - 1]
196 #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
197 pub enum ImplTraitId {
198 ReturnTypeImplTrait(hir_def::FunctionId, u16),
199 AsyncBlockTypeImplTrait(hir_def::DefWithBodyId, ExprId),
202 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
203 pub struct ReturnTypeImplTraits {
204 pub(crate) impl_traits: Vec<ReturnTypeImplTrait>,
207 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
208 pub(crate) struct ReturnTypeImplTrait {
209 pub(crate) bounds: Binders<Vec<QuantifiedWhereClause>>,
212 pub fn to_foreign_def_id(id: TypeAliasId) -> ForeignDefId {
213 chalk_ir::ForeignDefId(salsa::InternKey::as_intern_id(&id))
216 pub fn from_foreign_def_id(id: ForeignDefId) -> TypeAliasId {
217 salsa::InternKey::from_intern_id(id.0)
220 pub fn to_assoc_type_id(id: TypeAliasId) -> AssocTypeId {
221 chalk_ir::AssocTypeId(salsa::InternKey::as_intern_id(&id))
224 pub fn from_assoc_type_id(id: AssocTypeId) -> TypeAliasId {
225 salsa::InternKey::from_intern_id(id.0)
228 pub fn from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> TypeParamId {
229 assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT);
230 let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx));
231 db.lookup_intern_type_param_id(interned_id)
234 pub fn to_placeholder_idx(db: &dyn HirDatabase, id: TypeParamId) -> PlaceholderIndex {
235 let interned_id = db.intern_type_param_id(id);
237 ui: chalk_ir::UniverseIndex::ROOT,
238 idx: salsa::InternKey::as_intern_id(&interned_id).as_usize(),
242 pub fn lt_from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> LifetimeParamId {
243 assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT);
244 let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx));
245 db.lookup_intern_lifetime_param_id(interned_id)
248 pub fn const_from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> ConstParamId {
249 assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT);
250 let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx));
251 db.lookup_intern_const_param_id(interned_id)
254 pub fn to_chalk_trait_id(id: TraitId) -> ChalkTraitId {
255 chalk_ir::TraitId(salsa::InternKey::as_intern_id(&id))
258 pub fn from_chalk_trait_id(id: ChalkTraitId) -> TraitId {
259 salsa::InternKey::from_intern_id(id.0)
262 pub fn static_lifetime() -> Lifetime {
263 LifetimeData::Static.intern(&Interner)
266 pub fn dummy_usize_const() -> Const {
267 let usize_ty = chalk_ir::TyKind::Scalar(Scalar::Uint(UintTy::Usize)).intern(&Interner);
268 chalk_ir::ConstData {
270 value: chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst { interned: () }),