Expand description
Code for type-checking cast expressions.
A cast e as U is valid if one of the following holds:
ehas typeTandTcoerces toU; coercion-castehas type*T,Uis*U_0, and eitherU_0: Sizedor pointer_kind(T) = pointer_kind(U_0); ptr-ptr-castehas type*TandUis a numeric type, whileT: Sized; ptr-addr-casteis an integer andUis*U_0, whileU_0: Sized; addr-ptr-castehas typeTandTandUare any numeric types; numeric-casteis a C-like enum andUis an integer type; enum-castehas typeboolorcharandUis an integer; prim-int-castehas typeu8andUischar; u8-char-castehas type&[T; n]andUis*const T; array-ptr-casteis a function pointer type andUhas type*T, whileT: Sized; fptr-ptr-casteis a function pointer type andUis 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).
Re-exportsยง
use rustc_data_structures::fx::FxHashSet;๐use rustc_errors::Applicability;๐use rustc_errors::Diag;๐use rustc_errors::ErrorGuaranteed;๐use rustc_hir::def_id::DefId;๐use rustc_hir::def_id::LocalDefId;๐use rustc_hir as hir;๐use rustc_hir::ExprKind;๐use rustc_infer::infer::DefineOpaqueTypes;๐use rustc_infer::traits::ObligationCauseCode;๐use rustc_lint_defs::builtin::TRIVIAL_CASTS;๐use rustc_lint_defs::builtin::TRIVIAL_NUMERIC_CASTS;๐use rustc_macros::TypeFoldable;๐use rustc_macros::TypeVisitable;๐use rustc_middle::mir::Mutability;๐use rustc_middle::ty::adjustment::AllowTwoPhase;๐use rustc_middle::ty::cast::CastTy;๐use rustc_middle::ty::error::TypeError;๐use rustc_middle::ty;๐use rustc_middle::ty::Ty;๐use rustc_middle::ty::TyCtxt;๐use rustc_middle::ty::TypeAndMut;๐use rustc_middle::ty::TypeVisitableExt;๐use rustc_middle::ty::Unnormalized;๐use rustc_middle::ty::VariantDef;๐use rustc_middle::ty::elaborate;๐use rustc_span::DUMMY_SP;๐use rustc_span::Span;๐use rustc_span::bug;๐use rustc_span::span_bug;๐use rustc_span::sym;๐use rustc_trait_selection::infer::InferCtxtExt;๐use rustc_trait_selection::traits;๐use rustc_trait_selection::traits::ObligationCtxt;๐use rustc_trait_selection::traits::TraitEngine;๐use tracing::debug;๐use tracing::instrument;๐use tracing::instrument;๐use super::FnCtxt;๐use crate::diagnostics;๐use crate::type_error_struct;๐use rustc_errors::codes::*;๐
Structsยง
- Cast
Check ๐ - Reifies a cast check to be checked once we have full type information for a function context.
Enumsยง
- Cast
Error ๐ - Cast
Kind - Cast Kind. See RFC 401 (or rustc_hir_typeck/src/cast.rs).
- Pointer
Kind ๐ - The kind of pointer and associated metadata (thin, length or vtable) - we only allow casts between wide pointers if their metadata have the same kind.
Functionsยง
- check_
cast - If a cast from
from_tytoto_tyis valid, returns aSomecontaining the kind of the cast. - index_
operand_ ๐ambiguity_ span - make_
invalid_ ๐casting_ error