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;
44 pub use chalk_ext::{ProjectionTyExt, TyExt};
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))
111 pub fn hir_trait_id(&self) -> TraitId {
112 from_chalk_trait_id(self.trait_id)
116 impl<T> Canonical<T> {
117 pub fn new(value: T, kinds: impl IntoIterator<Item = TyVariableKind>) -> Self {
118 let kinds = kinds.into_iter().map(|tk| {
119 chalk_ir::CanonicalVarKind::new(
120 chalk_ir::VariableKind::Ty(tk),
121 chalk_ir::UniverseIndex::ROOT,
124 Self { value, binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds) }
128 /// A function signature as seen by type inference: Several parameter types and
130 #[derive(Clone, PartialEq, Eq, Debug)]
131 pub struct CallableSig {
132 params_and_return: Arc<[Ty]>,
136 /// A polymorphic function signature.
137 pub type PolyFnSig = Binders<CallableSig>;
140 pub fn from_params_and_return(mut params: Vec<Ty>, ret: Ty, is_varargs: bool) -> CallableSig {
142 CallableSig { params_and_return: params.into(), is_varargs }
145 pub fn from_fn_ptr(fn_ptr: &FnPointer) -> CallableSig {
147 // FIXME: what to do about lifetime params? -> return PolyFnSig
148 params_and_return: fn_ptr
151 .shifted_out_to(DebruijnIndex::ONE)
152 .expect("unexpected lifetime vars in fn ptr")
156 .map(|arg| arg.assert_ty_ref(&Interner).clone())
158 is_varargs: fn_ptr.sig.variadic,
162 pub fn params(&self) -> &[Ty] {
163 &self.params_and_return[0..self.params_and_return.len() - 1]
166 pub fn ret(&self) -> &Ty {
167 &self.params_and_return[self.params_and_return.len() - 1]
173 #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
174 pub enum ImplTraitId {
175 ReturnTypeImplTrait(hir_def::FunctionId, u16),
176 AsyncBlockTypeImplTrait(hir_def::DefWithBodyId, ExprId),
179 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
180 pub struct ReturnTypeImplTraits {
181 pub(crate) impl_traits: Vec<ReturnTypeImplTrait>,
184 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
185 pub(crate) struct ReturnTypeImplTrait {
186 pub(crate) bounds: Binders<Vec<QuantifiedWhereClause>>,
189 pub fn to_foreign_def_id(id: TypeAliasId) -> ForeignDefId {
190 chalk_ir::ForeignDefId(salsa::InternKey::as_intern_id(&id))
193 pub fn from_foreign_def_id(id: ForeignDefId) -> TypeAliasId {
194 salsa::InternKey::from_intern_id(id.0)
197 pub fn to_assoc_type_id(id: TypeAliasId) -> AssocTypeId {
198 chalk_ir::AssocTypeId(salsa::InternKey::as_intern_id(&id))
201 pub fn from_assoc_type_id(id: AssocTypeId) -> TypeAliasId {
202 salsa::InternKey::from_intern_id(id.0)
205 pub fn from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> TypeParamId {
206 assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT);
207 let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx));
208 db.lookup_intern_type_param_id(interned_id)
211 pub fn to_placeholder_idx(db: &dyn HirDatabase, id: TypeParamId) -> PlaceholderIndex {
212 let interned_id = db.intern_type_param_id(id);
214 ui: chalk_ir::UniverseIndex::ROOT,
215 idx: salsa::InternKey::as_intern_id(&interned_id).as_usize(),
219 pub fn lt_from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> LifetimeParamId {
220 assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT);
221 let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx));
222 db.lookup_intern_lifetime_param_id(interned_id)
225 pub fn const_from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> ConstParamId {
226 assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT);
227 let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx));
228 db.lookup_intern_const_param_id(interned_id)
231 pub fn to_chalk_trait_id(id: TraitId) -> ChalkTraitId {
232 chalk_ir::TraitId(salsa::InternKey::as_intern_id(&id))
235 pub fn from_chalk_trait_id(id: ChalkTraitId) -> TraitId {
236 salsa::InternKey::from_intern_id(id.0)
239 pub fn static_lifetime() -> Lifetime {
240 LifetimeData::Static.intern(&Interner)
243 pub fn dummy_usize_const() -> Const {
244 let usize_ty = chalk_ir::TyKind::Scalar(Scalar::Uint(UintTy::Usize)).intern(&Interner);
245 chalk_ir::ConstData {
247 value: chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst { interned: () }),