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::{diagnostics, 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                    diagnostics::CannotCastToBoolHelp::Numeric(
393                        self.expr_span.shrink_to_hi().with_hi(self.span.hi()),
394                    )
395                } else {
396                    diagnostics::CannotCastToBoolHelp::Unsupported(self.span)
397                };
398                fcx.dcx().emit_err(diagnostics::CannotCastToBool {
399                    span: self.span,
400                    expr_ty,
401                    help,
402                });
403            }
404            CastError::CastToChar => {
405                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!(
406                    fcx.dcx(),
407                    self.span,
408                    self.expr_ty,
409                    E0604,
410                    "only `u8` can be cast as `char`, not `{}`",
411                    self.expr_ty
412                );
413                err.span_label(self.span, "invalid cast");
414                if self.expr_ty.is_numeric() {
415                    if self.expr_ty == fcx.tcx.types.u32 {
416                        err.multipart_suggestion(
417                            "consider using `char::from_u32` instead",
418                            ::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![
419                                (self.expr_span.shrink_to_lo(), "char::from_u32(".to_string()),
420                                (self.expr_span.shrink_to_hi().to(self.cast_span), ")".to_string()),
421                            ],
422                            Applicability::MachineApplicable,
423                        );
424                    } else if self.expr_ty == fcx.tcx.types.i8 {
425                        err.span_help(self.span, "consider casting from `u8` instead");
426                    } else {
427                        err.span_help(
428                            self.span,
429                            "consider using `char::from_u32` instead (via a `u32`)",
430                        );
431                    };
432                }
433                err.emit();
434            }
435            CastError::NonScalar => {
436                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!(
437                    fcx.dcx(),
438                    self.span,
439                    self.expr_ty,
440                    E0605,
441                    "non-primitive cast: `{}` as `{}`",
442                    self.expr_ty,
443                    fcx.ty_to_string(self.cast_ty)
444                );
445
446                if let Ok(snippet) = fcx.tcx.sess.source_map().span_to_snippet(self.expr_span)
447                    && #[allow(non_exhaustive_omitted_patterns)] match self.expr.kind {
    ExprKind::AddrOf(..) => true,
    _ => false,
}matches!(self.expr.kind, ExprKind::AddrOf(..))
448                {
449                    err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("casting reference expression `{0}` because `&` binds tighter than `as`",
                snippet))
    })format!(
450                        "casting reference expression `{}` because `&` binds tighter than `as`",
451                        snippet
452                    ));
453                }
454
455                let mut sugg = None;
456                let mut sugg_mutref = false;
457                if let ty::Ref(reg, cast_ty, mutbl) = *self.cast_ty.kind() {
458                    if let ty::RawPtr(expr_ty, _) = *self.expr_ty.kind()
459                        && fcx.may_coerce(
460                            Ty::new_ref(fcx.tcx, fcx.tcx.lifetimes.re_erased, expr_ty, mutbl),
461                            self.cast_ty,
462                        )
463                    {
464                        sugg = Some((::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("&{0}*", mutbl.prefix_str()))
    })format!("&{}*", mutbl.prefix_str()), cast_ty == expr_ty));
465                    } else if let ty::Ref(expr_reg, expr_ty, expr_mutbl) = *self.expr_ty.kind()
466                        && expr_mutbl == Mutability::Not
467                        && mutbl == Mutability::Mut
468                        && fcx.may_coerce(Ty::new_mut_ref(fcx.tcx, expr_reg, expr_ty), self.cast_ty)
469                    {
470                        sugg_mutref = true;
471                    }
472
473                    if !sugg_mutref
474                        && sugg == None
475                        && fcx.may_coerce(
476                            Ty::new_ref(fcx.tcx, reg, self.expr_ty, mutbl),
477                            self.cast_ty,
478                        )
479                    {
480                        sugg = Some((::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("&{0}", mutbl.prefix_str()))
    })format!("&{}", mutbl.prefix_str()), false));
481                    }
482                } else if let ty::RawPtr(_, mutbl) = *self.cast_ty.kind()
483                    && fcx.may_coerce(
484                        Ty::new_ref(fcx.tcx, fcx.tcx.lifetimes.re_erased, self.expr_ty, mutbl),
485                        self.cast_ty,
486                    )
487                {
488                    sugg = Some((::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("&{0}", mutbl.prefix_str()))
    })format!("&{}", mutbl.prefix_str()), false));
489                }
490                if sugg_mutref {
491                    err.span_label(self.span, "invalid cast");
492                    err.span_note(self.expr_span, "this reference is immutable");
493                    err.span_note(self.cast_span, "trying to cast to a mutable reference type");
494                } else if let Some((sugg, remove_cast)) = sugg {
495                    err.span_label(self.span, "invalid cast");
496
497                    let has_parens = fcx
498                        .tcx
499                        .sess
500                        .source_map()
501                        .span_to_snippet(self.expr_span)
502                        .is_ok_and(|snip| snip.starts_with('('));
503
504                    // Very crude check to see whether the expression must be wrapped
505                    // in parentheses for the suggestion to work (issue #89497).
506                    // Can/should be extended in the future.
507                    let needs_parens =
508                        !has_parens && #[allow(non_exhaustive_omitted_patterns)] match self.expr.kind {
    hir::ExprKind::Cast(..) => true,
    _ => false,
}matches!(self.expr.kind, hir::ExprKind::Cast(..));
509
510                    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)];
511                    if needs_parens {
512                        suggestion[0].1 += "(";
513                        suggestion.push((self.expr_span.shrink_to_hi(), ")".to_string()));
514                    }
515                    if remove_cast {
516                        suggestion.push((
517                            self.expr_span.shrink_to_hi().to(self.cast_span),
518                            String::new(),
519                        ));
520                    }
521
522                    err.multipart_suggestion(
523                        "consider borrowing the value",
524                        suggestion,
525                        Applicability::MachineApplicable,
526                    );
527                } else if !#[allow(non_exhaustive_omitted_patterns)] match self.cast_ty.kind() {
    ty::FnDef(..) | ty::FnPtr(..) | ty::Closure(..) => true,
    _ => false,
}matches!(
528                    self.cast_ty.kind(),
529                    ty::FnDef(..) | ty::FnPtr(..) | ty::Closure(..)
530                ) {
531                    // Check `impl From<self.expr_ty> for self.cast_ty {}` for accurate suggestion:
532                    if let Some(from_trait) = fcx.tcx.get_diagnostic_item(sym::From) {
533                        let ty = fcx.resolve_vars_if_possible(self.cast_ty);
534                        let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
535                        if fcx
536                            .infcx
537                            .type_implements_trait(from_trait, [ty, expr_ty], fcx.param_env)
538                            .must_apply_modulo_regions()
539                        {
540                            let to_ty = if let ty::Adt(def, args) = self.cast_ty.kind() {
541                                fcx.tcx.value_path_str_with_args(def.did(), args)
542                            } else {
543                                self.cast_ty.to_string()
544                            };
545                            err.multipart_suggestion(
546                                "consider using the `From` trait instead",
547                                ::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![
548                                    (self.expr_span.shrink_to_lo(), format!("{to_ty}::from(")),
549                                    (
550                                        self.expr_span.shrink_to_hi().to(self.cast_span),
551                                        ")".to_string(),
552                                    ),
553                                ],
554                                Applicability::MaybeIncorrect,
555                            );
556                        }
557                    }
558
559                    let (msg, note) = if let ty::Adt(adt, _) = self.expr_ty.kind()
560                        && adt.is_enum()
561                        && self.cast_ty.is_numeric()
562                    {
563                        (
564                            "an `as` expression can be used to convert enum types to numeric \
565                             types only if the enum type is unit-only or field-less",
566                            Some(
567                                "see https://doc.rust-lang.org/reference/items/enumerations.html#casting for more information",
568                            ),
569                        )
570                    } else {
571                        (
572                            "an `as` expression can only be used to convert between primitive \
573                             types or to coerce to a specific trait object",
574                            None,
575                        )
576                    };
577
578                    err.span_label(self.span, msg);
579
580                    if let Some(note) = note {
581                        err.note(note);
582                    }
583                } else {
584                    err.span_label(self.span, "invalid cast");
585                }
586
587                fcx.suggest_no_capture_closure(&mut err, self.cast_ty, self.expr_ty);
588                self.try_suggest_collection_to_bool(fcx, &mut err);
589
590                err.emit();
591            }
592            CastError::SizedUnsizedCast => {
593                let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
594                let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
595                fcx.dcx().emit_err(diagnostics::CastThinPointerToWidePointer {
596                    span: self.span,
597                    expr_ty,
598                    cast_ty,
599                    teach: fcx.tcx.sess.teach(E0607),
600                });
601            }
602            CastError::IntToWideCast(known_metadata) => {
603                let expr_if_nightly = fcx.tcx.sess.is_nightly_build().then_some(self.expr_span);
604                let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
605                let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
606                let metadata = known_metadata.unwrap_or("type-specific metadata");
607                let known_wide = known_metadata.is_some();
608                let span = self.cast_span;
609                let param_note = (!known_wide)
610                    .then(|| match cast_ty.kind() {
611                        ty::RawPtr(pointee, _) => match pointee.kind() {
612                            ty::Param(param) => {
613                                Some(diagnostics::IntToWideParamNote { param: param.name })
614                            }
615                            _ => None,
616                        },
617                        _ => None,
618                    })
619                    .flatten();
620                fcx.dcx().emit_err(diagnostics::IntToWide {
621                    span,
622                    metadata,
623                    expr_ty,
624                    cast_ty,
625                    expr_if_nightly,
626                    known_wide,
627                    param_note,
628                });
629            }
630            CastError::UnknownCastPtrKind | CastError::UnknownExprPtrKind => {
631                let unknown_cast_to = match e {
632                    CastError::UnknownCastPtrKind => true,
633                    CastError::UnknownExprPtrKind => false,
634                    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:?}"),
635                };
636                let (span, sub) = if unknown_cast_to {
637                    (self.cast_span, diagnostics::CastUnknownPointerSub::To(self.cast_span))
638                } else {
639                    (self.cast_span, diagnostics::CastUnknownPointerSub::From(self.span))
640                };
641                fcx.dcx().emit_err(diagnostics::CastUnknownPointer {
642                    span,
643                    to: unknown_cast_to,
644                    sub,
645                });
646            }
647            CastError::CastEnumDrop => {
648                let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
649                let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
650
651                fcx.dcx().emit_err(diagnostics::CastEnumDrop { span: self.span, expr_ty, cast_ty });
652            }
653            CastError::ForeignNonExhaustiveAdt => {
654                make_invalid_casting_error(
655                    self.span,
656                    self.expr_ty,
657                    self.cast_ty,
658                    fcx,
659                )
660                .with_note("cannot cast an enum with a non-exhaustive variant when it's defined in another crate")
661                .emit();
662            }
663            CastError::PtrPtrAddingAutoTrait(added) => {
664                fcx.dcx().emit_err(diagnostics::PtrCastAddAutoToObject {
665                    span: self.span,
666                    traits_len: added.len(),
667                    traits: {
668                        let mut traits: Vec<_> = added
669                            .into_iter()
670                            .map(|trait_did| fcx.tcx.def_path_str(trait_did))
671                            .collect();
672
673                        traits.sort();
674                        traits.into()
675                    },
676                });
677            }
678        }
679    }
680
681    fn report_cast_to_unsized_type(&self, fcx: &FnCtxt<'a, 'tcx>) -> ErrorGuaranteed {
682        if let Err(err) = self.cast_ty.error_reported() {
683            return err;
684        }
685        if let Err(err) = self.expr_ty.error_reported() {
686            return err;
687        }
688
689        let tstr = fcx.ty_to_string(self.cast_ty);
690        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!(
691            fcx.dcx(),
692            self.span,
693            self.expr_ty,
694            E0620,
695            "cast to unsized type: `{}` as `{}`",
696            fcx.resolve_vars_if_possible(self.expr_ty),
697            tstr
698        );
699        match self.expr_ty.kind() {
700            ty::Ref(_, _, mt) => {
701                let mtstr = mt.prefix_str();
702                err.span_suggestion_verbose(
703                    self.cast_span.shrink_to_lo(),
704                    "consider casting to a reference instead",
705                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("&{0}", mtstr))
    })format!("&{mtstr}"),
706                    Applicability::MachineApplicable,
707                );
708            }
709            ty::Adt(def, ..) if def.is_box() => {
710                err.multipart_suggestion(
711                    "you can cast to a `Box` instead",
712                    ::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![
713                        (self.cast_span.shrink_to_lo(), "Box<".to_string()),
714                        (self.cast_span.shrink_to_hi(), ">".to_string()),
715                    ],
716                    Applicability::MachineApplicable,
717                );
718            }
719            _ => {
720                err.span_help(self.expr_span, "consider using a box or reference as appropriate");
721            }
722        }
723        err.emit()
724    }
725
726    fn trivial_cast_lint(&self, fcx: &FnCtxt<'a, 'tcx>) {
727        let (numeric, lint) = if self.cast_ty.is_numeric() && self.expr_ty.is_numeric() {
728            (true, lint::builtin::TRIVIAL_NUMERIC_CASTS)
729        } else {
730            (false, lint::builtin::TRIVIAL_CASTS)
731        };
732        let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
733        let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
734        fcx.tcx.emit_node_span_lint(
735            lint,
736            self.expr.hir_id,
737            self.span,
738            diagnostics::TrivialCast { numeric, expr_ty, cast_ty },
739        );
740    }
741
742    fn expr_span_for_type_resolution(&self, fcx: &FnCtxt<'a, 'tcx>) -> Span {
743        if let hir::ExprKind::Index(_, idx, _) = self.expr.kind
744            && fcx.resolve_vars_if_possible(self.expr_ty).is_ty_var()
745            && fcx.resolve_vars_if_possible(fcx.node_ty(idx.hir_id)).is_ty_var()
746        {
747            index_operand_ambiguity_span(idx)
748        } else {
749            self.expr_span
750        }
751    }
752
753    #[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(753u32),
                                    ::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;
        }
        {
            let expr_span = self.expr_span_for_type_resolution(fcx);
            self.expr_ty =
                fcx.structurally_resolve_type(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:759",
                                    "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(759u32),
                                    ::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:777",
                                                    "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(777u32),
                                                    ::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:780",
                                                    "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(780u32),
                                                    ::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:789",
                                                        "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(789u32),
                                                        ::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")]
754    pub(crate) fn check(mut self, fcx: &FnCtxt<'a, 'tcx>) {
755        let expr_span = self.expr_span_for_type_resolution(fcx);
756        self.expr_ty = fcx.structurally_resolve_type(expr_span, self.expr_ty);
757        self.cast_ty = fcx.structurally_resolve_type(self.cast_span, self.cast_ty);
758
759        debug!("check_cast({}, {:?} as {:?})", self.expr.hir_id, self.expr_ty, self.cast_ty);
760
761        if !fcx.type_is_sized_modulo_regions(fcx.param_env, self.cast_ty)
762            && !self.cast_ty.has_infer_types()
763        {
764            self.report_cast_to_unsized_type(fcx);
765        } else if self.expr_ty.references_error() || self.cast_ty.references_error() {
766            // No sense in giving duplicate error messages
767        } else {
768            match self.try_coercion_cast(fcx) {
769                Ok(()) => {
770                    if self.expr_ty.is_raw_ptr() && self.cast_ty.is_raw_ptr() {
771                        // When casting a raw pointer to another raw pointer, we cannot convert the cast into
772                        // a coercion because the pointee types might only differ in regions, which HIR typeck
773                        // cannot distinguish. This would cause us to erroneously discard a cast which will
774                        // lead to a borrowck error like #113257.
775                        // We still did a coercion above to unify inference variables for `ptr as _` casts.
776                        // This does cause us to miss some trivial casts in the trivial cast lint.
777                        debug!(" -> PointerCast");
778                    } else {
779                        self.trivial_cast_lint(fcx);
780                        debug!(" -> CoercionCast");
781                        fcx.typeck_results
782                            .borrow_mut()
783                            .set_coercion_cast(self.expr.hir_id.local_id);
784                    }
785                }
786                Err(_) => {
787                    match self.do_check(fcx) {
788                        Ok(k) => {
789                            debug!(" -> {:?}", k);
790                        }
791                        Err(e) => self.report_cast_error(fcx, e),
792                    };
793                }
794            };
795        }
796    }
797    /// Checks a cast, and report an error if one exists. In some cases, this
798    /// can return Ok and create type errors in the fcx rather than returning
799    /// directly. coercion-cast is handled in check instead of here.
800    fn do_check(&self, fcx: &FnCtxt<'a, 'tcx>) -> Result<CastKind, CastError<'tcx>> {
801        use rustc_middle::ty::cast::CastTy::*;
802        use rustc_middle::ty::cast::IntTy::*;
803
804        let (t_from, t_cast) = match (CastTy::from_ty(self.expr_ty), CastTy::from_ty(self.cast_ty))
805        {
806            (Some(t_from), Some(t_cast)) => (t_from, t_cast),
807            // Function item types may need to be reified before casts.
808            (None, Some(t_cast)) => {
809                match *self.expr_ty.kind() {
810                    ty::FnDef(..) => {
811                        // Attempt a coercion to a fn pointer type.
812                        let f = fcx.normalize(
813                            self.expr_span,
814                            Unnormalized::new_wip(self.expr_ty.fn_sig(fcx.tcx)),
815                        );
816                        let res = fcx.coerce(
817                            self.expr,
818                            self.expr_ty,
819                            Ty::new_fn_ptr(fcx.tcx, f),
820                            AllowTwoPhase::No,
821                            None,
822                        );
823                        if let Err(TypeError::IntrinsicCast) = res {
824                            return Err(CastError::IllegalCast);
825                        }
826                        if res.is_err() {
827                            return Err(CastError::NonScalar);
828                        }
829                        (FnPtr, t_cast)
830                    }
831                    // Special case some errors for references, and check for
832                    // array-ptr-casts. `Ref` is not a CastTy because the cast
833                    // is split into a coercion to a pointer type, followed by
834                    // a cast.
835                    ty::Ref(_, inner_ty, mutbl) => {
836                        return match t_cast {
837                            Int(_) | Float => match *inner_ty.kind() {
838                                ty::Int(_)
839                                | ty::Uint(_)
840                                | ty::Float(_)
841                                | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(_)) => {
842                                    Err(CastError::NeedDeref)
843                                }
844                                _ => Err(CastError::NeedViaPtr),
845                            },
846                            // array-ptr-cast
847                            Ptr(mt) => {
848                                if !fcx.type_is_sized_modulo_regions(fcx.param_env, mt.ty) {
849                                    return Err(CastError::IllegalCast);
850                                }
851                                self.check_ref_cast(fcx, TypeAndMut { mutbl, ty: inner_ty }, mt)
852                            }
853                            _ => Err(CastError::NonScalar),
854                        };
855                    }
856                    _ => return Err(CastError::NonScalar),
857                }
858            }
859            _ => return Err(CastError::NonScalar),
860        };
861        if let ty::Adt(adt_def, _) = *self.expr_ty.kind()
862            && !adt_def.did().is_local()
863            && adt_def.variants().iter().any(VariantDef::is_field_list_non_exhaustive)
864        {
865            return Err(CastError::ForeignNonExhaustiveAdt);
866        }
867        match (t_from, t_cast) {
868            // These types have invariants! can't cast into them.
869            (_, Int(CEnum) | FnPtr) => Err(CastError::NonScalar),
870
871            // * -> Bool
872            (_, Int(Bool)) => Err(CastError::CastToBool),
873
874            // * -> Char
875            (Int(U(ty::UintTy::U8)), Int(Char)) => Ok(CastKind::U8CharCast), // u8-char-cast
876            (_, Int(Char)) => Err(CastError::CastToChar),
877
878            // prim -> float,ptr
879            (Int(Bool) | Int(CEnum) | Int(Char), Float) => Err(CastError::NeedViaInt),
880
881            (Int(Bool) | Int(CEnum) | Int(Char) | Float, Ptr(_)) | (Ptr(_) | FnPtr, Float) => {
882                Err(CastError::IllegalCast)
883            }
884
885            // ptr -> ptr
886            (Ptr(m_e), Ptr(m_c)) => self.check_ptr_ptr_cast(fcx, m_e, m_c), // ptr-ptr-cast
887
888            // ptr-addr-cast
889            (Ptr(m_expr), Int(_)) => self.check_ptr_addr_cast(fcx, m_expr),
890
891            (FnPtr, Int(_)) => {
892                // FIXME(#95489): there should eventually be a lint for these casts
893                Ok(CastKind::FnPtrAddrCast)
894            }
895            // addr-ptr-cast
896            (Int(_), Ptr(mt)) => self.check_addr_ptr_cast(fcx, mt),
897            // fn-ptr-cast
898            (FnPtr, Ptr(mt)) => self.check_fptr_ptr_cast(fcx, mt),
899
900            // enum -> int
901            (Int(CEnum), Int(_)) => self.check_enum_cast(fcx),
902
903            // prim -> prim
904            (Int(Char) | Int(Bool), Int(_)) => Ok(CastKind::PrimIntCast),
905
906            (Int(_) | Float, Int(_) | Float) => Ok(CastKind::NumericCast),
907        }
908    }
909
910    fn check_ptr_ptr_cast(
911        &self,
912        fcx: &FnCtxt<'a, 'tcx>,
913        m_src: ty::TypeAndMut<'tcx>,
914        m_dst: ty::TypeAndMut<'tcx>,
915    ) -> Result<CastKind, CastError<'tcx>> {
916        {
    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:916",
                        "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(916u32),
                        ::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:?}");
917        // ptr-ptr cast. metadata must match.
918
919        let src_kind = fcx.tcx.erase_and_anonymize_regions(fcx.pointer_kind(m_src.ty, self.span)?);
920        let dst_kind = fcx.tcx.erase_and_anonymize_regions(fcx.pointer_kind(m_dst.ty, self.span)?);
921
922        // We can't cast if target pointer kind is unknown
923        let Some(dst_kind) = dst_kind else {
924            return Err(CastError::UnknownCastPtrKind);
925        };
926
927        // Cast to thin pointer is OK
928        if dst_kind == PointerKind::Thin {
929            return Ok(CastKind::PtrPtrCast);
930        }
931
932        // We can't cast to wide pointer if source pointer kind is unknown
933        let Some(src_kind) = src_kind else {
934            return Err(CastError::UnknownCastPtrKind);
935        };
936
937        match (src_kind, dst_kind) {
938            // thin -> fat? report invalid cast (don't complain about vtable kinds)
939            (PointerKind::Thin, _) => Err(CastError::SizedUnsizedCast),
940
941            // trait object -> trait object? need to do additional checks
942            (PointerKind::VTable(src_tty), PointerKind::VTable(dst_tty)) => {
943                match (src_tty.principal(), dst_tty.principal()) {
944                    // A<dyn Src<...> + SrcAuto> -> B<dyn Dst<...> + DstAuto>. need to make sure
945                    // - `Src` and `Dst` traits are the same
946                    // - traits have the same generic arguments
947                    // - projections are the same
948                    // - `SrcAuto` (+auto traits implied by `Src`) is a superset of `DstAuto`
949                    //
950                    // Note that trait upcasting goes through a different mechanism (`coerce_unsized`)
951                    // and is unaffected by this check.
952                    (Some(src_principal), Some(_)) => {
953                        let tcx = fcx.tcx;
954
955                        // We need to reconstruct trait object types.
956                        // `m_src` and `m_dst` won't work for us here because they will potentially
957                        // contain wrappers, which we do not care about.
958                        //
959                        // e.g. we want to allow `dyn T -> (dyn T,)`, etc.
960                        //
961                        // We also need to skip auto traits to emit an FCW and not an error.
962                        let src_obj = Ty::new_dynamic(
963                            tcx,
964                            tcx.mk_poly_existential_predicates(
965                                &src_tty.without_auto_traits().collect::<Vec<_>>(),
966                            ),
967                            tcx.lifetimes.re_erased,
968                        );
969                        let dst_obj = Ty::new_dynamic(
970                            tcx,
971                            tcx.mk_poly_existential_predicates(
972                                &dst_tty.without_auto_traits().collect::<Vec<_>>(),
973                            ),
974                            tcx.lifetimes.re_erased,
975                        );
976
977                        // `dyn Src = dyn Dst`, this checks for matching traits/generics/projections
978                        // This is `fcx.demand_eqtype`, but inlined to give a better error.
979                        let cause = fcx.misc(self.span);
980                        if fcx
981                            .at(&cause, fcx.param_env)
982                            .eq(DefineOpaqueTypes::Yes, src_obj, dst_obj)
983                            .map(|infer_ok| fcx.register_infer_ok_obligations(infer_ok))
984                            .is_err()
985                        {
986                            return Err(CastError::DifferingKinds { src_kind, dst_kind });
987                        }
988
989                        // Check that `SrcAuto` (+auto traits implied by `Src`) is a superset of `DstAuto`.
990                        // Emit an FCW otherwise.
991                        let src_auto: FxHashSet<_> = src_tty
992                            .auto_traits()
993                            .chain(
994                                elaborate::supertrait_def_ids(tcx, src_principal.def_id())
995                                    .filter(|def_id| tcx.trait_is_auto(*def_id)),
996                            )
997                            .collect();
998
999                        let added = dst_tty
1000                            .auto_traits()
1001                            .filter(|trait_did| !src_auto.contains(trait_did))
1002                            .collect::<Vec<_>>();
1003
1004                        if !added.is_empty() {
1005                            return Err(CastError::PtrPtrAddingAutoTrait(added));
1006                        }
1007
1008                        Ok(CastKind::PtrPtrCast)
1009                    }
1010
1011                    // dyn Auto -> dyn Auto'? ok.
1012                    (None, None) => Ok(CastKind::PtrPtrCast),
1013
1014                    // dyn Trait -> dyn Auto? not ok (for now).
1015                    //
1016                    // Although dropping the principal is already allowed for unsizing coercions
1017                    // (e.g. `*const (dyn Trait + Auto)` to `*const dyn Auto`), dropping it is
1018                    // currently **NOT** allowed for (non-coercion) ptr-to-ptr casts (e.g
1019                    // `*const Foo` to `*const Bar` where `Foo` has a `dyn Trait + Auto` tail
1020                    // and `Bar` has a `dyn Auto` tail), because the underlying MIR operations
1021                    // currently work very differently:
1022                    //
1023                    // * A MIR unsizing coercion on raw pointers to trait objects (`*const dyn Src`
1024                    //   to `*const dyn Dst`) is currently equivalent to downcasting the source to
1025                    //   the concrete sized type that it was originally unsized from first (via a
1026                    //   ptr-to-ptr cast from `*const Src` to `*const T` with `T: Sized`) and then
1027                    //   unsizing this thin pointer to the target type (unsizing `*const T` to
1028                    //   `*const Dst`). In particular, this means that the pointer's metadata
1029                    //   (vtable) will semantically change, e.g. for const eval and miri, even
1030                    //   though the vtables will always be merged for codegen.
1031                    //
1032                    // * A MIR ptr-to-ptr cast is currently equivalent to a transmute and does not
1033                    //   change the pointer metadata (vtable) at all.
1034                    //
1035                    // In addition to this potentially surprising difference between coercion and
1036                    // non-coercion casts, casting away the principal with a MIR ptr-to-ptr cast
1037                    // is currently considered undefined behavior:
1038                    //
1039                    // As a validity invariant of pointers to trait objects, we currently require
1040                    // that the principal of the vtable in the pointer metadata exactly matches
1041                    // the principal of the pointee type, where "no principal" is also considered
1042                    // a kind of principal.
1043                    (Some(_), None) => Err(CastError::DifferingKinds { src_kind, dst_kind }),
1044
1045                    // dyn Auto -> dyn Trait? not ok.
1046                    (None, Some(_)) => Err(CastError::DifferingKinds { src_kind, dst_kind }),
1047                }
1048            }
1049
1050            // fat -> fat? metadata kinds must match
1051            (src_kind, dst_kind) if src_kind == dst_kind => Ok(CastKind::PtrPtrCast),
1052
1053            (_, _) => Err(CastError::DifferingKinds { src_kind, dst_kind }),
1054        }
1055    }
1056
1057    fn check_fptr_ptr_cast(
1058        &self,
1059        fcx: &FnCtxt<'a, 'tcx>,
1060        m_cast: ty::TypeAndMut<'tcx>,
1061    ) -> Result<CastKind, CastError<'tcx>> {
1062        // fptr-ptr cast. must be to thin ptr
1063
1064        match fcx.pointer_kind(m_cast.ty, self.span)? {
1065            None => Err(CastError::UnknownCastPtrKind),
1066            Some(PointerKind::Thin) => Ok(CastKind::FnPtrPtrCast),
1067            _ => Err(CastError::IllegalCast),
1068        }
1069    }
1070
1071    fn check_ptr_addr_cast(
1072        &self,
1073        fcx: &FnCtxt<'a, 'tcx>,
1074        m_expr: ty::TypeAndMut<'tcx>,
1075    ) -> Result<CastKind, CastError<'tcx>> {
1076        // ptr-addr cast. must be from thin ptr
1077
1078        match fcx.pointer_kind(m_expr.ty, self.span)? {
1079            None => Err(CastError::UnknownExprPtrKind),
1080            Some(PointerKind::Thin) => Ok(CastKind::PtrAddrCast),
1081            _ => Err(CastError::NeedViaThinPtr),
1082        }
1083    }
1084
1085    fn check_ref_cast(
1086        &self,
1087        fcx: &FnCtxt<'a, 'tcx>,
1088        mut m_expr: ty::TypeAndMut<'tcx>,
1089        mut m_cast: ty::TypeAndMut<'tcx>,
1090    ) -> Result<CastKind, CastError<'tcx>> {
1091        // array-ptr-cast: allow mut-to-mut, mut-to-const, const-to-const
1092        m_expr.ty = fcx.resolve_vars_with_obligations(m_expr.ty);
1093        m_cast.ty = fcx.resolve_vars_with_obligations(m_cast.ty);
1094
1095        if m_expr.mutbl >= m_cast.mutbl
1096            && let ty::Array(ety, _) = m_expr.ty.kind()
1097            && fcx.can_eq(fcx.param_env, *ety, m_cast.ty)
1098        {
1099            // Due to historical reasons we allow directly casting references of
1100            // arrays into raw pointers of their element type.
1101
1102            // Coerce to a raw pointer so that we generate RawPtr in MIR.
1103            let array_ptr_type = Ty::new_ptr(fcx.tcx, m_expr.ty, m_expr.mutbl);
1104            fcx.coerce(self.expr, self.expr_ty, array_ptr_type, AllowTwoPhase::No, None)
1105                .unwrap_or_else(|_| {
1106                    ::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!(
1107                        "could not cast from reference to array to pointer to array ({:?} to {:?})",
1108                        self.expr_ty,
1109                        array_ptr_type,
1110                    )
1111                });
1112
1113            // this will report a type mismatch if needed
1114            fcx.demand_eqtype(self.span, *ety, m_cast.ty);
1115            return Ok(CastKind::ArrayPtrCast);
1116        }
1117
1118        Err(CastError::IllegalCast)
1119    }
1120
1121    fn check_addr_ptr_cast(
1122        &self,
1123        fcx: &FnCtxt<'a, 'tcx>,
1124        m_cast: TypeAndMut<'tcx>,
1125    ) -> Result<CastKind, CastError<'tcx>> {
1126        // ptr-addr cast. pointer must be thin.
1127        match fcx.pointer_kind(m_cast.ty, self.span)? {
1128            None => Err(CastError::UnknownCastPtrKind),
1129            Some(PointerKind::Thin) => Ok(CastKind::AddrPtrCast),
1130            Some(PointerKind::VTable(_)) => Err(CastError::IntToWideCast(Some("a vtable"))),
1131            Some(PointerKind::Length) => Err(CastError::IntToWideCast(Some("a length"))),
1132            Some(PointerKind::OfAlias(_) | PointerKind::OfParam(_)) => {
1133                Err(CastError::IntToWideCast(None))
1134            }
1135        }
1136    }
1137
1138    fn check_enum_cast(&self, fcx: &FnCtxt<'a, 'tcx>) -> Result<CastKind, CastError<'tcx>> {
1139        if let ty::Adt(d, _) = self.expr_ty.kind()
1140            && d.has_dtor(fcx.tcx)
1141        {
1142            Err(CastError::CastEnumDrop)
1143        } else {
1144            Ok(CastKind::EnumCast)
1145        }
1146    }
1147
1148    fn try_coercion_cast(&self, fcx: &FnCtxt<'a, 'tcx>) -> Result<(), ty::error::TypeError<'tcx>> {
1149        match fcx.coerce(self.expr, self.expr_ty, self.cast_ty, AllowTwoPhase::No, None) {
1150            Ok(_) => Ok(()),
1151            Err(err) => Err(err),
1152        }
1153    }
1154
1155    /// Attempt to suggest using `.is_empty` when trying to cast from a
1156    /// collection type to a boolean.
1157    fn try_suggest_collection_to_bool(&self, fcx: &FnCtxt<'a, 'tcx>, err: &mut Diag<'_>) {
1158        if self.cast_ty.is_bool() {
1159            let derefed = fcx
1160                .autoderef(self.expr_span, self.expr_ty)
1161                .silence_errors()
1162                .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(..)));
1163
1164            if let Some((deref_ty, _)) = derefed {
1165                // Give a note about what the expr derefs to.
1166                if deref_ty != self.expr_ty.peel_refs() {
1167                    err.subdiagnostic(diagnostics::DerefImplsIsEmpty {
1168                        span: self.expr_span,
1169                        deref_ty,
1170                    });
1171                }
1172
1173                // Create a multipart suggestion: add `!` and `.is_empty()` in
1174                // place of the cast.
1175                err.subdiagnostic(diagnostics::UseIsEmpty {
1176                    lo: self.expr_span.shrink_to_lo(),
1177                    hi: self.span.with_lo(self.expr_span.hi()),
1178                    expr_ty: self.expr_ty,
1179                });
1180            }
1181        }
1182    }
1183}
1184
1185fn index_operand_ambiguity_span(expr: &hir::Expr<'_>) -> Span {
1186    match expr.kind {
1187        hir::ExprKind::MethodCall(segment, ..) => segment.ident.span,
1188        _ => expr.span,
1189    }
1190}