2019-02-23 14:24:07 +00:00
|
|
|
//! The type system. We currently use this to infer types for completion, hover
|
|
|
|
//! information and various assists.
|
2018-12-29 19:27:13 +00:00
|
|
|
|
2019-01-06 18:51:42 +00:00
|
|
|
mod autoderef;
|
2019-01-10 15:03:15 +00:00
|
|
|
pub(crate) mod primitive;
|
2018-12-20 20:56:28 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
2019-03-31 18:02:16 +00:00
|
|
|
pub(crate) mod traits;
|
2019-01-07 12:44:54 +00:00
|
|
|
pub(crate) mod method_resolution;
|
2019-02-23 14:24:07 +00:00
|
|
|
mod op;
|
|
|
|
mod lower;
|
|
|
|
mod infer;
|
2019-03-14 21:03:39 +00:00
|
|
|
pub(crate) mod display;
|
2018-12-20 20:56:28 +00:00
|
|
|
|
2019-05-01 14:48:05 +00:00
|
|
|
use std::ops::Deref;
|
2019-07-04 20:05:17 +00:00
|
|
|
use std::sync::Arc;
|
2018-12-26 16:00:42 +00:00
|
|
|
use std::{fmt, mem};
|
2018-12-20 20:56:28 +00:00
|
|
|
|
2019-07-04 20:05:17 +00:00
|
|
|
use crate::{db::HirDatabase, type_ref::Mutability, AdtDef, GenericParams, Name, Trait, TypeAlias};
|
2019-04-11 12:34:13 +00:00
|
|
|
use display::{HirDisplay, HirFormatter};
|
2019-01-26 22:57:03 +00:00
|
|
|
|
2019-05-12 16:33:47 +00:00
|
|
|
pub(crate) use autoderef::autoderef;
|
2019-07-04 20:05:17 +00:00
|
|
|
pub(crate) use infer::{infer_query, InferTy, InferenceResult};
|
|
|
|
pub use lower::CallableDef;
|
|
|
|
pub(crate) use lower::{
|
|
|
|
callable_item_sig, generic_defaults, generic_predicates, type_for_def, type_for_field,
|
|
|
|
TypableDef,
|
|
|
|
};
|
2019-06-16 10:04:08 +00:00
|
|
|
pub(crate) use traits::ProjectionPredicate;
|
2019-01-13 20:00:31 +00:00
|
|
|
|
2019-03-21 21:23:52 +00:00
|
|
|
/// A type constructor or type name: this might be something like the primitive
|
|
|
|
/// type `bool`, a struct like `Vec`, or things like function pointers or
|
|
|
|
/// tuples.
|
2019-03-17 18:37:09 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
|
2019-03-21 21:20:03 +00:00
|
|
|
pub enum TypeCtor {
|
2019-03-17 17:20:51 +00:00
|
|
|
/// The primitive boolean type. Written as `bool`.
|
|
|
|
Bool,
|
|
|
|
|
|
|
|
/// The primitive character type; holds a Unicode scalar value
|
|
|
|
/// (a non-surrogate code point). Written as `char`.
|
|
|
|
Char,
|
|
|
|
|
|
|
|
/// A primitive integer type. For example, `i32`.
|
|
|
|
Int(primitive::UncertainIntTy),
|
|
|
|
|
|
|
|
/// A primitive floating-point type. For example, `f64`.
|
|
|
|
Float(primitive::UncertainFloatTy),
|
|
|
|
|
|
|
|
/// Structures, enumerations and unions.
|
|
|
|
Adt(AdtDef),
|
|
|
|
|
|
|
|
/// The pointee of a string slice. Written as `str`.
|
|
|
|
Str,
|
|
|
|
|
|
|
|
/// The pointee of an array slice. Written as `[T]`.
|
|
|
|
Slice,
|
|
|
|
|
|
|
|
/// An array with the given length. Written as `[T; n]`.
|
|
|
|
Array,
|
|
|
|
|
|
|
|
/// A raw pointer. Written as `*mut T` or `*const T`
|
|
|
|
RawPtr(Mutability),
|
|
|
|
|
|
|
|
/// A reference; a pointer with an associated lifetime. Written as
|
|
|
|
/// `&'a mut T` or `&'a T`.
|
|
|
|
Ref(Mutability),
|
|
|
|
|
|
|
|
/// The anonymous type of a function declaration/definition. Each
|
|
|
|
/// function has a unique type, which is output (for a function
|
|
|
|
/// named `foo` returning an `i32`) as `fn() -> i32 {foo}`.
|
|
|
|
///
|
|
|
|
/// This includes tuple struct / enum variant constructors as well.
|
|
|
|
///
|
|
|
|
/// For example the type of `bar` here:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// fn foo() -> i32 { 1 }
|
|
|
|
/// let bar = foo; // bar: fn() -> i32 {foo}
|
|
|
|
/// ```
|
|
|
|
FnDef(CallableDef),
|
|
|
|
|
|
|
|
/// A pointer to a function. Written as `fn() -> i32`.
|
|
|
|
///
|
|
|
|
/// For example the type of `bar` here:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// fn foo() -> i32 { 1 }
|
|
|
|
/// let bar: fn() -> i32 = foo;
|
|
|
|
/// ```
|
2019-05-04 17:07:25 +00:00
|
|
|
FnPtr { num_args: u16 },
|
2019-03-17 17:20:51 +00:00
|
|
|
|
|
|
|
/// The never type `!`.
|
|
|
|
Never,
|
|
|
|
|
|
|
|
/// A tuple type. For example, `(i32, bool)`.
|
2019-05-04 17:07:25 +00:00
|
|
|
Tuple { cardinality: u16 },
|
2019-03-17 17:20:51 +00:00
|
|
|
}
|
|
|
|
|
2019-03-21 21:23:52 +00:00
|
|
|
/// A nominal type with (maybe 0) type parameters. This might be a primitive
|
|
|
|
/// type like `bool`, a struct, tuple, function pointer, reference or
|
|
|
|
/// several other things.
|
2019-03-24 16:37:27 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
2019-03-17 17:20:51 +00:00
|
|
|
pub struct ApplicationTy {
|
2019-03-21 21:29:12 +00:00
|
|
|
pub ctor: TypeCtor,
|
2019-03-17 17:20:51 +00:00
|
|
|
pub parameters: Substs,
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:53:44 +00:00
|
|
|
/// A "projection" type corresponds to an (unnormalized)
|
|
|
|
/// projection like `<P0 as Trait<P1..Pn>>::Foo`. Note that the
|
|
|
|
/// trait and all its parameters are fully known.
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
|
|
|
pub struct ProjectionTy {
|
|
|
|
pub associated_ty: TypeAlias,
|
|
|
|
pub parameters: Substs,
|
|
|
|
}
|
|
|
|
|
2019-03-21 21:23:52 +00:00
|
|
|
/// A type.
|
|
|
|
///
|
|
|
|
/// See also the `TyKind` enum in rustc (librustc/ty/sty.rs), which represents
|
|
|
|
/// the same thing (but in a different way).
|
2018-12-29 19:27:13 +00:00
|
|
|
///
|
|
|
|
/// This should be cheap to clone.
|
2019-03-24 16:37:27 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
2018-12-20 20:56:28 +00:00
|
|
|
pub enum Ty {
|
2019-03-17 18:37:09 +00:00
|
|
|
/// A nominal type with (maybe 0) type parameters. This might be a primitive
|
|
|
|
/// type like `bool`, a struct, tuple, function pointer, reference or
|
|
|
|
/// several other things.
|
2019-03-17 17:20:51 +00:00
|
|
|
Apply(ApplicationTy),
|
|
|
|
|
2019-01-12 20:27:35 +00:00
|
|
|
/// A type parameter; for example, `T` in `fn f<T>(x: T) {}
|
|
|
|
Param {
|
2019-01-13 10:58:41 +00:00
|
|
|
/// The index of the parameter (starting with parameters from the
|
|
|
|
/// surrounding impl, then the current function).
|
2019-01-12 20:27:35 +00:00
|
|
|
idx: u32,
|
|
|
|
/// The name of the parameter, for displaying.
|
2019-04-20 10:34:36 +00:00
|
|
|
// FIXME get rid of this
|
2019-01-12 20:27:35 +00:00
|
|
|
name: Name,
|
|
|
|
},
|
2018-12-20 20:56:28 +00:00
|
|
|
|
2019-04-27 19:05:59 +00:00
|
|
|
/// A bound type variable. Only used during trait resolution to represent
|
|
|
|
/// Chalk variables.
|
|
|
|
Bound(u32),
|
|
|
|
|
2018-12-26 16:00:42 +00:00
|
|
|
/// A type variable used during type checking. Not to be confused with a
|
|
|
|
/// type parameter.
|
|
|
|
Infer(InferTy),
|
|
|
|
|
|
|
|
/// A placeholder for a type which could not be computed; this is propagated
|
|
|
|
/// to avoid useless error messages. Doubles as a placeholder where type
|
|
|
|
/// variables are inserted before type checking, since we want to try to
|
2018-12-29 19:27:13 +00:00
|
|
|
/// infer a better type here anyway -- for the IDE use case, we want to try
|
|
|
|
/// to infer as much as possible even in the presence of type errors.
|
2018-12-20 20:56:28 +00:00
|
|
|
Unknown,
|
|
|
|
}
|
|
|
|
|
2019-02-23 14:24:07 +00:00
|
|
|
/// A list of substitutions for generic parameters.
|
2019-03-24 16:37:27 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
2019-02-23 14:24:07 +00:00
|
|
|
pub struct Substs(Arc<[Ty]>);
|
|
|
|
|
|
|
|
impl Substs {
|
|
|
|
pub fn empty() -> Substs {
|
|
|
|
Substs(Arc::new([]))
|
|
|
|
}
|
2019-03-16 16:21:32 +00:00
|
|
|
|
2019-03-17 18:37:09 +00:00
|
|
|
pub fn single(ty: Ty) -> Substs {
|
|
|
|
Substs(Arc::new([ty]))
|
|
|
|
}
|
|
|
|
|
2019-03-31 18:02:16 +00:00
|
|
|
pub fn prefix(&self, n: usize) -> Substs {
|
|
|
|
Substs(self.0.iter().cloned().take(n).collect::<Vec<_>>().into())
|
|
|
|
}
|
|
|
|
|
2019-03-16 16:21:32 +00:00
|
|
|
pub fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) {
|
|
|
|
// Without an Arc::make_mut_slice, we can't avoid the clone here:
|
|
|
|
let mut v: Vec<_> = self.0.iter().cloned().collect();
|
|
|
|
for t in &mut v {
|
|
|
|
t.walk_mut(f);
|
|
|
|
}
|
|
|
|
self.0 = v.into();
|
|
|
|
}
|
2019-03-17 17:20:51 +00:00
|
|
|
|
|
|
|
pub fn as_single(&self) -> &Ty {
|
|
|
|
if self.0.len() != 1 {
|
|
|
|
panic!("expected substs of len 1, got {:?}", self);
|
|
|
|
}
|
|
|
|
&self.0[0]
|
|
|
|
}
|
2019-04-20 10:34:36 +00:00
|
|
|
|
|
|
|
/// Return Substs that replace each parameter by itself (i.e. `Ty::Param`).
|
|
|
|
pub fn identity(generic_params: &GenericParams) -> Substs {
|
|
|
|
Substs(
|
|
|
|
generic_params
|
|
|
|
.params_including_parent()
|
|
|
|
.into_iter()
|
|
|
|
.map(|p| Ty::Param { idx: p.idx, name: p.name.clone() })
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.into(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return Substs that replace each parameter by a bound variable.
|
|
|
|
pub fn bound_vars(generic_params: &GenericParams) -> Substs {
|
|
|
|
Substs(
|
|
|
|
generic_params
|
|
|
|
.params_including_parent()
|
|
|
|
.into_iter()
|
|
|
|
.map(|p| Ty::Bound(p.idx))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.into(),
|
|
|
|
)
|
|
|
|
}
|
2019-02-23 14:24:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-31 18:02:16 +00:00
|
|
|
impl From<Vec<Ty>> for Substs {
|
|
|
|
fn from(v: Vec<Ty>) -> Self {
|
|
|
|
Substs(v.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 14:48:05 +00:00
|
|
|
impl Deref for Substs {
|
|
|
|
type Target = [Ty];
|
|
|
|
|
|
|
|
fn deref(&self) -> &[Ty] {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-24 16:37:27 +00:00
|
|
|
/// A trait with type parameters. This includes the `Self`, so this represents a concrete type implementing the trait.
|
|
|
|
/// Name to be bikeshedded: TraitBound? TraitImplements?
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
|
|
|
pub struct TraitRef {
|
|
|
|
/// FIXME name?
|
2019-05-12 16:33:47 +00:00
|
|
|
pub trait_: Trait,
|
|
|
|
pub substs: Substs,
|
2019-03-24 16:37:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TraitRef {
|
|
|
|
pub fn self_ty(&self) -> &Ty {
|
2019-05-01 14:48:05 +00:00
|
|
|
&self.substs[0]
|
2019-03-24 16:37:27 +00:00
|
|
|
}
|
2019-04-20 10:34:36 +00:00
|
|
|
|
|
|
|
pub fn subst(mut self, substs: &Substs) -> TraitRef {
|
|
|
|
self.substs.walk_mut(&mut |ty_mut| {
|
|
|
|
let ty = mem::replace(ty_mut, Ty::Unknown);
|
|
|
|
*ty_mut = ty.subst(substs);
|
|
|
|
});
|
|
|
|
self
|
|
|
|
}
|
2019-03-24 16:37:27 +00:00
|
|
|
}
|
|
|
|
|
2019-05-05 12:21:00 +00:00
|
|
|
/// Like `generics::WherePredicate`, but with resolved types: A condition on the
|
|
|
|
/// parameters of a generic item.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub enum GenericPredicate {
|
|
|
|
/// The given trait needs to be implemented for its type parameters.
|
|
|
|
Implemented(TraitRef),
|
|
|
|
/// We couldn't resolve the trait reference. (If some type parameters can't
|
|
|
|
/// be resolved, they will just be Unknown).
|
|
|
|
Error,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GenericPredicate {
|
|
|
|
pub fn is_error(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
GenericPredicate::Error => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn subst(self, substs: &Substs) -> GenericPredicate {
|
|
|
|
match self {
|
|
|
|
GenericPredicate::Implemented(trait_ref) => {
|
|
|
|
GenericPredicate::Implemented(trait_ref.subst(substs))
|
|
|
|
}
|
|
|
|
GenericPredicate::Error => self,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 15:57:56 +00:00
|
|
|
/// Basically a claim (currently not validated / checked) that the contained
|
|
|
|
/// type / trait ref contains no inference variables; any inference variables it
|
|
|
|
/// contained have been replaced by bound variables, and `num_vars` tells us how
|
|
|
|
/// many there are. This is used to erase irrelevant differences between types
|
|
|
|
/// before using them in queries.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2019-05-05 14:04:31 +00:00
|
|
|
pub struct Canonical<T> {
|
2019-05-01 15:57:56 +00:00
|
|
|
pub value: T,
|
|
|
|
pub num_vars: usize,
|
|
|
|
}
|
|
|
|
|
2019-03-21 21:23:52 +00:00
|
|
|
/// A function signature as seen by type inference: Several parameter types and
|
|
|
|
/// one return type.
|
2019-01-12 20:27:35 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
2018-12-23 16:13:11 +00:00
|
|
|
pub struct FnSig {
|
2019-03-16 16:21:32 +00:00
|
|
|
params_and_return: Arc<[Ty]>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FnSig {
|
|
|
|
pub fn from_params_and_return(mut params: Vec<Ty>, ret: Ty) -> FnSig {
|
|
|
|
params.push(ret);
|
|
|
|
FnSig { params_and_return: params.into() }
|
|
|
|
}
|
2019-03-16 17:14:41 +00:00
|
|
|
|
|
|
|
pub fn from_fn_ptr_substs(substs: &Substs) -> FnSig {
|
|
|
|
FnSig { params_and_return: Arc::clone(&substs.0) }
|
|
|
|
}
|
|
|
|
|
2019-03-16 16:21:32 +00:00
|
|
|
pub fn params(&self) -> &[Ty] {
|
|
|
|
&self.params_and_return[0..self.params_and_return.len() - 1]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ret(&self) -> &Ty {
|
|
|
|
&self.params_and_return[self.params_and_return.len() - 1]
|
|
|
|
}
|
|
|
|
|
2019-04-09 20:16:20 +00:00
|
|
|
/// Applies the given substitutions to all types in this signature and
|
|
|
|
/// returns the result.
|
|
|
|
pub fn subst(&self, substs: &Substs) -> FnSig {
|
|
|
|
let result: Vec<_> =
|
|
|
|
self.params_and_return.iter().map(|ty| ty.clone().subst(substs)).collect();
|
|
|
|
FnSig { params_and_return: result.into() }
|
|
|
|
}
|
|
|
|
|
2019-03-16 16:21:32 +00:00
|
|
|
pub fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) {
|
|
|
|
// Without an Arc::make_mut_slice, we can't avoid the clone here:
|
|
|
|
let mut v: Vec<_> = self.params_and_return.iter().cloned().collect();
|
|
|
|
for t in &mut v {
|
|
|
|
t.walk_mut(f);
|
|
|
|
}
|
|
|
|
self.params_and_return = v.into();
|
|
|
|
}
|
2018-12-23 16:13:11 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 20:56:28 +00:00
|
|
|
impl Ty {
|
2019-03-21 21:29:12 +00:00
|
|
|
pub fn simple(ctor: TypeCtor) -> Ty {
|
|
|
|
Ty::Apply(ApplicationTy { ctor, parameters: Substs::empty() })
|
2019-03-17 18:37:09 +00:00
|
|
|
}
|
2019-03-21 21:29:12 +00:00
|
|
|
pub fn apply_one(ctor: TypeCtor, param: Ty) -> Ty {
|
|
|
|
Ty::Apply(ApplicationTy { ctor, parameters: Substs::single(param) })
|
2019-03-17 18:37:09 +00:00
|
|
|
}
|
2019-03-21 21:29:12 +00:00
|
|
|
pub fn apply(ctor: TypeCtor, parameters: Substs) -> Ty {
|
|
|
|
Ty::Apply(ApplicationTy { ctor, parameters })
|
2019-03-17 17:20:51 +00:00
|
|
|
}
|
2018-12-20 20:56:28 +00:00
|
|
|
pub fn unit() -> Self {
|
2019-05-04 17:07:25 +00:00
|
|
|
Ty::apply(TypeCtor::Tuple { cardinality: 0 }, Substs::empty())
|
2018-12-26 16:00:42 +00:00
|
|
|
}
|
|
|
|
|
2019-02-09 17:27:11 +00:00
|
|
|
pub fn walk(&self, f: &mut impl FnMut(&Ty)) {
|
|
|
|
match self {
|
2019-03-17 17:20:51 +00:00
|
|
|
Ty::Apply(a_ty) => {
|
|
|
|
for t in a_ty.parameters.iter() {
|
|
|
|
t.walk(f);
|
|
|
|
}
|
|
|
|
}
|
2019-04-27 19:05:59 +00:00
|
|
|
Ty::Param { .. } | Ty::Bound(_) | Ty::Infer(_) | Ty::Unknown => {}
|
2019-02-09 17:27:11 +00:00
|
|
|
}
|
2019-02-11 22:01:52 +00:00
|
|
|
f(self);
|
2019-02-09 17:27:11 +00:00
|
|
|
}
|
|
|
|
|
2018-12-26 16:00:42 +00:00
|
|
|
fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) {
|
|
|
|
match self {
|
2019-03-17 17:20:51 +00:00
|
|
|
Ty::Apply(a_ty) => {
|
|
|
|
a_ty.parameters.walk_mut(f);
|
|
|
|
}
|
2019-04-27 19:05:59 +00:00
|
|
|
Ty::Param { .. } | Ty::Bound(_) | Ty::Infer(_) | Ty::Unknown => {}
|
2018-12-26 16:00:42 +00:00
|
|
|
}
|
2019-02-11 22:01:52 +00:00
|
|
|
f(self);
|
2018-12-26 16:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fold(mut self, f: &mut impl FnMut(Ty) -> Ty) -> Ty {
|
|
|
|
self.walk_mut(&mut |ty_mut| {
|
|
|
|
let ty = mem::replace(ty_mut, Ty::Unknown);
|
|
|
|
*ty_mut = f(ty);
|
|
|
|
});
|
|
|
|
self
|
2018-12-20 20:56:28 +00:00
|
|
|
}
|
2019-01-06 18:51:42 +00:00
|
|
|
|
2019-03-17 18:37:09 +00:00
|
|
|
pub fn as_reference(&self) -> Option<(&Ty, Mutability)> {
|
|
|
|
match self {
|
2019-03-21 21:29:12 +00:00
|
|
|
Ty::Apply(ApplicationTy { ctor: TypeCtor::Ref(mutability), parameters }) => {
|
2019-03-17 18:37:09 +00:00
|
|
|
Some((parameters.as_single(), *mutability))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_adt(&self) -> Option<(AdtDef, &Substs)> {
|
|
|
|
match self {
|
2019-03-21 21:29:12 +00:00
|
|
|
Ty::Apply(ApplicationTy { ctor: TypeCtor::Adt(adt_def), parameters }) => {
|
2019-03-17 18:37:09 +00:00
|
|
|
Some((*adt_def, parameters))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_tuple(&self) -> Option<&Substs> {
|
|
|
|
match self {
|
2019-05-04 17:07:25 +00:00
|
|
|
Ty::Apply(ApplicationTy { ctor: TypeCtor::Tuple { .. }, parameters }) => {
|
|
|
|
Some(parameters)
|
|
|
|
}
|
2019-03-17 18:37:09 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 12:34:13 +00:00
|
|
|
pub fn as_callable(&self) -> Option<(CallableDef, &Substs)> {
|
|
|
|
match self {
|
|
|
|
Ty::Apply(ApplicationTy { ctor: TypeCtor::FnDef(callable_def), parameters }) => {
|
|
|
|
Some((*callable_def, parameters))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-06 18:51:42 +00:00
|
|
|
fn builtin_deref(&self) -> Option<Ty> {
|
|
|
|
match self {
|
2019-03-21 21:29:12 +00:00
|
|
|
Ty::Apply(a_ty) => match a_ty.ctor {
|
2019-03-21 21:20:03 +00:00
|
|
|
TypeCtor::Ref(..) => Some(Ty::clone(a_ty.parameters.as_single())),
|
|
|
|
TypeCtor::RawPtr(..) => Some(Ty::clone(a_ty.parameters.as_single())),
|
2019-03-17 17:20:51 +00:00
|
|
|
_ => None,
|
|
|
|
},
|
2019-01-06 18:51:42 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2019-01-12 20:27:35 +00:00
|
|
|
|
2019-04-09 20:16:20 +00:00
|
|
|
fn callable_sig(&self, db: &impl HirDatabase) -> Option<FnSig> {
|
|
|
|
match self {
|
|
|
|
Ty::Apply(a_ty) => match a_ty.ctor {
|
2019-05-04 17:07:25 +00:00
|
|
|
TypeCtor::FnPtr { .. } => Some(FnSig::from_fn_ptr_substs(&a_ty.parameters)),
|
2019-04-09 20:16:20 +00:00
|
|
|
TypeCtor::FnDef(def) => {
|
|
|
|
let sig = db.callable_item_signature(def);
|
|
|
|
Some(sig.subst(&a_ty.parameters))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-13 20:00:31 +00:00
|
|
|
/// If this is a type with type parameters (an ADT or function), replaces
|
|
|
|
/// the `Substs` for these type parameters with the given ones. (So e.g. if
|
|
|
|
/// `self` is `Option<_>` and the substs contain `u32`, we'll have
|
|
|
|
/// `Option<u32>` afterwards.)
|
|
|
|
pub fn apply_substs(self, substs: Substs) -> Ty {
|
|
|
|
match self {
|
2019-03-21 21:39:31 +00:00
|
|
|
Ty::Apply(ApplicationTy { ctor, parameters: previous_substs }) => {
|
|
|
|
assert_eq!(previous_substs.len(), substs.len());
|
2019-03-21 21:29:12 +00:00
|
|
|
Ty::Apply(ApplicationTy { ctor, parameters: substs })
|
2019-03-17 17:20:51 +00:00
|
|
|
}
|
2019-01-13 20:00:31 +00:00
|
|
|
_ => self,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 20:27:35 +00:00
|
|
|
/// Replaces type parameters in this type using the given `Substs`. (So e.g.
|
|
|
|
/// if `self` is `&[T]`, where type parameter T has index 0, and the
|
|
|
|
/// `Substs` contain `u32` at index 0, we'll have `&[u32]` afterwards.)
|
|
|
|
pub fn subst(self, substs: &Substs) -> Ty {
|
|
|
|
self.fold(&mut |ty| match ty {
|
|
|
|
Ty::Param { idx, name } => {
|
2019-05-04 16:41:48 +00:00
|
|
|
substs.get(idx as usize).cloned().unwrap_or(Ty::Param { idx, name })
|
2019-01-12 20:27:35 +00:00
|
|
|
}
|
|
|
|
ty => ty,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-20 10:34:36 +00:00
|
|
|
/// Substitutes `Ty::Bound` vars (as opposed to type parameters).
|
|
|
|
pub fn subst_bound_vars(self, substs: &Substs) -> Ty {
|
|
|
|
self.fold(&mut |ty| match ty {
|
2019-06-03 14:27:51 +00:00
|
|
|
Ty::Bound(idx) => substs.get(idx as usize).cloned().unwrap_or_else(|| Ty::Bound(idx)),
|
2019-04-20 10:34:36 +00:00
|
|
|
ty => ty,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-12 20:27:35 +00:00
|
|
|
/// Returns the type parameters of this type if it has some (i.e. is an ADT
|
|
|
|
/// or function); so if `self` is `Option<u32>`, this returns the `u32`.
|
|
|
|
fn substs(&self) -> Option<Substs> {
|
|
|
|
match self {
|
2019-03-17 17:20:51 +00:00
|
|
|
Ty::Apply(ApplicationTy { parameters, .. }) => Some(parameters.clone()),
|
2019-01-12 20:27:35 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2019-06-15 16:20:59 +00:00
|
|
|
|
|
|
|
/// Shifts up `Ty::Bound` vars by `n`.
|
|
|
|
pub fn shift_bound_vars(self, n: i32) -> Ty {
|
|
|
|
self.fold(&mut |ty| match ty {
|
|
|
|
Ty::Bound(idx) => {
|
|
|
|
assert!(idx as i32 >= -n);
|
|
|
|
Ty::Bound((idx as i32 + n) as u32)
|
|
|
|
}
|
|
|
|
ty => ty,
|
|
|
|
})
|
|
|
|
}
|
2018-12-20 20:56:28 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 21:03:39 +00:00
|
|
|
impl HirDisplay for &Ty {
|
|
|
|
fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
|
|
|
|
HirDisplay::hir_fmt(*self, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-17 17:20:51 +00:00
|
|
|
impl HirDisplay for ApplicationTy {
|
|
|
|
fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
|
2019-03-21 21:29:12 +00:00
|
|
|
match self.ctor {
|
2019-03-21 21:20:03 +00:00
|
|
|
TypeCtor::Bool => write!(f, "bool")?,
|
|
|
|
TypeCtor::Char => write!(f, "char")?,
|
|
|
|
TypeCtor::Int(t) => write!(f, "{}", t)?,
|
|
|
|
TypeCtor::Float(t) => write!(f, "{}", t)?,
|
|
|
|
TypeCtor::Str => write!(f, "str")?,
|
2019-04-03 22:23:58 +00:00
|
|
|
TypeCtor::Slice => {
|
2019-03-17 17:20:51 +00:00
|
|
|
let t = self.parameters.as_single();
|
|
|
|
write!(f, "[{}]", t.display(f.db))?;
|
|
|
|
}
|
2019-04-03 22:23:58 +00:00
|
|
|
TypeCtor::Array => {
|
|
|
|
let t = self.parameters.as_single();
|
2019-04-04 22:29:21 +00:00
|
|
|
write!(f, "[{};_]", t.display(f.db))?;
|
2019-04-03 22:23:58 +00:00
|
|
|
}
|
2019-03-21 21:20:03 +00:00
|
|
|
TypeCtor::RawPtr(m) => {
|
2019-03-17 17:20:51 +00:00
|
|
|
let t = self.parameters.as_single();
|
|
|
|
write!(f, "*{}{}", m.as_keyword_for_ptr(), t.display(f.db))?;
|
|
|
|
}
|
2019-03-21 21:20:03 +00:00
|
|
|
TypeCtor::Ref(m) => {
|
2019-03-17 17:20:51 +00:00
|
|
|
let t = self.parameters.as_single();
|
|
|
|
write!(f, "&{}{}", m.as_keyword_for_ref(), t.display(f.db))?;
|
|
|
|
}
|
2019-03-21 21:20:03 +00:00
|
|
|
TypeCtor::Never => write!(f, "!")?,
|
2019-05-04 17:07:25 +00:00
|
|
|
TypeCtor::Tuple { .. } => {
|
2019-03-17 17:20:51 +00:00
|
|
|
let ts = &self.parameters;
|
2019-05-01 14:48:05 +00:00
|
|
|
if ts.len() == 1 {
|
|
|
|
write!(f, "({},)", ts[0].display(f.db))?;
|
2019-03-17 17:20:51 +00:00
|
|
|
} else {
|
|
|
|
write!(f, "(")?;
|
|
|
|
f.write_joined(&*ts.0, ", ")?;
|
|
|
|
write!(f, ")")?;
|
|
|
|
}
|
|
|
|
}
|
2019-05-04 17:07:25 +00:00
|
|
|
TypeCtor::FnPtr { .. } => {
|
2019-03-17 17:20:51 +00:00
|
|
|
let sig = FnSig::from_fn_ptr_substs(&self.parameters);
|
|
|
|
write!(f, "fn(")?;
|
|
|
|
f.write_joined(sig.params(), ", ")?;
|
|
|
|
write!(f, ") -> {}", sig.ret().display(f.db))?;
|
|
|
|
}
|
2019-03-21 21:20:03 +00:00
|
|
|
TypeCtor::FnDef(def) => {
|
2019-03-17 17:20:51 +00:00
|
|
|
let sig = f.db.callable_item_signature(def);
|
|
|
|
let name = match def {
|
|
|
|
CallableDef::Function(ff) => ff.name(f.db),
|
|
|
|
CallableDef::Struct(s) => s.name(f.db).unwrap_or_else(Name::missing),
|
|
|
|
CallableDef::EnumVariant(e) => e.name(f.db).unwrap_or_else(Name::missing),
|
|
|
|
};
|
|
|
|
match def {
|
|
|
|
CallableDef::Function(_) => write!(f, "fn {}", name)?,
|
|
|
|
CallableDef::Struct(_) | CallableDef::EnumVariant(_) => write!(f, "{}", name)?,
|
|
|
|
}
|
2019-05-01 14:48:05 +00:00
|
|
|
if self.parameters.len() > 0 {
|
2019-03-17 17:20:51 +00:00
|
|
|
write!(f, "<")?;
|
|
|
|
f.write_joined(&*self.parameters.0, ", ")?;
|
|
|
|
write!(f, ">")?;
|
|
|
|
}
|
|
|
|
write!(f, "(")?;
|
|
|
|
f.write_joined(sig.params(), ", ")?;
|
|
|
|
write!(f, ") -> {}", sig.ret().display(f.db))?;
|
|
|
|
}
|
2019-03-21 21:20:03 +00:00
|
|
|
TypeCtor::Adt(def_id) => {
|
2019-03-17 17:20:51 +00:00
|
|
|
let name = match def_id {
|
|
|
|
AdtDef::Struct(s) => s.name(f.db),
|
2019-05-23 17:18:47 +00:00
|
|
|
AdtDef::Union(u) => u.name(f.db),
|
2019-03-17 17:20:51 +00:00
|
|
|
AdtDef::Enum(e) => e.name(f.db),
|
|
|
|
}
|
|
|
|
.unwrap_or_else(Name::missing);
|
|
|
|
write!(f, "{}", name)?;
|
2019-05-01 14:48:05 +00:00
|
|
|
if self.parameters.len() > 0 {
|
2019-03-17 17:20:51 +00:00
|
|
|
write!(f, "<")?;
|
|
|
|
f.write_joined(&*self.parameters.0, ", ")?;
|
|
|
|
write!(f, ">")?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 21:03:39 +00:00
|
|
|
impl HirDisplay for Ty {
|
|
|
|
fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
|
2018-12-23 11:05:54 +00:00
|
|
|
match self {
|
2019-03-17 17:20:51 +00:00
|
|
|
Ty::Apply(a_ty) => a_ty.hir_fmt(f)?,
|
2019-03-14 21:03:39 +00:00
|
|
|
Ty::Param { name, .. } => write!(f, "{}", name)?,
|
2019-04-27 19:05:59 +00:00
|
|
|
Ty::Bound(idx) => write!(f, "?{}", idx)?,
|
2019-03-14 21:03:39 +00:00
|
|
|
Ty::Unknown => write!(f, "{{unknown}}")?,
|
|
|
|
Ty::Infer(..) => write!(f, "_")?,
|
2018-12-23 11:05:54 +00:00
|
|
|
}
|
2019-03-14 21:03:39 +00:00
|
|
|
Ok(())
|
2018-12-23 11:05:54 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-07 10:08:42 +00:00
|
|
|
|
|
|
|
impl HirDisplay for TraitRef {
|
|
|
|
fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"{}: {}",
|
|
|
|
self.substs[0].display(f.db),
|
|
|
|
self.trait_.name(f.db).unwrap_or_else(Name::missing)
|
|
|
|
)?;
|
|
|
|
if self.substs.len() > 1 {
|
|
|
|
write!(f, "<")?;
|
|
|
|
f.write_joined(&self.substs[1..], ", ")?;
|
|
|
|
write!(f, ">")?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|