Skip to main content

Module cast

Module 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 type T and T coerces to U; coercion-cast
  • e has type *T, U is *U_0, and either U_0: Sized or pointer_kind(T) = pointer_kind(U_0); ptr-ptr-cast
  • e has type *T and U is a numeric type, while T: Sized; ptr-addr-cast
  • e is an integer and U is *U_0, while U_0: Sized; addr-ptr-cast
  • e has type T and T and U are any numeric types; numeric-cast
  • e is a C-like enum and U is an integer type; enum-cast
  • e has type bool or char and U is an integer; prim-int-cast
  • e has type u8 and U is char; u8-char-cast
  • e has type &[T; n] and U is *const T; array-ptr-cast
  • e is a function pointer type and U has type *T, while T: Sized; fptr-ptr-cast
  • e is a function pointer type and U 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).

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ยง

CastCheck ๐Ÿ”’
Reifies a cast check to be checked once we have full type information for a function context.

Enumsยง

CastError ๐Ÿ”’
CastKind
Cast Kind. See RFC 401 (or rustc_hir_typeck/src/cast.rs).
PointerKind ๐Ÿ”’
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_ty to to_ty is valid, returns a Some containing the kind of the cast.
index_operand_ambiguity_span ๐Ÿ”’
make_invalid_casting_error ๐Ÿ”’