Skip to main content

rustc_hir_typeck/
cast.rs

1//! Code for type-checking cast expressions.
2//!
3//! A cast `e as U` is valid if one of the following holds:
4//! * `e` has type `T` and `T` coerces to `U`; *coercion-cast*
5//! * `e` has type `*T`, `U` is `*U_0`, and either `U_0: Sized` or
6//!    pointer_kind(`T`) = pointer_kind(`U_0`); *ptr-ptr-cast*
7//! * `e` has type `*T` and `U` is a numeric type, while `T: Sized`; *ptr-addr-cast*
8//! * `e` is an integer and `U` is `*U_0`, while `U_0: Sized`; *addr-ptr-cast*
9//! * `e` has type `T` and `T` and `U` are any numeric types; *numeric-cast*
10//! * `e` is a C-like enum and `U` is an integer type; *enum-cast*
11//! * `e` has type `bool` or `char` and `U` is an integer; *prim-int-cast*
12//! * `e` has type `u8` and `U` is `char`; *u8-char-cast*
13//! * `e` has type `&[T; n]` and `U` is `*const T`; *array-ptr-cast*
14//! * `e` is a function pointer type and `U` has type `*T`,
15//!   while `T: Sized`; *fptr-ptr-cast*
16//! * `e` is a function pointer type and `U` is an integer; *fptr-addr-cast*
17//!
18//! where `&.T` and `*T` are references of either mutability,
19//! and where pointer_kind(`T`) is the kind of the unsize info
20//! in `T` - the vtable for a trait definition (e.g., `fmt::Display` or
21//! `Iterator`, not `Iterator<Item=u8>`) or a length (or `()` if `T: Sized`).
22//!
23//! Note that lengths are not adjusted when casting raw slices -
24//! `T: *const [u16] as *const [u8]` creates a slice that only includes
25//! half of the original memory.
26//!
27//! Casting is not transitive, that is, even if `e as U1 as U2` is a valid
28//! expression, `e as U2` is not necessarily so (in fact it will only be valid if
29//! `U1` coerces to `U2`).
30
31use rustc_data_structures::fx::FxHashSet;
32use rustc_errors::codes::*;
33use rustc_errors::{Applicability, Diag, ErrorGuaranteed};
34use rustc_hir::def_id::{DefId, LocalDefId};
35use rustc_hir::{self as hir, ExprKind};
36use rustc_infer::infer::DefineOpaqueTypes;
37use rustc_macros::{TypeFoldable, TypeVisitable};
38use rustc_middle::mir::Mutability;
39use rustc_middle::ty::adjustment::AllowTwoPhase;
40use rustc_middle::ty::cast::{CastKind, CastTy};
41use rustc_middle::ty::error::TypeError;
42use rustc_middle::ty::{
43    self, Ty, TyCtxt, TypeAndMut, TypeVisitableExt, Unnormalized, VariantDef, elaborate,
44};
45use rustc_middle::{bug, span_bug};
46use rustc_session::lint;
47use rustc_span::{DUMMY_SP, Span, sym};
48use rustc_trait_selection::infer::InferCtxtExt;
49use tracing::{debug, instrument};
50
51use super::FnCtxt;
52use crate::{errors, type_error_struct};
53
54/// Reifies a cast check to be checked once we have full type information for
55/// a function context.
56#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for CastCheck<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        let names: &'static _ =
            &["expr", "expr_ty", "expr_span", "cast_ty", "cast_span", "span",
                        "body_id"];
        let values: &[&dyn ::core::fmt::Debug] =
            &[&self.expr, &self.expr_ty, &self.expr_span, &self.cast_ty,
                        &self.cast_span, &self.span, &&self.body_id];
        ::core::fmt::Formatter::debug_struct_fields_finish(f, "CastCheck",
            names, values)
    }
}Debug)]
57pub(crate) struct CastCheck<'tcx> {
58    /// The expression whose value is being casted
59    expr: &'tcx hir::Expr<'tcx>,
60    /// The source type for the cast expression
61    expr_ty: Ty<'tcx>,
62    expr_span: Span,
63    /// The target type. That is, the type we are casting to.
64    cast_ty: Ty<'tcx>,
65    cast_span: Span,
66    span: Span,
67    pub body_id: LocalDefId,
68}
69
70/// The kind of pointer and associated metadata (thin, length or vtable) - we
71/// only allow casts between wide pointers if their metadata have the same
72/// kind.
73#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for PointerKind<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            PointerKind::Thin => ::core::fmt::Formatter::write_str(f, "Thin"),
            PointerKind::VTable(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "VTable",
                    &__self_0),
            PointerKind::Length =>
                ::core::fmt::Formatter::write_str(f, "Length"),
            PointerKind::OfAlias(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "OfAlias", &__self_0),
            PointerKind::OfParam(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "OfParam", &__self_0),
        }
    }
}Debug, #[automatically_derived]
impl<'tcx> ::core::marker::Copy for PointerKind<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for PointerKind<'tcx> {
    #[inline]
    fn clone(&self) -> PointerKind<'tcx> {
        let _:
                ::core::clone::AssertParamIsClone<&'tcx ty::List<ty::Binder<'tcx,
                ty::ExistentialPredicate<'tcx>>>>;
        let _: ::core::clone::AssertParamIsClone<ty::AliasTy<'tcx>>;
        let _: ::core::clone::AssertParamIsClone<ty::ParamTy>;
        *self
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::cmp::PartialEq for PointerKind<'tcx> {
    #[inline]
    fn eq(&self, other: &PointerKind<'tcx>) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (PointerKind::VTable(__self_0), PointerKind::VTable(__arg1_0))
                    => __self_0 == __arg1_0,
                (PointerKind::OfAlias(__self_0),
                    PointerKind::OfAlias(__arg1_0)) => __self_0 == __arg1_0,
                (PointerKind::OfParam(__self_0),
                    PointerKind::OfParam(__arg1_0)) => __self_0 == __arg1_0,
                _ => true,
            }
    }
}PartialEq, #[automatically_derived]
impl<'tcx> ::core::cmp::Eq for PointerKind<'tcx> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _:
                ::core::cmp::AssertParamIsEq<&'tcx ty::List<ty::Binder<'tcx,
                ty::ExistentialPredicate<'tcx>>>>;
        let _: ::core::cmp::AssertParamIsEq<ty::AliasTy<'tcx>>;
        let _: ::core::cmp::AssertParamIsEq<ty::ParamTy>;
    }
}Eq, const _: () =
    {
        impl<'tcx>
            ::rustc_middle::ty::TypeVisitable<::rustc_middle::ty::TyCtxt<'tcx>>
            for PointerKind<'tcx> {
            fn visit_with<__V: ::rustc_middle::ty::TypeVisitor<::rustc_middle::ty::TyCtxt<'tcx>>>(&self,
                __visitor: &mut __V) -> __V::Result {
                match *self {
                    PointerKind::Thin => {}
                    PointerKind::VTable(ref __binding_0) => {
                        {
                            match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_0,
                                        __visitor)) {
                                ::core::ops::ControlFlow::Continue(()) => {}
                                ::core::ops::ControlFlow::Break(r) => {
                                    return ::rustc_middle::ty::VisitorResult::from_residual(r);
                                }
                            }
                        }
                    }
                    PointerKind::Length => {}
                    PointerKind::OfAlias(ref __binding_0) => {
                        {
                            match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_0,
                                        __visitor)) {
                                ::core::ops::ControlFlow::Continue(()) => {}
                                ::core::ops::ControlFlow::Break(r) => {
                                    return ::rustc_middle::ty::VisitorResult::from_residual(r);
                                }
                            }
                        }
                    }
                    PointerKind::OfParam(ref __binding_0) => {
                        {
                            match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_0,
                                        __visitor)) {
                                ::core::ops::ControlFlow::Continue(()) => {}
                                ::core::ops::ControlFlow::Break(r) => {
                                    return ::rustc_middle::ty::VisitorResult::from_residual(r);
                                }
                            }
                        }
                    }
                }
                <__V::Result as ::rustc_middle::ty::VisitorResult>::output()
            }
        }
    };TypeVisitable, const _: () =
    {
        impl<'tcx>
            ::rustc_middle::ty::TypeFoldable<::rustc_middle::ty::TyCtxt<'tcx>>
            for PointerKind<'tcx> {
            fn try_fold_with<__F: ::rustc_middle::ty::FallibleTypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(self,
                __folder: &mut __F) -> Result<Self, __F::Error> {
                Ok(match self {
                        PointerKind::Thin => { PointerKind::Thin }
                        PointerKind::VTable(__binding_0) => {
                            PointerKind::VTable(::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_0,
                                        __folder)?)
                        }
                        PointerKind::Length => { PointerKind::Length }
                        PointerKind::OfAlias(__binding_0) => {
                            PointerKind::OfAlias(::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_0,
                                        __folder)?)
                        }
                        PointerKind::OfParam(__binding_0) => {
                            PointerKind::OfParam(::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_0,
                                        __folder)?)
                        }
                    })
            }
            fn fold_with<__F: ::rustc_middle::ty::TypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(self,
                __folder: &mut __F) -> Self {
                match self {
                    PointerKind::Thin => { PointerKind::Thin }
                    PointerKind::VTable(__binding_0) => {
                        PointerKind::VTable(::rustc_middle::ty::TypeFoldable::fold_with(__binding_0,
                                __folder))
                    }
                    PointerKind::Length => { PointerKind::Length }
                    PointerKind::OfAlias(__binding_0) => {
                        PointerKind::OfAlias(::rustc_middle::ty::TypeFoldable::fold_with(__binding_0,
                                __folder))
                    }
                    PointerKind::OfParam(__binding_0) => {
                        PointerKind::OfParam(::rustc_middle::ty::TypeFoldable::fold_with(__binding_0,
                                __folder))
                    }
                }
            }
        }
    };TypeFoldable)]
74enum PointerKind<'tcx> {
75    /// No metadata attached, ie pointer to sized type or foreign type
76    Thin,
77    /// A trait object
78    VTable(&'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>),
79    /// Slice
80    Length,
81    /// The unsize info of this projection or opaque type
82    OfAlias(ty::AliasTy<'tcx>),
83    /// The unsize info of this parameter
84    OfParam(ty::ParamTy),
85}
86
87impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
88    /// Returns the kind of unsize information of t, or None
89    /// if t is unknown.
90    fn pointer_kind(
91        &self,
92        t: Ty<'tcx>,
93        span: Span,
94    ) -> Result<Option<PointerKind<'tcx>>, ErrorGuaranteed> {
95        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/cast.rs:95",
                        "rustc_hir_typeck::cast", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/cast.rs"),
                        ::tracing_core::__macro_support::Option::Some(95u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::cast"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("pointer_kind({0:?}, {1:?})",
                                                    t, span) as &dyn Value))])
            });
    } else { ; }
};debug!("pointer_kind({:?}, {:?})", t, span);
96
97        let t = self.resolve_vars_if_possible(t);
98        t.error_reported()?;
99
100        if self.type_is_sized_modulo_regions(self.param_env, t) {
101            return Ok(Some(PointerKind::Thin));
102        }
103
104        let t = self.resolve_vars_with_obligations(t);
105
106        Ok(match *t.kind() {
107            ty::Slice(_) | ty::Str => Some(PointerKind::Length),
108            ty::Dynamic(tty, _) => Some(PointerKind::VTable(tty)),
109            ty::Adt(def, args) if def.is_struct() => match def.non_enum_variant().tail_opt() {
110                None => Some(PointerKind::Thin),
111                Some(f) => {
112                    let field_ty = self.field_ty(span, f, args);
113                    self.pointer_kind(field_ty, span)?
114                }
115            },
116            ty::Tuple(fields) => match fields.last() {
117                None => Some(PointerKind::Thin),
118                Some(&f) => self.pointer_kind(f, span)?,
119            },
120
121            ty::UnsafeBinder(_) => {
    ::core::panicking::panic_fmt(format_args!("not yet implemented: {0}",
            format_args!("FIXME(unsafe_binder)")));
}todo!("FIXME(unsafe_binder)"),
122
123            // Pointers to foreign types are thin, despite being unsized
124            ty::Foreign(..) => Some(PointerKind::Thin),
125            // We should really try to normalize here.
126            ty::Alias(pi) => Some(PointerKind::OfAlias(pi)),
127            ty::Param(p) => Some(PointerKind::OfParam(p)),
128            // Insufficient type information.
129            ty::Placeholder(..) | ty::Bound(..) | ty::Infer(_) => None,
130
131            ty::Bool
132            | ty::Char
133            | ty::Int(..)
134            | ty::Uint(..)
135            | ty::Float(_)
136            | ty::Array(..)
137            | ty::CoroutineWitness(..)
138            | ty::RawPtr(_, _)
139            | ty::Ref(..)
140            | ty::Pat(..)
141            | ty::FnDef(..)
142            | ty::FnPtr(..)
143            | ty::Closure(..)
144            | ty::CoroutineClosure(..)
145            | ty::Coroutine(..)
146            | ty::Adt(..)
147            | ty::Never
148            | ty::Error(_) => {
149                let guar = self
150                    .dcx()
151                    .span_delayed_bug(span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0:?}` should be sized but is not?",
                t))
    })format!("`{t:?}` should be sized but is not?"));
152                return Err(guar);
153            }
154        })
155    }
156}
157
158#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for CastError<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            CastError::ErrorGuaranteed(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "ErrorGuaranteed", &__self_0),
            CastError::CastToBool =>
                ::core::fmt::Formatter::write_str(f, "CastToBool"),
            CastError::CastToChar =>
                ::core::fmt::Formatter::write_str(f, "CastToChar"),
            CastError::DifferingKinds { src_kind: __self_0, dst_kind: __self_1
                } =>
                ::core::fmt::Formatter::debug_struct_field2_finish(f,
                    "DifferingKinds", "src_kind", __self_0, "dst_kind",
                    &__self_1),
            CastError::SizedUnsizedCast =>
                ::core::fmt::Formatter::write_str(f, "SizedUnsizedCast"),
            CastError::IllegalCast =>
                ::core::fmt::Formatter::write_str(f, "IllegalCast"),
            CastError::NeedDeref =>
                ::core::fmt::Formatter::write_str(f, "NeedDeref"),
            CastError::NeedViaPtr =>
                ::core::fmt::Formatter::write_str(f, "NeedViaPtr"),
            CastError::NeedViaThinPtr =>
                ::core::fmt::Formatter::write_str(f, "NeedViaThinPtr"),
            CastError::NeedViaInt =>
                ::core::fmt::Formatter::write_str(f, "NeedViaInt"),
            CastError::NonScalar =>
                ::core::fmt::Formatter::write_str(f, "NonScalar"),
            CastError::UnknownExprPtrKind =>
                ::core::fmt::Formatter::write_str(f, "UnknownExprPtrKind"),
            CastError::UnknownCastPtrKind =>
                ::core::fmt::Formatter::write_str(f, "UnknownCastPtrKind"),
            CastError::CastEnumDrop =>
                ::core::fmt::Formatter::write_str(f, "CastEnumDrop"),
            CastError::IntToWideCast(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "IntToWideCast", &__self_0),
            CastError::ForeignNonExhaustiveAdt =>
                ::core::fmt::Formatter::write_str(f,
                    "ForeignNonExhaustiveAdt"),
            CastError::PtrPtrAddingAutoTrait(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "PtrPtrAddingAutoTrait", &__self_0),
        }
    }
}Debug)]
159enum CastError<'tcx> {
160    ErrorGuaranteed(ErrorGuaranteed),
161
162    CastToBool,
163    CastToChar,
164    DifferingKinds {
165        src_kind: PointerKind<'tcx>,
166        dst_kind: PointerKind<'tcx>,
167    },
168    /// Cast of thin to wide raw ptr (e.g., `*const () as *const [u8]`).
169    SizedUnsizedCast,
170    IllegalCast,
171    NeedDeref,
172    NeedViaPtr,
173    NeedViaThinPtr,
174    NeedViaInt,
175    NonScalar,
176    UnknownExprPtrKind,
177    UnknownCastPtrKind,
178    CastEnumDrop,
179    /// Cast of int to (possibly) wide raw pointer.
180    ///
181    /// Argument is the specific name of the metadata in plain words, such as "a vtable"
182    /// or "a length". If this argument is None, then the metadata is unknown, for example,
183    /// when we're typechecking a type parameter with a ?Sized bound.
184    IntToWideCast(Option<&'static str>),
185    ForeignNonExhaustiveAdt,
186    PtrPtrAddingAutoTrait(Vec<DefId>),
187}
188
189impl From<ErrorGuaranteed> for CastError<'_> {
190    fn from(err: ErrorGuaranteed) -> Self {
191        CastError::ErrorGuaranteed(err)
192    }
193}
194
195fn make_invalid_casting_error<'a, 'tcx>(
196    span: Span,
197    expr_ty: Ty<'tcx>,
198    cast_ty: Ty<'tcx>,
199    fcx: &FnCtxt<'a, 'tcx>,
200) -> Diag<'a> {
201    {
    let mut err =
        {
            fcx.dcx().struct_span_err(span,
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("casting `{0}` as `{1}` is invalid",
                                    fcx.ty_to_string(expr_ty), fcx.ty_to_string(cast_ty)))
                        })).with_code(E0606)
        };
    if expr_ty.references_error() { err.downgrade_to_delayed_bug(); }
    err
}type_error_struct!(
202        fcx.dcx(),
203        span,
204        expr_ty,
205        E0606,
206        "casting `{}` as `{}` is invalid",
207        fcx.ty_to_string(expr_ty),
208        fcx.ty_to_string(cast_ty)
209    )
210}
211
212/// If a cast from `from_ty` to `to_ty` is valid, returns a `Some` containing the kind
213/// of the cast.
214///
215/// This is a helper used from clippy.
216pub fn check_cast<'tcx>(
217    tcx: TyCtxt<'tcx>,
218    param_env: ty::ParamEnv<'tcx>,
219    e: &'tcx hir::Expr<'tcx>,
220    from_ty: Ty<'tcx>,
221    to_ty: Ty<'tcx>,
222) -> Option<CastKind> {
223    let hir_id = e.hir_id;
224    let local_def_id = hir_id.owner.def_id;
225
226    let root_ctxt = crate::TypeckRootCtxt::new(tcx, local_def_id);
227    let fn_ctxt = FnCtxt::new(&root_ctxt, param_env, local_def_id);
228
229    if let Ok(check) = CastCheck::new(
230        &fn_ctxt, e, from_ty, to_ty,
231        // We won't show any errors to the user, so the span is irrelevant here.
232        DUMMY_SP, DUMMY_SP,
233    ) {
234        check.do_check(&fn_ctxt).ok()
235    } else {
236        None
237    }
238}
239
240impl<'a, 'tcx> CastCheck<'tcx> {
241    pub(crate) fn new(
242        fcx: &FnCtxt<'a, 'tcx>,
243        expr: &'tcx hir::Expr<'tcx>,
244        expr_ty: Ty<'tcx>,
245        cast_ty: Ty<'tcx>,
246        cast_span: Span,
247        span: Span,
248    ) -> Result<CastCheck<'tcx>, ErrorGuaranteed> {
249        let expr_span = expr.span.find_ancestor_inside(span).unwrap_or(expr.span);
250        let check =
251            CastCheck { expr, expr_ty, expr_span, cast_ty, cast_span, span, body_id: fcx.body_id };
252
253        // For better error messages, check for some obviously unsized
254        // cases now. We do a more thorough check at the end, once
255        // inference is more completely known.
256        match cast_ty.kind() {
257            ty::Dynamic(_, _) | ty::Slice(..) => Err(check.report_cast_to_unsized_type(fcx)),
258            _ => Ok(check),
259        }
260    }
261
262    fn report_cast_error(&self, fcx: &FnCtxt<'a, 'tcx>, e: CastError<'tcx>) {
263        match e {
264            CastError::ErrorGuaranteed(_) => {
265                // an error has already been reported
266            }
267            CastError::NeedDeref => {
268                let mut err =
269                    make_invalid_casting_error(self.span, self.expr_ty, self.cast_ty, fcx);
270
271                if #[allow(non_exhaustive_omitted_patterns)] match self.expr.kind {
    ExprKind::AddrOf(..) => true,
    _ => false,
}matches!(self.expr.kind, ExprKind::AddrOf(..)) {
272                    // get just the borrow part of the expression
273                    let span = self.expr_span.with_hi(self.expr.peel_borrows().span.lo());
274                    err.span_suggestion_verbose(
275                        span,
276                        "remove the unneeded borrow",
277                        "",
278                        Applicability::MachineApplicable,
279                    );
280                } else {
281                    err.span_suggestion_verbose(
282                        self.expr_span.shrink_to_lo(),
283                        "dereference the expression",
284                        "*",
285                        Applicability::MachineApplicable,
286                    );
287                }
288
289                err.emit();
290            }
291            CastError::NeedViaThinPtr | CastError::NeedViaPtr => {
292                let mut err =
293                    make_invalid_casting_error(self.span, self.expr_ty, self.cast_ty, fcx);
294
295                if self.cast_ty.is_integral() {
296                    if !#[allow(non_exhaustive_omitted_patterns)] match self.expr.kind {
    ExprKind::AddrOf(..) => true,
    _ => false,
}matches!(self.expr.kind, ExprKind::AddrOf(..))
297                        && let ty::Ref(_, inner_ty, _) = *self.expr_ty.kind()
298                        && let ty::Adt(adt_def, _) = *inner_ty.kind()
299                        && adt_def.is_enum()
300                        && adt_def.is_payloadfree()
301                    {
302                        err.span_suggestion_verbose(
303                            self.expr_span.shrink_to_lo(),
304                            "try dereferencing before the cast",
305                            "*",
306                            Applicability::MaybeIncorrect,
307                        );
308                        if !fcx.type_is_copy_modulo_regions(fcx.param_env, inner_ty) {
309                            err.span_suggestion_verbose(
310                                fcx.tcx.def_span(adt_def.did()).shrink_to_lo(),
311                                "add `#[derive(Copy, Clone)]` to the enum definition",
312                                "#[derive(Copy, Clone)]\n",
313                                Applicability::MaybeIncorrect,
314                            );
315                        }
316                    } else {
317                        err.help(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("cast through {0} first",
                match e {
                    CastError::NeedViaPtr => "a raw pointer",
                    CastError::NeedViaThinPtr => "a thin pointer",
                    e => {
                        ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
                                format_args!("control flow means we should never encounter a {0:?}",
                                    e)));
                    }
                }))
    })format!(
318                            "cast through {} first",
319                            match e {
320                                CastError::NeedViaPtr => "a raw pointer",
321                                CastError::NeedViaThinPtr => "a thin pointer",
322                                e => unreachable!(
323                                    "control flow means we should never encounter a {e:?}"
324                                ),
325                            }
326                        ));
327                    }
328                }
329
330                self.try_suggest_collection_to_bool(fcx, &mut err);
331
332                err.emit();
333            }
334            CastError::NeedViaInt => {
335                make_invalid_casting_error(self.span, self.expr_ty, self.cast_ty, fcx)
336                    .with_help("cast through an integer first")
337                    .emit();
338            }
339            CastError::IllegalCast => {
340                make_invalid_casting_error(self.span, self.expr_ty, self.cast_ty, fcx).emit();
341            }
342            CastError::DifferingKinds { src_kind, dst_kind } => {
343                let mut err =
344                    make_invalid_casting_error(self.span, self.expr_ty, self.cast_ty, fcx);
345
346                match (src_kind, dst_kind) {
347                    (PointerKind::VTable(_), PointerKind::VTable(_)) => {
348                        err.note("the trait objects may have different vtables");
349                    }
350                    (
351                        PointerKind::OfParam(_) | PointerKind::OfAlias(_),
352                        PointerKind::OfParam(_)
353                        | PointerKind::OfAlias(_)
354                        | PointerKind::VTable(_)
355                        | PointerKind::Length,
356                    )
357                    | (
358                        PointerKind::VTable(_) | PointerKind::Length,
359                        PointerKind::OfParam(_) | PointerKind::OfAlias(_),
360                    ) => {
361                        err.note("the pointers may have different metadata");
362                    }
363                    (PointerKind::VTable(_), PointerKind::Length)
364                    | (PointerKind::Length, PointerKind::VTable(_)) => {
365                        err.note("the pointers have different metadata");
366                    }
367                    (
368                        PointerKind::Thin,
369                        PointerKind::Thin
370                        | PointerKind::VTable(_)
371                        | PointerKind::Length
372                        | PointerKind::OfParam(_)
373                        | PointerKind::OfAlias(_),
374                    )
375                    | (
376                        PointerKind::VTable(_)
377                        | PointerKind::Length
378                        | PointerKind::OfParam(_)
379                        | PointerKind::OfAlias(_),
380                        PointerKind::Thin,
381                    )
382                    | (PointerKind::Length, PointerKind::Length) => {
383                        ::rustc_middle::util::bug::span_bug_fmt(self.span,
    format_args!("unexpected cast error: {0:?}", e))span_bug!(self.span, "unexpected cast error: {e:?}")
384                    }
385                }
386
387                err.emit();
388            }
389            CastError::CastToBool => {
390                let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
391                let help = if self.expr_ty.is_numeric() {
392                    errors::CannotCastToBoolHelp::Numeric(
393                        self.expr_span.shrink_to_hi().with_hi(self.span.hi()),
394                    )
395                } else {
396                    errors::CannotCastToBoolHelp::Unsupported(self.span)
397                };
398                fcx.dcx().emit_err(errors::CannotCastToBool { span: self.span, expr_ty, help });
399            }
400            CastError::CastToChar => {
401                let mut err = {
    let mut err =
        {
            fcx.dcx().struct_span_err(self.span,
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("only `u8` can be cast as `char`, not `{0}`",
                                    self.expr_ty))
                        })).with_code(E0604)
        };
    if self.expr_ty.references_error() { err.downgrade_to_delayed_bug(); }
    err
}type_error_struct!(
402                    fcx.dcx(),
403                    self.span,
404                    self.expr_ty,
405                    E0604,
406                    "only `u8` can be cast as `char`, not `{}`",
407                    self.expr_ty
408                );
409                err.span_label(self.span, "invalid cast");
410                if self.expr_ty.is_numeric() {
411                    if self.expr_ty == fcx.tcx.types.u32 {
412                        err.multipart_suggestion(
413                            "consider using `char::from_u32` instead",
414                            ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(self.expr_span.shrink_to_lo(), "char::from_u32(".to_string()),
                (self.expr_span.shrink_to_hi().to(self.cast_span),
                    ")".to_string())]))vec![
415                                (self.expr_span.shrink_to_lo(), "char::from_u32(".to_string()),
416                                (self.expr_span.shrink_to_hi().to(self.cast_span), ")".to_string()),
417                            ],
418                            Applicability::MachineApplicable,
419                        );
420                    } else if self.expr_ty == fcx.tcx.types.i8 {
421                        err.span_help(self.span, "consider casting from `u8` instead");
422                    } else {
423                        err.span_help(
424                            self.span,
425                            "consider using `char::from_u32` instead (via a `u32`)",
426                        );
427                    };
428                }
429                err.emit();
430            }
431            CastError::NonScalar => {
432                let mut err = {
    let mut err =
        {
            fcx.dcx().struct_span_err(self.span,
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("non-primitive cast: `{0}` as `{1}`",
                                    self.expr_ty, fcx.ty_to_string(self.cast_ty)))
                        })).with_code(E0605)
        };
    if self.expr_ty.references_error() { err.downgrade_to_delayed_bug(); }
    err
}type_error_struct!(
433                    fcx.dcx(),
434                    self.span,
435                    self.expr_ty,
436                    E0605,
437                    "non-primitive cast: `{}` as `{}`",
438                    self.expr_ty,
439                    fcx.ty_to_string(self.cast_ty)
440                );
441
442                if let Ok(snippet) = fcx.tcx.sess.source_map().span_to_snippet(self.expr_span)
443                    && #[allow(non_exhaustive_omitted_patterns)] match self.expr.kind {
    ExprKind::AddrOf(..) => true,
    _ => false,
}matches!(self.expr.kind, ExprKind::AddrOf(..))
444                {
445                    err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("casting reference expression `{0}` because `&` binds tighter than `as`",
                snippet))
    })format!(
446                        "casting reference expression `{}` because `&` binds tighter than `as`",
447                        snippet
448                    ));
449                }
450
451                let mut sugg = None;
452                let mut sugg_mutref = false;
453                if let ty::Ref(reg, cast_ty, mutbl) = *self.cast_ty.kind() {
454                    if let ty::RawPtr(expr_ty, _) = *self.expr_ty.kind()
455                        && fcx.may_coerce(
456                            Ty::new_ref(fcx.tcx, fcx.tcx.lifetimes.re_erased, expr_ty, mutbl),
457                            self.cast_ty,
458                        )
459                    {
460                        sugg = Some((::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("&{0}*", mutbl.prefix_str()))
    })format!("&{}*", mutbl.prefix_str()), cast_ty == expr_ty));
461                    } else if let ty::Ref(expr_reg, expr_ty, expr_mutbl) = *self.expr_ty.kind()
462                        && expr_mutbl == Mutability::Not
463                        && mutbl == Mutability::Mut
464                        && fcx.may_coerce(Ty::new_mut_ref(fcx.tcx, expr_reg, expr_ty), self.cast_ty)
465                    {
466                        sugg_mutref = true;
467                    }
468
469                    if !sugg_mutref
470                        && sugg == None
471                        && fcx.may_coerce(
472                            Ty::new_ref(fcx.tcx, reg, self.expr_ty, mutbl),
473                            self.cast_ty,
474                        )
475                    {
476                        sugg = Some((::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("&{0}", mutbl.prefix_str()))
    })format!("&{}", mutbl.prefix_str()), false));
477                    }
478                } else if let ty::RawPtr(_, mutbl) = *self.cast_ty.kind()
479                    && fcx.may_coerce(
480                        Ty::new_ref(fcx.tcx, fcx.tcx.lifetimes.re_erased, self.expr_ty, mutbl),
481                        self.cast_ty,
482                    )
483                {
484                    sugg = Some((::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("&{0}", mutbl.prefix_str()))
    })format!("&{}", mutbl.prefix_str()), false));
485                }
486                if sugg_mutref {
487                    err.span_label(self.span, "invalid cast");
488                    err.span_note(self.expr_span, "this reference is immutable");
489                    err.span_note(self.cast_span, "trying to cast to a mutable reference type");
490                } else if let Some((sugg, remove_cast)) = sugg {
491                    err.span_label(self.span, "invalid cast");
492
493                    let has_parens = fcx
494                        .tcx
495                        .sess
496                        .source_map()
497                        .span_to_snippet(self.expr_span)
498                        .is_ok_and(|snip| snip.starts_with('('));
499
500                    // Very crude check to see whether the expression must be wrapped
501                    // in parentheses for the suggestion to work (issue #89497).
502                    // Can/should be extended in the future.
503                    let needs_parens =
504                        !has_parens && #[allow(non_exhaustive_omitted_patterns)] match self.expr.kind {
    hir::ExprKind::Cast(..) => true,
    _ => false,
}matches!(self.expr.kind, hir::ExprKind::Cast(..));
505
506                    let mut suggestion = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(self.expr_span.shrink_to_lo(), sugg)]))vec![(self.expr_span.shrink_to_lo(), sugg)];
507                    if needs_parens {
508                        suggestion[0].1 += "(";
509                        suggestion.push((self.expr_span.shrink_to_hi(), ")".to_string()));
510                    }
511                    if remove_cast {
512                        suggestion.push((
513                            self.expr_span.shrink_to_hi().to(self.cast_span),
514                            String::new(),
515                        ));
516                    }
517
518                    err.multipart_suggestion(
519                        "consider borrowing the value",
520                        suggestion,
521                        Applicability::MachineApplicable,
522                    );
523                } else if !#[allow(non_exhaustive_omitted_patterns)] match self.cast_ty.kind() {
    ty::FnDef(..) | ty::FnPtr(..) | ty::Closure(..) => true,
    _ => false,
}matches!(
524                    self.cast_ty.kind(),
525                    ty::FnDef(..) | ty::FnPtr(..) | ty::Closure(..)
526                ) {
527                    // Check `impl From<self.expr_ty> for self.cast_ty {}` for accurate suggestion:
528                    if let Some(from_trait) = fcx.tcx.get_diagnostic_item(sym::From) {
529                        let ty = fcx.resolve_vars_if_possible(self.cast_ty);
530                        let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
531                        if fcx
532                            .infcx
533                            .type_implements_trait(from_trait, [ty, expr_ty], fcx.param_env)
534                            .must_apply_modulo_regions()
535                        {
536                            let to_ty = if let ty::Adt(def, args) = self.cast_ty.kind() {
537                                fcx.tcx.value_path_str_with_args(def.did(), args)
538                            } else {
539                                self.cast_ty.to_string()
540                            };
541                            err.multipart_suggestion(
542                                "consider using the `From` trait instead",
543                                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(self.expr_span.shrink_to_lo(),
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("{0}::from(", to_ty))
                        })),
                (self.expr_span.shrink_to_hi().to(self.cast_span),
                    ")".to_string())]))vec![
544                                    (self.expr_span.shrink_to_lo(), format!("{to_ty}::from(")),
545                                    (
546                                        self.expr_span.shrink_to_hi().to(self.cast_span),
547                                        ")".to_string(),
548                                    ),
549                                ],
550                                Applicability::MaybeIncorrect,
551                            );
552                        }
553                    }
554
555                    let (msg, note) = if let ty::Adt(adt, _) = self.expr_ty.kind()
556                        && adt.is_enum()
557                        && self.cast_ty.is_numeric()
558                    {
559                        (
560                            "an `as` expression can be used to convert enum types to numeric \
561                             types only if the enum type is unit-only or field-less",
562                            Some(
563                                "see https://doc.rust-lang.org/reference/items/enumerations.html#casting for more information",
564                            ),
565                        )
566                    } else {
567                        (
568                            "an `as` expression can only be used to convert between primitive \
569                             types or to coerce to a specific trait object",
570                            None,
571                        )
572                    };
573
574                    err.span_label(self.span, msg);
575
576                    if let Some(note) = note {
577                        err.note(note);
578                    }
579                } else {
580                    err.span_label(self.span, "invalid cast");
581                }
582
583                fcx.suggest_no_capture_closure(&mut err, self.cast_ty, self.expr_ty);
584                self.try_suggest_collection_to_bool(fcx, &mut err);
585
586                err.emit();
587            }
588            CastError::SizedUnsizedCast => {
589                let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
590                let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
591                fcx.dcx().emit_err(errors::CastThinPointerToWidePointer {
592                    span: self.span,
593                    expr_ty,
594                    cast_ty,
595                    teach: fcx.tcx.sess.teach(E0607),
596                });
597            }
598            CastError::IntToWideCast(known_metadata) => {
599                let expr_if_nightly = fcx.tcx.sess.is_nightly_build().then_some(self.expr_span);
600                let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
601                let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
602                let metadata = known_metadata.unwrap_or("type-specific metadata");
603                let known_wide = known_metadata.is_some();
604                let span = self.cast_span;
605                let param_note = (!known_wide)
606                    .then(|| match cast_ty.kind() {
607                        ty::RawPtr(pointee, _) => match pointee.kind() {
608                            ty::Param(param) => {
609                                Some(errors::IntToWideParamNote { param: param.name })
610                            }
611                            _ => None,
612                        },
613                        _ => None,
614                    })
615                    .flatten();
616                fcx.dcx().emit_err(errors::IntToWide {
617                    span,
618                    metadata,
619                    expr_ty,
620                    cast_ty,
621                    expr_if_nightly,
622                    known_wide,
623                    param_note,
624                });
625            }
626            CastError::UnknownCastPtrKind | CastError::UnknownExprPtrKind => {
627                let unknown_cast_to = match e {
628                    CastError::UnknownCastPtrKind => true,
629                    CastError::UnknownExprPtrKind => false,
630                    e => {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("control flow means we should never encounter a {0:?}",
                e)));
}unreachable!("control flow means we should never encounter a {e:?}"),
631                };
632                let (span, sub) = if unknown_cast_to {
633                    (self.cast_span, errors::CastUnknownPointerSub::To(self.cast_span))
634                } else {
635                    (self.cast_span, errors::CastUnknownPointerSub::From(self.span))
636                };
637                fcx.dcx().emit_err(errors::CastUnknownPointer { span, to: unknown_cast_to, sub });
638            }
639            CastError::CastEnumDrop => {
640                let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
641                let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
642
643                fcx.dcx().emit_err(errors::CastEnumDrop { span: self.span, expr_ty, cast_ty });
644            }
645            CastError::ForeignNonExhaustiveAdt => {
646                make_invalid_casting_error(
647                    self.span,
648                    self.expr_ty,
649                    self.cast_ty,
650                    fcx,
651                )
652                .with_note("cannot cast an enum with a non-exhaustive variant when it's defined in another crate")
653                .emit();
654            }
655            CastError::PtrPtrAddingAutoTrait(added) => {
656                fcx.dcx().emit_err(errors::PtrCastAddAutoToObject {
657                    span: self.span,
658                    traits_len: added.len(),
659                    traits: {
660                        let mut traits: Vec<_> = added
661                            .into_iter()
662                            .map(|trait_did| fcx.tcx.def_path_str(trait_did))
663                            .collect();
664
665                        traits.sort();
666                        traits.into()
667                    },
668                });
669            }
670        }
671    }
672
673    fn report_cast_to_unsized_type(&self, fcx: &FnCtxt<'a, 'tcx>) -> ErrorGuaranteed {
674        if let Err(err) = self.cast_ty.error_reported() {
675            return err;
676        }
677        if let Err(err) = self.expr_ty.error_reported() {
678            return err;
679        }
680
681        let tstr = fcx.ty_to_string(self.cast_ty);
682        let mut err = {
    let mut err =
        {
            fcx.dcx().struct_span_err(self.span,
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("cast to unsized type: `{0}` as `{1}`",
                                    fcx.resolve_vars_if_possible(self.expr_ty), tstr))
                        })).with_code(E0620)
        };
    if self.expr_ty.references_error() { err.downgrade_to_delayed_bug(); }
    err
}type_error_struct!(
683            fcx.dcx(),
684            self.span,
685            self.expr_ty,
686            E0620,
687            "cast to unsized type: `{}` as `{}`",
688            fcx.resolve_vars_if_possible(self.expr_ty),
689            tstr
690        );
691        match self.expr_ty.kind() {
692            ty::Ref(_, _, mt) => {
693                let mtstr = mt.prefix_str();
694                err.span_suggestion_verbose(
695                    self.cast_span.shrink_to_lo(),
696                    "consider casting to a reference instead",
697                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("&{0}", mtstr))
    })format!("&{mtstr}"),
698                    Applicability::MachineApplicable,
699                );
700            }
701            ty::Adt(def, ..) if def.is_box() => {
702                err.multipart_suggestion(
703                    "you can cast to a `Box` instead",
704                    ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(self.cast_span.shrink_to_lo(), "Box<".to_string()),
                (self.cast_span.shrink_to_hi(), ">".to_string())]))vec![
705                        (self.cast_span.shrink_to_lo(), "Box<".to_string()),
706                        (self.cast_span.shrink_to_hi(), ">".to_string()),
707                    ],
708                    Applicability::MachineApplicable,
709                );
710            }
711            _ => {
712                err.span_help(self.expr_span, "consider using a box or reference as appropriate");
713            }
714        }
715        err.emit()
716    }
717
718    fn trivial_cast_lint(&self, fcx: &FnCtxt<'a, 'tcx>) {
719        let (numeric, lint) = if self.cast_ty.is_numeric() && self.expr_ty.is_numeric() {
720            (true, lint::builtin::TRIVIAL_NUMERIC_CASTS)
721        } else {
722            (false, lint::builtin::TRIVIAL_CASTS)
723        };
724        let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
725        let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
726        fcx.tcx.emit_node_span_lint(
727            lint,
728            self.expr.hir_id,
729            self.span,
730            errors::TrivialCast { numeric, expr_ty, cast_ty },
731        );
732    }
733
734    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("check",
                                    "rustc_hir_typeck::cast", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/cast.rs"),
                                    ::tracing_core::__macro_support::Option::Some(734u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::cast"),
                                    ::tracing_core::field::FieldSet::new(&["self"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&self)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: () = loop {};
            return __tracing_attr_fake_return;
        }
        {
            self.expr_ty =
                fcx.structurally_resolve_type(self.expr_span, self.expr_ty);
            self.cast_ty =
                fcx.structurally_resolve_type(self.cast_span, self.cast_ty);
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/cast.rs:739",
                                    "rustc_hir_typeck::cast", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/cast.rs"),
                                    ::tracing_core::__macro_support::Option::Some(739u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::cast"),
                                    ::tracing_core::field::FieldSet::new(&["message"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&format_args!("check_cast({0}, {1:?} as {2:?})",
                                                                self.expr.hir_id, self.expr_ty, self.cast_ty) as
                                                        &dyn Value))])
                        });
                } else { ; }
            };
            if !fcx.type_is_sized_modulo_regions(fcx.param_env, self.cast_ty)
                    && !self.cast_ty.has_infer_types() {
                self.report_cast_to_unsized_type(fcx);
            } else if self.expr_ty.references_error() ||
                    self.cast_ty.references_error()
                {} else {
                match self.try_coercion_cast(fcx) {
                    Ok(()) => {
                        if self.expr_ty.is_raw_ptr() && self.cast_ty.is_raw_ptr() {
                            {
                                use ::tracing::__macro_support::Callsite as _;
                                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                                    {
                                        static META: ::tracing::Metadata<'static> =
                                            {
                                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/cast.rs:757",
                                                    "rustc_hir_typeck::cast", ::tracing::Level::DEBUG,
                                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/cast.rs"),
                                                    ::tracing_core::__macro_support::Option::Some(757u32),
                                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::cast"),
                                                    ::tracing_core::field::FieldSet::new(&["message"],
                                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                                    ::tracing::metadata::Kind::EVENT)
                                            };
                                        ::tracing::callsite::DefaultCallsite::new(&META)
                                    };
                                let enabled =
                                    ::tracing::Level::DEBUG <=
                                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                            ::tracing::Level::DEBUG <=
                                                ::tracing::level_filters::LevelFilter::current() &&
                                        {
                                            let interest = __CALLSITE.interest();
                                            !interest.is_never() &&
                                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                                    interest)
                                        };
                                if enabled {
                                    (|value_set: ::tracing::field::ValueSet|
                                                {
                                                    let meta = __CALLSITE.metadata();
                                                    ::tracing::Event::dispatch(meta, &value_set);
                                                    ;
                                                })({
                                            #[allow(unused_imports)]
                                            use ::tracing::field::{debug, display, Value};
                                            let mut iter = __CALLSITE.metadata().fields().iter();
                                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                                ::tracing::__macro_support::Option::Some(&format_args!(" -> PointerCast")
                                                                        as &dyn Value))])
                                        });
                                } else { ; }
                            };
                        } else {
                            self.trivial_cast_lint(fcx);
                            {
                                use ::tracing::__macro_support::Callsite as _;
                                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                                    {
                                        static META: ::tracing::Metadata<'static> =
                                            {
                                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/cast.rs:760",
                                                    "rustc_hir_typeck::cast", ::tracing::Level::DEBUG,
                                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/cast.rs"),
                                                    ::tracing_core::__macro_support::Option::Some(760u32),
                                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::cast"),
                                                    ::tracing_core::field::FieldSet::new(&["message"],
                                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                                    ::tracing::metadata::Kind::EVENT)
                                            };
                                        ::tracing::callsite::DefaultCallsite::new(&META)
                                    };
                                let enabled =
                                    ::tracing::Level::DEBUG <=
                                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                            ::tracing::Level::DEBUG <=
                                                ::tracing::level_filters::LevelFilter::current() &&
                                        {
                                            let interest = __CALLSITE.interest();
                                            !interest.is_never() &&
                                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                                    interest)
                                        };
                                if enabled {
                                    (|value_set: ::tracing::field::ValueSet|
                                                {
                                                    let meta = __CALLSITE.metadata();
                                                    ::tracing::Event::dispatch(meta, &value_set);
                                                    ;
                                                })({
                                            #[allow(unused_imports)]
                                            use ::tracing::field::{debug, display, Value};
                                            let mut iter = __CALLSITE.metadata().fields().iter();
                                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                                ::tracing::__macro_support::Option::Some(&format_args!(" -> CoercionCast")
                                                                        as &dyn Value))])
                                        });
                                } else { ; }
                            };
                            fcx.typeck_results.borrow_mut().set_coercion_cast(self.expr.hir_id.local_id);
                        }
                    }
                    Err(_) => {
                        match self.do_check(fcx) {
                            Ok(k) => {
                                {
                                    use ::tracing::__macro_support::Callsite as _;
                                    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                                        {
                                            static META: ::tracing::Metadata<'static> =
                                                {
                                                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/cast.rs:769",
                                                        "rustc_hir_typeck::cast", ::tracing::Level::DEBUG,
                                                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/cast.rs"),
                                                        ::tracing_core::__macro_support::Option::Some(769u32),
                                                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::cast"),
                                                        ::tracing_core::field::FieldSet::new(&["message"],
                                                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                                        ::tracing::metadata::Kind::EVENT)
                                                };
                                            ::tracing::callsite::DefaultCallsite::new(&META)
                                        };
                                    let enabled =
                                        ::tracing::Level::DEBUG <=
                                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                                ::tracing::Level::DEBUG <=
                                                    ::tracing::level_filters::LevelFilter::current() &&
                                            {
                                                let interest = __CALLSITE.interest();
                                                !interest.is_never() &&
                                                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                                        interest)
                                            };
                                    if enabled {
                                        (|value_set: ::tracing::field::ValueSet|
                                                    {
                                                        let meta = __CALLSITE.metadata();
                                                        ::tracing::Event::dispatch(meta, &value_set);
                                                        ;
                                                    })({
                                                #[allow(unused_imports)]
                                                use ::tracing::field::{debug, display, Value};
                                                let mut iter = __CALLSITE.metadata().fields().iter();
                                                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                                    ::tracing::__macro_support::Option::Some(&format_args!(" -> {0:?}",
                                                                                    k) as &dyn Value))])
                                            });
                                    } else { ; }
                                };
                            }
                            Err(e) => self.report_cast_error(fcx, e),
                        };
                    }
                };
            }
        }
    }
}#[instrument(skip(fcx), level = "debug")]
735    pub(crate) fn check(mut self, fcx: &FnCtxt<'a, 'tcx>) {
736        self.expr_ty = fcx.structurally_resolve_type(self.expr_span, self.expr_ty);
737        self.cast_ty = fcx.structurally_resolve_type(self.cast_span, self.cast_ty);
738
739        debug!("check_cast({}, {:?} as {:?})", self.expr.hir_id, self.expr_ty, self.cast_ty);
740
741        if !fcx.type_is_sized_modulo_regions(fcx.param_env, self.cast_ty)
742            && !self.cast_ty.has_infer_types()
743        {
744            self.report_cast_to_unsized_type(fcx);
745        } else if self.expr_ty.references_error() || self.cast_ty.references_error() {
746            // No sense in giving duplicate error messages
747        } else {
748            match self.try_coercion_cast(fcx) {
749                Ok(()) => {
750                    if self.expr_ty.is_raw_ptr() && self.cast_ty.is_raw_ptr() {
751                        // When casting a raw pointer to another raw pointer, we cannot convert the cast into
752                        // a coercion because the pointee types might only differ in regions, which HIR typeck
753                        // cannot distinguish. This would cause us to erroneously discard a cast which will
754                        // lead to a borrowck error like #113257.
755                        // We still did a coercion above to unify inference variables for `ptr as _` casts.
756                        // This does cause us to miss some trivial casts in the trivial cast lint.
757                        debug!(" -> PointerCast");
758                    } else {
759                        self.trivial_cast_lint(fcx);
760                        debug!(" -> CoercionCast");
761                        fcx.typeck_results
762                            .borrow_mut()
763                            .set_coercion_cast(self.expr.hir_id.local_id);
764                    }
765                }
766                Err(_) => {
767                    match self.do_check(fcx) {
768                        Ok(k) => {
769                            debug!(" -> {:?}", k);
770                        }
771                        Err(e) => self.report_cast_error(fcx, e),
772                    };
773                }
774            };
775        }
776    }
777    /// Checks a cast, and report an error if one exists. In some cases, this
778    /// can return Ok and create type errors in the fcx rather than returning
779    /// directly. coercion-cast is handled in check instead of here.
780    fn do_check(&self, fcx: &FnCtxt<'a, 'tcx>) -> Result<CastKind, CastError<'tcx>> {
781        use rustc_middle::ty::cast::CastTy::*;
782        use rustc_middle::ty::cast::IntTy::*;
783
784        let (t_from, t_cast) = match (CastTy::from_ty(self.expr_ty), CastTy::from_ty(self.cast_ty))
785        {
786            (Some(t_from), Some(t_cast)) => (t_from, t_cast),
787            // Function item types may need to be reified before casts.
788            (None, Some(t_cast)) => {
789                match *self.expr_ty.kind() {
790                    ty::FnDef(..) => {
791                        // Attempt a coercion to a fn pointer type.
792                        let f = fcx.normalize(
793                            self.expr_span,
794                            Unnormalized::new_wip(self.expr_ty.fn_sig(fcx.tcx)),
795                        );
796                        let res = fcx.coerce(
797                            self.expr,
798                            self.expr_ty,
799                            Ty::new_fn_ptr(fcx.tcx, f),
800                            AllowTwoPhase::No,
801                            None,
802                        );
803                        if let Err(TypeError::IntrinsicCast) = res {
804                            return Err(CastError::IllegalCast);
805                        }
806                        if res.is_err() {
807                            return Err(CastError::NonScalar);
808                        }
809                        (FnPtr, t_cast)
810                    }
811                    // Special case some errors for references, and check for
812                    // array-ptr-casts. `Ref` is not a CastTy because the cast
813                    // is split into a coercion to a pointer type, followed by
814                    // a cast.
815                    ty::Ref(_, inner_ty, mutbl) => {
816                        return match t_cast {
817                            Int(_) | Float => match *inner_ty.kind() {
818                                ty::Int(_)
819                                | ty::Uint(_)
820                                | ty::Float(_)
821                                | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(_)) => {
822                                    Err(CastError::NeedDeref)
823                                }
824                                _ => Err(CastError::NeedViaPtr),
825                            },
826                            // array-ptr-cast
827                            Ptr(mt) => {
828                                if !fcx.type_is_sized_modulo_regions(fcx.param_env, mt.ty) {
829                                    return Err(CastError::IllegalCast);
830                                }
831                                self.check_ref_cast(fcx, TypeAndMut { mutbl, ty: inner_ty }, mt)
832                            }
833                            _ => Err(CastError::NonScalar),
834                        };
835                    }
836                    _ => return Err(CastError::NonScalar),
837                }
838            }
839            _ => return Err(CastError::NonScalar),
840        };
841        if let ty::Adt(adt_def, _) = *self.expr_ty.kind()
842            && !adt_def.did().is_local()
843            && adt_def.variants().iter().any(VariantDef::is_field_list_non_exhaustive)
844        {
845            return Err(CastError::ForeignNonExhaustiveAdt);
846        }
847        match (t_from, t_cast) {
848            // These types have invariants! can't cast into them.
849            (_, Int(CEnum) | FnPtr) => Err(CastError::NonScalar),
850
851            // * -> Bool
852            (_, Int(Bool)) => Err(CastError::CastToBool),
853
854            // * -> Char
855            (Int(U(ty::UintTy::U8)), Int(Char)) => Ok(CastKind::U8CharCast), // u8-char-cast
856            (_, Int(Char)) => Err(CastError::CastToChar),
857
858            // prim -> float,ptr
859            (Int(Bool) | Int(CEnum) | Int(Char), Float) => Err(CastError::NeedViaInt),
860
861            (Int(Bool) | Int(CEnum) | Int(Char) | Float, Ptr(_)) | (Ptr(_) | FnPtr, Float) => {
862                Err(CastError::IllegalCast)
863            }
864
865            // ptr -> ptr
866            (Ptr(m_e), Ptr(m_c)) => self.check_ptr_ptr_cast(fcx, m_e, m_c), // ptr-ptr-cast
867
868            // ptr-addr-cast
869            (Ptr(m_expr), Int(_)) => self.check_ptr_addr_cast(fcx, m_expr),
870
871            (FnPtr, Int(_)) => {
872                // FIXME(#95489): there should eventually be a lint for these casts
873                Ok(CastKind::FnPtrAddrCast)
874            }
875            // addr-ptr-cast
876            (Int(_), Ptr(mt)) => self.check_addr_ptr_cast(fcx, mt),
877            // fn-ptr-cast
878            (FnPtr, Ptr(mt)) => self.check_fptr_ptr_cast(fcx, mt),
879
880            // enum -> int
881            (Int(CEnum), Int(_)) => self.check_enum_cast(fcx),
882
883            // prim -> prim
884            (Int(Char) | Int(Bool), Int(_)) => Ok(CastKind::PrimIntCast),
885
886            (Int(_) | Float, Int(_) | Float) => Ok(CastKind::NumericCast),
887        }
888    }
889
890    fn check_ptr_ptr_cast(
891        &self,
892        fcx: &FnCtxt<'a, 'tcx>,
893        m_src: ty::TypeAndMut<'tcx>,
894        m_dst: ty::TypeAndMut<'tcx>,
895    ) -> Result<CastKind, CastError<'tcx>> {
896        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/cast.rs:896",
                        "rustc_hir_typeck::cast", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/cast.rs"),
                        ::tracing_core::__macro_support::Option::Some(896u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::cast"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("check_ptr_ptr_cast m_src={0:?} m_dst={1:?}",
                                                    m_src, m_dst) as &dyn Value))])
            });
    } else { ; }
};debug!("check_ptr_ptr_cast m_src={m_src:?} m_dst={m_dst:?}");
897        // ptr-ptr cast. metadata must match.
898
899        let src_kind = fcx.tcx.erase_and_anonymize_regions(fcx.pointer_kind(m_src.ty, self.span)?);
900        let dst_kind = fcx.tcx.erase_and_anonymize_regions(fcx.pointer_kind(m_dst.ty, self.span)?);
901
902        // We can't cast if target pointer kind is unknown
903        let Some(dst_kind) = dst_kind else {
904            return Err(CastError::UnknownCastPtrKind);
905        };
906
907        // Cast to thin pointer is OK
908        if dst_kind == PointerKind::Thin {
909            return Ok(CastKind::PtrPtrCast);
910        }
911
912        // We can't cast to wide pointer if source pointer kind is unknown
913        let Some(src_kind) = src_kind else {
914            return Err(CastError::UnknownCastPtrKind);
915        };
916
917        match (src_kind, dst_kind) {
918            // thin -> fat? report invalid cast (don't complain about vtable kinds)
919            (PointerKind::Thin, _) => Err(CastError::SizedUnsizedCast),
920
921            // trait object -> trait object? need to do additional checks
922            (PointerKind::VTable(src_tty), PointerKind::VTable(dst_tty)) => {
923                match (src_tty.principal(), dst_tty.principal()) {
924                    // A<dyn Src<...> + SrcAuto> -> B<dyn Dst<...> + DstAuto>. need to make sure
925                    // - `Src` and `Dst` traits are the same
926                    // - traits have the same generic arguments
927                    // - projections are the same
928                    // - `SrcAuto` (+auto traits implied by `Src`) is a superset of `DstAuto`
929                    //
930                    // Note that trait upcasting goes through a different mechanism (`coerce_unsized`)
931                    // and is unaffected by this check.
932                    (Some(src_principal), Some(_)) => {
933                        let tcx = fcx.tcx;
934
935                        // We need to reconstruct trait object types.
936                        // `m_src` and `m_dst` won't work for us here because they will potentially
937                        // contain wrappers, which we do not care about.
938                        //
939                        // e.g. we want to allow `dyn T -> (dyn T,)`, etc.
940                        //
941                        // We also need to skip auto traits to emit an FCW and not an error.
942                        let src_obj = Ty::new_dynamic(
943                            tcx,
944                            tcx.mk_poly_existential_predicates(
945                                &src_tty.without_auto_traits().collect::<Vec<_>>(),
946                            ),
947                            tcx.lifetimes.re_erased,
948                        );
949                        let dst_obj = Ty::new_dynamic(
950                            tcx,
951                            tcx.mk_poly_existential_predicates(
952                                &dst_tty.without_auto_traits().collect::<Vec<_>>(),
953                            ),
954                            tcx.lifetimes.re_erased,
955                        );
956
957                        // `dyn Src = dyn Dst`, this checks for matching traits/generics/projections
958                        // This is `fcx.demand_eqtype`, but inlined to give a better error.
959                        let cause = fcx.misc(self.span);
960                        if fcx
961                            .at(&cause, fcx.param_env)
962                            .eq(DefineOpaqueTypes::Yes, src_obj, dst_obj)
963                            .map(|infer_ok| fcx.register_infer_ok_obligations(infer_ok))
964                            .is_err()
965                        {
966                            return Err(CastError::DifferingKinds { src_kind, dst_kind });
967                        }
968
969                        // Check that `SrcAuto` (+auto traits implied by `Src`) is a superset of `DstAuto`.
970                        // Emit an FCW otherwise.
971                        let src_auto: FxHashSet<_> = src_tty
972                            .auto_traits()
973                            .chain(
974                                elaborate::supertrait_def_ids(tcx, src_principal.def_id())
975                                    .filter(|def_id| tcx.trait_is_auto(*def_id)),
976                            )
977                            .collect();
978
979                        let added = dst_tty
980                            .auto_traits()
981                            .filter(|trait_did| !src_auto.contains(trait_did))
982                            .collect::<Vec<_>>();
983
984                        if !added.is_empty() {
985                            return Err(CastError::PtrPtrAddingAutoTrait(added));
986                        }
987
988                        Ok(CastKind::PtrPtrCast)
989                    }
990
991                    // dyn Auto -> dyn Auto'? ok.
992                    (None, None) => Ok(CastKind::PtrPtrCast),
993
994                    // dyn Trait -> dyn Auto? not ok (for now).
995                    //
996                    // Although dropping the principal is already allowed for unsizing coercions
997                    // (e.g. `*const (dyn Trait + Auto)` to `*const dyn Auto`), dropping it is
998                    // currently **NOT** allowed for (non-coercion) ptr-to-ptr casts (e.g
999                    // `*const Foo` to `*const Bar` where `Foo` has a `dyn Trait + Auto` tail
1000                    // and `Bar` has a `dyn Auto` tail), because the underlying MIR operations
1001                    // currently work very differently:
1002                    //
1003                    // * A MIR unsizing coercion on raw pointers to trait objects (`*const dyn Src`
1004                    //   to `*const dyn Dst`) is currently equivalent to downcasting the source to
1005                    //   the concrete sized type that it was originally unsized from first (via a
1006                    //   ptr-to-ptr cast from `*const Src` to `*const T` with `T: Sized`) and then
1007                    //   unsizing this thin pointer to the target type (unsizing `*const T` to
1008                    //   `*const Dst`). In particular, this means that the pointer's metadata
1009                    //   (vtable) will semantically change, e.g. for const eval and miri, even
1010                    //   though the vtables will always be merged for codegen.
1011                    //
1012                    // * A MIR ptr-to-ptr cast is currently equivalent to a transmute and does not
1013                    //   change the pointer metadata (vtable) at all.
1014                    //
1015                    // In addition to this potentially surprising difference between coercion and
1016                    // non-coercion casts, casting away the principal with a MIR ptr-to-ptr cast
1017                    // is currently considered undefined behavior:
1018                    //
1019                    // As a validity invariant of pointers to trait objects, we currently require
1020                    // that the principal of the vtable in the pointer metadata exactly matches
1021                    // the principal of the pointee type, where "no principal" is also considered
1022                    // a kind of principal.
1023                    (Some(_), None) => Err(CastError::DifferingKinds { src_kind, dst_kind }),
1024
1025                    // dyn Auto -> dyn Trait? not ok.
1026                    (None, Some(_)) => Err(CastError::DifferingKinds { src_kind, dst_kind }),
1027                }
1028            }
1029
1030            // fat -> fat? metadata kinds must match
1031            (src_kind, dst_kind) if src_kind == dst_kind => Ok(CastKind::PtrPtrCast),
1032
1033            (_, _) => Err(CastError::DifferingKinds { src_kind, dst_kind }),
1034        }
1035    }
1036
1037    fn check_fptr_ptr_cast(
1038        &self,
1039        fcx: &FnCtxt<'a, 'tcx>,
1040        m_cast: ty::TypeAndMut<'tcx>,
1041    ) -> Result<CastKind, CastError<'tcx>> {
1042        // fptr-ptr cast. must be to thin ptr
1043
1044        match fcx.pointer_kind(m_cast.ty, self.span)? {
1045            None => Err(CastError::UnknownCastPtrKind),
1046            Some(PointerKind::Thin) => Ok(CastKind::FnPtrPtrCast),
1047            _ => Err(CastError::IllegalCast),
1048        }
1049    }
1050
1051    fn check_ptr_addr_cast(
1052        &self,
1053        fcx: &FnCtxt<'a, 'tcx>,
1054        m_expr: ty::TypeAndMut<'tcx>,
1055    ) -> Result<CastKind, CastError<'tcx>> {
1056        // ptr-addr cast. must be from thin ptr
1057
1058        match fcx.pointer_kind(m_expr.ty, self.span)? {
1059            None => Err(CastError::UnknownExprPtrKind),
1060            Some(PointerKind::Thin) => Ok(CastKind::PtrAddrCast),
1061            _ => Err(CastError::NeedViaThinPtr),
1062        }
1063    }
1064
1065    fn check_ref_cast(
1066        &self,
1067        fcx: &FnCtxt<'a, 'tcx>,
1068        mut m_expr: ty::TypeAndMut<'tcx>,
1069        mut m_cast: ty::TypeAndMut<'tcx>,
1070    ) -> Result<CastKind, CastError<'tcx>> {
1071        // array-ptr-cast: allow mut-to-mut, mut-to-const, const-to-const
1072        m_expr.ty = fcx.resolve_vars_with_obligations(m_expr.ty);
1073        m_cast.ty = fcx.resolve_vars_with_obligations(m_cast.ty);
1074
1075        if m_expr.mutbl >= m_cast.mutbl
1076            && let ty::Array(ety, _) = m_expr.ty.kind()
1077            && fcx.can_eq(fcx.param_env, *ety, m_cast.ty)
1078        {
1079            // Due to historical reasons we allow directly casting references of
1080            // arrays into raw pointers of their element type.
1081
1082            // Coerce to a raw pointer so that we generate RawPtr in MIR.
1083            let array_ptr_type = Ty::new_ptr(fcx.tcx, m_expr.ty, m_expr.mutbl);
1084            fcx.coerce(self.expr, self.expr_ty, array_ptr_type, AllowTwoPhase::No, None)
1085                .unwrap_or_else(|_| {
1086                    ::rustc_middle::util::bug::bug_fmt(format_args!("could not cast from reference to array to pointer to array ({0:?} to {1:?})",
        self.expr_ty, array_ptr_type))bug!(
1087                        "could not cast from reference to array to pointer to array ({:?} to {:?})",
1088                        self.expr_ty,
1089                        array_ptr_type,
1090                    )
1091                });
1092
1093            // this will report a type mismatch if needed
1094            fcx.demand_eqtype(self.span, *ety, m_cast.ty);
1095            return Ok(CastKind::ArrayPtrCast);
1096        }
1097
1098        Err(CastError::IllegalCast)
1099    }
1100
1101    fn check_addr_ptr_cast(
1102        &self,
1103        fcx: &FnCtxt<'a, 'tcx>,
1104        m_cast: TypeAndMut<'tcx>,
1105    ) -> Result<CastKind, CastError<'tcx>> {
1106        // ptr-addr cast. pointer must be thin.
1107        match fcx.pointer_kind(m_cast.ty, self.span)? {
1108            None => Err(CastError::UnknownCastPtrKind),
1109            Some(PointerKind::Thin) => Ok(CastKind::AddrPtrCast),
1110            Some(PointerKind::VTable(_)) => Err(CastError::IntToWideCast(Some("a vtable"))),
1111            Some(PointerKind::Length) => Err(CastError::IntToWideCast(Some("a length"))),
1112            Some(PointerKind::OfAlias(_) | PointerKind::OfParam(_)) => {
1113                Err(CastError::IntToWideCast(None))
1114            }
1115        }
1116    }
1117
1118    fn check_enum_cast(&self, fcx: &FnCtxt<'a, 'tcx>) -> Result<CastKind, CastError<'tcx>> {
1119        if let ty::Adt(d, _) = self.expr_ty.kind()
1120            && d.has_dtor(fcx.tcx)
1121        {
1122            Err(CastError::CastEnumDrop)
1123        } else {
1124            Ok(CastKind::EnumCast)
1125        }
1126    }
1127
1128    fn try_coercion_cast(&self, fcx: &FnCtxt<'a, 'tcx>) -> Result<(), ty::error::TypeError<'tcx>> {
1129        match fcx.coerce(self.expr, self.expr_ty, self.cast_ty, AllowTwoPhase::No, None) {
1130            Ok(_) => Ok(()),
1131            Err(err) => Err(err),
1132        }
1133    }
1134
1135    /// Attempt to suggest using `.is_empty` when trying to cast from a
1136    /// collection type to a boolean.
1137    fn try_suggest_collection_to_bool(&self, fcx: &FnCtxt<'a, 'tcx>, err: &mut Diag<'_>) {
1138        if self.cast_ty.is_bool() {
1139            let derefed = fcx
1140                .autoderef(self.expr_span, self.expr_ty)
1141                .silence_errors()
1142                .find(|t| #[allow(non_exhaustive_omitted_patterns)] match t.0.kind() {
    ty::Str | ty::Slice(..) => true,
    _ => false,
}matches!(t.0.kind(), ty::Str | ty::Slice(..)));
1143
1144            if let Some((deref_ty, _)) = derefed {
1145                // Give a note about what the expr derefs to.
1146                if deref_ty != self.expr_ty.peel_refs() {
1147                    err.subdiagnostic(errors::DerefImplsIsEmpty { span: self.expr_span, deref_ty });
1148                }
1149
1150                // Create a multipart suggestion: add `!` and `.is_empty()` in
1151                // place of the cast.
1152                err.subdiagnostic(errors::UseIsEmpty {
1153                    lo: self.expr_span.shrink_to_lo(),
1154                    hi: self.span.with_lo(self.expr_span.hi()),
1155                    expr_ty: self.expr_ty,
1156                });
1157            }
1158        }
1159    }
1160}