Module rustc_hir_typeck::cast
source · Expand description
Code for type-checking cast expressions.
A cast e as U
is valid if one of the following holds:
e
has typeT
andT
coerces toU
; coercion-caste
has type*T
,U
is*U_0
, and eitherU_0: Sized
or pointer_kind(T
) = pointer_kind(U_0
); ptr-ptr-caste
has type*T
andU
is a numeric type, whileT: Sized
; ptr-addr-caste
is an integer andU
is*U_0
, whileU_0: Sized
; addr-ptr-caste
has typeT
andT
andU
are any numeric types; numeric-caste
is a C-like enum andU
is an integer type; enum-caste
has typebool
orchar
andU
is an integer; prim-int-caste
has typeu8
andU
ischar
; u8-char-caste
has type&[T; n]
andU
is*const T
; array-ptr-caste
is a function pointer type andU
has type*T
, whileT: Sized
; fptr-ptr-caste
is a function pointer type andU
is an integer; fptr-addr-cast
where &.T
and *T
are references of either mutability,
and where pointer_kind(T
) is the kind of the unsize info
in T
- the vtable for a trait definition (e.g., fmt::Display
or
Iterator
, not Iterator<Item=u8>
) or a length (or ()
if T: Sized
).
Note that lengths are not adjusted when casting raw slices -
T: *const [u16] as *const [u8]
creates a slice that only includes
half of the original memory.
Casting is not transitive, that is, even if e as U1 as U2
is a valid
expression, e as U2
is not necessarily so (in fact it will only be valid if
U1
coerces to U2
).
Structs
Reifies a cast check to be checked once we have full type information for
a function context.
Enums
The kind of pointer and associated metadata (thin, length or vtable) - we
only allow casts between fat pointers if their metadata have the same
kind.