TyKind in rustc_middle::ty::sty - Rust
Description: API documentation for the Rust `TyKind` type in crate `rustc_middle`.

Skip to main content

## TyKind

# Type Alias TyKind

Source

    pub type TyKind<'tcx> = TyKind<TyCtxt<'tcx>>;

## Aliased Type§

    pub enum TyKind<'tcx> {
    Show 29 variants    Bool,
        Char,
        Int(IntTy),
        Uint(UintTy),
        Float(FloatTy),
        Adt(AdtDef<'tcx>, &'tcx RawList<(), GenericArg<'tcx>>),
        Foreign(DefId),
        Str,
        Array(Ty<'tcx>, Const<'tcx>),
        Pat(Ty<'tcx>, Pattern<'tcx>),
        Slice(Ty<'tcx>),
        RawPtr(Ty<'tcx>, Mutability),
        Ref(Region<'tcx>, Ty<'tcx>, Mutability),
        FnDef(DefId, &'tcx RawList<(), GenericArg<'tcx>>),
        FnPtr(Binder<TyCtxt<'tcx>, FnSigTys<TyCtxt<'tcx>>>, FnHeader<TyCtxt<'tcx>>),
        UnsafeBinder(UnsafeBinderInner<TyCtxt<'tcx>>),
        Dynamic(&'tcx RawList<(), Binder<TyCtxt<'tcx>, ExistentialPredicate<TyCtxt<'tcx>>>>, Region<'tcx>),
        Closure(DefId, &'tcx RawList<(), GenericArg<'tcx>>),
        CoroutineClosure(DefId, &'tcx RawList<(), GenericArg<'tcx>>),
        Coroutine(DefId, &'tcx RawList<(), GenericArg<'tcx>>),
        CoroutineWitness(DefId, &'tcx RawList<(), GenericArg<'tcx>>),
        Never,
        Tuple(&'tcx RawList<(), Ty<'tcx>>),
        Alias(AliasTy<TyCtxt<'tcx>>),
        Param(ParamTy),
        Bound(BoundVarIndexKind, BoundTy<TyCtxt<'tcx>>),
        Placeholder(Placeholder<TyCtxt<'tcx>, BoundTy<TyCtxt<'tcx>>>),
        Infer(InferTy),
        Error(ErrorGuaranteed),
    }

## Variants§

§

### Bool

The primitive boolean type. Written as `bool`.

§

### Char

The primitive character type; holds a Unicode scalar value (a non-surrogate code point). Written as `char`.

§

### Int(IntTy)

A primitive signed integer type. For example, `i32`.

§

### Uint(UintTy)

A primitive unsigned integer type. For example, `u32`.

§

### Float(FloatTy)

A primitive floating-point type. For example, `f64`.

§

### Adt(AdtDef<'tcx>, &'tcx RawList<(), GenericArg<'tcx>>)

Algebraic data types (ADT). For example: structures, enumerations and unions.

For example, the type `List<i32>` would be represented using the `AdtDef` for `struct List<T>` and the args `[i32]`.

Note that generic parameters in fields only get lazily instantiated by using something like `adt_def.all_fields().map(|field| field.ty(interner, args))`.

§

### Foreign(DefId)

An unsized FFI type that is opaque to Rust. Written as `extern type T`.

§

### Str

The pointee of a string slice. Written as `str`.

§

### Array(Ty<'tcx>, Const<'tcx>)

An array with the given length. Written as `[T; N]`.

§

### Pat(Ty<'tcx>, Pattern<'tcx>)

A pattern newtype.

Takes any type and restricts its valid values to its pattern. This will also change the layout to take advantage of this restriction. Only `Copy` and `Clone` will automatically get implemented for pattern types. Auto-traits treat this as if it were an aggregate with a single nested type. Only supports integer range patterns for now.

§

### Slice(Ty<'tcx>)

The pointee of an array slice. Written as `[T]`.

§

### RawPtr(Ty<'tcx>, Mutability)

A raw pointer. Written as `*mut T` or `*const T`

§

### Ref(Region<'tcx>, Ty<'tcx>, Mutability)

A reference; a pointer with an associated lifetime. Written as `&'a mut T` or `&'a T`.

§

### FnDef(DefId, &'tcx RawList<(), GenericArg<'tcx>>)

The anonymous type of a function declaration/definition.

Each function has a unique type.

For the function `fn foo() -> i32 { 3 }` this type would be shown to the user as `fn() -> i32 {foo}`.

For example the type of `bar` here:

    fn foo() -> i32 { 1 }
    let bar = foo; // bar: fn() -> i32 {foo}

§

### FnPtr(Binder<TyCtxt<'tcx>, FnSigTys<TyCtxt<'tcx>>>, FnHeader<TyCtxt<'tcx>>)

A pointer to a function.

Written as `fn() -> i32`.

Note that both functions and closures start out as either [FnDef] or [Closure] which can be then be coerced to this variant.

For example the type of `bar` here:

    fn foo() -> i32 { 1 }
    let bar: fn() -> i32 = foo;

These two fields are equivalent to a `ty::Binder<I, FnSig<I>>`. But by splitting that into two pieces, we get a more compact data layout that reduces the size of `TyKind` by 8 bytes. It is a very hot type, so it’s worth the mild inconvenience.

§

### UnsafeBinder(UnsafeBinderInner<TyCtxt<'tcx>>)

An unsafe binder type.

A higher-ranked type used to represent a type which has had some of its lifetimes erased. This can be used to represent types in positions where a lifetime is literally inexpressible, such as self-referential types.

§

### Dynamic(&'tcx RawList<(), Binder<TyCtxt<'tcx>, ExistentialPredicate<TyCtxt<'tcx>>>>, Region<'tcx>)

A trait object. Written as `dyn for<'b> Trait<'b, Assoc = u32> + Send + 'a`.

§

### Closure(DefId, &'tcx RawList<(), GenericArg<'tcx>>)

The anonymous type of a closure. Used to represent the type of `|a| a`.

Closure args contain both the - potentially instantiated - generic parameters of its parent and some synthetic parameters. See the documentation for `ClosureArgs` for more details.

§

### CoroutineClosure(DefId, &'tcx RawList<(), GenericArg<'tcx>>)

The anonymous type of a closure. Used to represent the type of `async |a| a`.

Coroutine-closure args contain both the - potentially instantiated - generic parameters of its parent and some synthetic parameters. See the documentation for `CoroutineClosureArgs` for more details.

§

### Coroutine(DefId, &'tcx RawList<(), GenericArg<'tcx>>)

The anonymous type of a coroutine. Used to represent the type of `|a| yield a`.

For more info about coroutine args, visit the documentation for `CoroutineArgs`.

§

### CoroutineWitness(DefId, &'tcx RawList<(), GenericArg<'tcx>>)

A type representing the types stored inside a coroutine.

This should only appear as part of the `CoroutineArgs`.

Unlike upvars, the witness can reference lifetimes from inside of the coroutine itself. To deal with them in the type of the coroutine, we convert them to higher ranked lifetimes bound by the witness itself.

This contains the `DefId` and the `GenericArgsRef` of the coroutine. The actual witness types are computed on MIR by the `mir_coroutine_witnesses` query.

Looking at the following example, the witness for this coroutine may end up as something like `for<'a> [Vec<i32>, &'a Vec<i32>]`:

    #![feature(coroutines)]
    #[coroutine] static |a| {
        let x = &vec![3];
        yield a;
        yield x[0];
    }

§

### Never

The never type `!`.

§

### Tuple(&'tcx RawList<(), Ty<'tcx>>)

A tuple type. For example, `(i32, bool)`.

§

### Alias(AliasTy<TyCtxt<'tcx>>)

A projection, opaque type, free type alias, or inherent associated type.

All of these types are represented as pairs of def-id and args, and can be normalized, so they are grouped conceptually.

§

### Param(ParamTy)

A type parameter; for example, `T` in `fn f<T>(x: T) {}`.

§

### Bound(BoundVarIndexKind, BoundTy<TyCtxt<'tcx>>)

Bound type variable, used to represent the `'a` in `for<'a> fn(&'a ())`.

For canonical queries, we replace inference variables with bound variables, so e.g. when checking whether `&'_ (): Trait<_>` holds, we canonicalize that to `for<'a, T> &'a (): Trait<T>` and then convert the introduced bound variables back to inference variables in a new inference context when inside of the query.

It is conventional to render anonymous bound types like `^N` or `^D_N`, where `N` is the bound variable’s anonymous index into the binder, and `D` is the debruijn index, or totally omitted if the debruijn index is zero.

See the `rustc-dev-guide` for more details about higher-ranked trait bounds and canonical queries.

§

### Placeholder(Placeholder<TyCtxt<'tcx>, BoundTy<TyCtxt<'tcx>>>)

A placeholder type, used during higher ranked subtyping to instantiate bound variables.

It is conventional to render anonymous placeholder types like `!N` or `!U_N`, where `N` is the placeholder variable’s anonymous index (which corresponds to the bound variable’s index from the binder from which it was instantiated), and `U` is the universe index in which it is instantiated, or totally omitted if the universe index is zero.

§

### Infer(InferTy)

A type variable used during type checking.

Similar to placeholders, inference variables also live in a universe to correctly deal with higher ranked types. Though unlike placeholders, that universe is stored in the `InferCtxt` instead of directly inside of the type.

§

### Error(ErrorGuaranteed)

A placeholder for a type which could not be computed.

This is propagated to avoid useless error messages.

## Trait Implementations§

Source§

### impl<'tcx> Article for TyKind<'tcx>

Source§

#### fn article(&self) -> &'static str

Get the article (“a” or “an”) to use with this type.

## Layout§

Note: Unable to compute type layout, possibly due to this type having generic parameters. Layout can only be computed for concrete, fully-instantiated types.