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_ast::util::parser::ExprPrecedence;
32use rustc_data_structures::fx::FxHashSet;
33use rustc_errors::codes::*;
34use rustc_errors::{Applicability, Diag, ErrorGuaranteed};
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::{self, Ty, TyCtxt, TypeAndMut, TypeVisitableExt, VariantDef};
43use rustc_middle::{bug, span_bug};
44use rustc_session::lint;
45use rustc_span::def_id::LOCAL_CRATE;
46use rustc_span::{DUMMY_SP, Span, sym};
47use rustc_trait_selection::infer::InferCtxtExt;
48use rustc_type_ir::elaborate;
49use tracing::{debug, instrument};
50
51use super::FnCtxt;
52use crate::{errors, type_error_struct};
53
54/// Reifies a cast check to be checked once we have full type information for
55/// a function context.
56#[derive(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}
68
69/// The kind of pointer and associated metadata (thin, length or vtable) - we
70/// only allow casts between wide pointers if their metadata have the same
71/// kind.
72#[derive(Debug, Copy, Clone, PartialEq, Eq, TypeVisitable, TypeFoldable)]
73enum PointerKind<'tcx> {
74    /// No metadata attached, ie pointer to sized type or foreign type
75    Thin,
76    /// A trait object
77    VTable(&'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>),
78    /// Slice
79    Length,
80    /// The unsize info of this projection or opaque type
81    OfAlias(ty::AliasTy<'tcx>),
82    /// The unsize info of this parameter
83    OfParam(ty::ParamTy),
84}
85
86impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
87    /// Returns the kind of unsize information of t, or None
88    /// if t is unknown.
89    fn pointer_kind(
90        &self,
91        t: Ty<'tcx>,
92        span: Span,
93    ) -> Result<Option<PointerKind<'tcx>>, ErrorGuaranteed> {
94        debug!("pointer_kind({:?}, {:?})", t, span);
95
96        let t = self.resolve_vars_if_possible(t);
97        t.error_reported()?;
98
99        if self.type_is_sized_modulo_regions(self.param_env, t) {
100            return Ok(Some(PointerKind::Thin));
101        }
102
103        let t = self.try_structurally_resolve_type(span, t);
104
105        Ok(match *t.kind() {
106            ty::Slice(_) | ty::Str => Some(PointerKind::Length),
107            ty::Dynamic(tty, _, ty::Dyn) => Some(PointerKind::VTable(tty)),
108            ty::Adt(def, args) if def.is_struct() => match def.non_enum_variant().tail_opt() {
109                None => Some(PointerKind::Thin),
110                Some(f) => {
111                    let field_ty = self.field_ty(span, f, args);
112                    self.pointer_kind(field_ty, span)?
113                }
114            },
115            ty::Tuple(fields) => match fields.last() {
116                None => Some(PointerKind::Thin),
117                Some(&f) => self.pointer_kind(f, span)?,
118            },
119
120            ty::UnsafeBinder(_) => todo!("FIXME(unsafe_binder)"),
121
122            // Pointers to foreign types are thin, despite being unsized
123            ty::Foreign(..) => Some(PointerKind::Thin),
124            // We should really try to normalize here.
125            ty::Alias(_, pi) => Some(PointerKind::OfAlias(pi)),
126            ty::Param(p) => Some(PointerKind::OfParam(p)),
127            // Insufficient type information.
128            ty::Placeholder(..) | ty::Bound(..) | ty::Infer(_) => None,
129
130            ty::Bool
131            | ty::Char
132            | ty::Int(..)
133            | ty::Uint(..)
134            | ty::Float(_)
135            | ty::Array(..)
136            | ty::CoroutineWitness(..)
137            | ty::RawPtr(_, _)
138            | ty::Ref(..)
139            | ty::Pat(..)
140            | ty::FnDef(..)
141            | ty::FnPtr(..)
142            | ty::Closure(..)
143            | ty::CoroutineClosure(..)
144            | ty::Coroutine(..)
145            | ty::Adt(..)
146            | ty::Never
147            | ty::Dynamic(_, _, ty::DynStar)
148            | ty::Error(_) => {
149                let guar = self
150                    .dcx()
151                    .span_delayed_bug(span, format!("`{t:?}` should be sized but is not?"));
152                return Err(guar);
153            }
154        })
155    }
156}
157
158#[derive(Copy, Clone, 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    /// Cast of int to (possibly) wide raw pointer.
179    ///
180    /// Argument is the specific name of the metadata in plain words, such as "a vtable"
181    /// or "a length". If this argument is None, then the metadata is unknown, for example,
182    /// when we're typechecking a type parameter with a ?Sized bound.
183    IntToWideCast(Option<&'static str>),
184    ForeignNonExhaustiveAdt,
185}
186
187impl From<ErrorGuaranteed> for CastError<'_> {
188    fn from(err: ErrorGuaranteed) -> Self {
189        CastError::ErrorGuaranteed(err)
190    }
191}
192
193fn make_invalid_casting_error<'a, 'tcx>(
194    span: Span,
195    expr_ty: Ty<'tcx>,
196    cast_ty: Ty<'tcx>,
197    fcx: &FnCtxt<'a, 'tcx>,
198) -> Diag<'a> {
199    type_error_struct!(
200        fcx.dcx(),
201        span,
202        expr_ty,
203        E0606,
204        "casting `{}` as `{}` is invalid",
205        fcx.ty_to_string(expr_ty),
206        fcx.ty_to_string(cast_ty)
207    )
208}
209
210/// If a cast from `from_ty` to `to_ty` is valid, returns a `Some` containing the kind
211/// of the cast.
212///
213/// This is a helper used from clippy.
214pub fn check_cast<'tcx>(
215    tcx: TyCtxt<'tcx>,
216    param_env: ty::ParamEnv<'tcx>,
217    e: &'tcx hir::Expr<'tcx>,
218    from_ty: Ty<'tcx>,
219    to_ty: Ty<'tcx>,
220) -> Option<CastKind> {
221    let hir_id = e.hir_id;
222    let local_def_id = hir_id.owner.def_id;
223
224    let root_ctxt = crate::TypeckRootCtxt::new(tcx, local_def_id);
225    let fn_ctxt = FnCtxt::new(&root_ctxt, param_env, local_def_id);
226
227    if let Ok(check) = CastCheck::new(
228        &fn_ctxt, e, from_ty, to_ty,
229        // We won't show any errors to the user, so the span is irrelevant here.
230        DUMMY_SP, DUMMY_SP,
231    ) {
232        check.do_check(&fn_ctxt).ok()
233    } else {
234        None
235    }
236}
237
238impl<'a, 'tcx> CastCheck<'tcx> {
239    pub(crate) fn new(
240        fcx: &FnCtxt<'a, 'tcx>,
241        expr: &'tcx hir::Expr<'tcx>,
242        expr_ty: Ty<'tcx>,
243        cast_ty: Ty<'tcx>,
244        cast_span: Span,
245        span: Span,
246    ) -> Result<CastCheck<'tcx>, ErrorGuaranteed> {
247        let expr_span = expr.span.find_ancestor_inside(span).unwrap_or(expr.span);
248        let check = CastCheck { expr, expr_ty, expr_span, cast_ty, cast_span, span };
249
250        // For better error messages, check for some obviously unsized
251        // cases now. We do a more thorough check at the end, once
252        // inference is more completely known.
253        match cast_ty.kind() {
254            ty::Dynamic(_, _, ty::Dyn) | ty::Slice(..) => {
255                Err(check.report_cast_to_unsized_type(fcx))
256            }
257            _ => Ok(check),
258        }
259    }
260
261    fn report_cast_error(&self, fcx: &FnCtxt<'a, 'tcx>, e: CastError<'tcx>) {
262        match e {
263            CastError::ErrorGuaranteed(_) => {
264                // an error has already been reported
265            }
266            CastError::NeedDeref => {
267                let mut err =
268                    make_invalid_casting_error(self.span, self.expr_ty, self.cast_ty, fcx);
269
270                if matches!(self.expr.kind, ExprKind::AddrOf(..)) {
271                    // get just the borrow part of the expression
272                    let span = self.expr_span.with_hi(self.expr.peel_borrows().span.lo());
273                    err.span_suggestion_verbose(
274                        span,
275                        "remove the unneeded borrow",
276                        "",
277                        Applicability::MachineApplicable,
278                    );
279                } else {
280                    err.span_suggestion_verbose(
281                        self.expr_span.shrink_to_lo(),
282                        "dereference the expression",
283                        "*",
284                        Applicability::MachineApplicable,
285                    );
286                }
287
288                err.emit();
289            }
290            CastError::NeedViaThinPtr | CastError::NeedViaPtr => {
291                let mut err =
292                    make_invalid_casting_error(self.span, self.expr_ty, self.cast_ty, fcx);
293                if self.cast_ty.is_integral() {
294                    err.help(format!("cast through {} first", match e {
295                        CastError::NeedViaPtr => "a raw pointer",
296                        CastError::NeedViaThinPtr => "a thin pointer",
297                        e => unreachable!("control flow means we should never encounter a {e:?}"),
298                    }));
299                }
300
301                self.try_suggest_collection_to_bool(fcx, &mut err);
302
303                err.emit();
304            }
305            CastError::NeedViaInt => {
306                make_invalid_casting_error(self.span, self.expr_ty, self.cast_ty, fcx)
307                    .with_help("cast through an integer first")
308                    .emit();
309            }
310            CastError::IllegalCast => {
311                make_invalid_casting_error(self.span, self.expr_ty, self.cast_ty, fcx).emit();
312            }
313            CastError::DifferingKinds { src_kind, dst_kind } => {
314                let mut err =
315                    make_invalid_casting_error(self.span, self.expr_ty, self.cast_ty, fcx);
316
317                match (src_kind, dst_kind) {
318                    (PointerKind::VTable(_), PointerKind::VTable(_)) => {
319                        err.note("the trait objects may have different vtables");
320                    }
321                    (
322                        PointerKind::OfParam(_) | PointerKind::OfAlias(_),
323                        PointerKind::OfParam(_)
324                        | PointerKind::OfAlias(_)
325                        | PointerKind::VTable(_)
326                        | PointerKind::Length,
327                    )
328                    | (
329                        PointerKind::VTable(_) | PointerKind::Length,
330                        PointerKind::OfParam(_) | PointerKind::OfAlias(_),
331                    ) => {
332                        err.note("the pointers may have different metadata");
333                    }
334                    (PointerKind::VTable(_), PointerKind::Length)
335                    | (PointerKind::Length, PointerKind::VTable(_)) => {
336                        err.note("the pointers have different metadata");
337                    }
338                    (
339                        PointerKind::Thin,
340                        PointerKind::Thin
341                        | PointerKind::VTable(_)
342                        | PointerKind::Length
343                        | PointerKind::OfParam(_)
344                        | PointerKind::OfAlias(_),
345                    )
346                    | (
347                        PointerKind::VTable(_)
348                        | PointerKind::Length
349                        | PointerKind::OfParam(_)
350                        | PointerKind::OfAlias(_),
351                        PointerKind::Thin,
352                    )
353                    | (PointerKind::Length, PointerKind::Length) => {
354                        span_bug!(self.span, "unexpected cast error: {e:?}")
355                    }
356                }
357
358                err.emit();
359            }
360            CastError::CastToBool => {
361                let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
362                let help = if self.expr_ty.is_numeric() {
363                    errors::CannotCastToBoolHelp::Numeric(
364                        self.expr_span.shrink_to_hi().with_hi(self.span.hi()),
365                    )
366                } else {
367                    errors::CannotCastToBoolHelp::Unsupported(self.span)
368                };
369                fcx.dcx().emit_err(errors::CannotCastToBool { span: self.span, expr_ty, help });
370            }
371            CastError::CastToChar => {
372                let mut err = type_error_struct!(
373                    fcx.dcx(),
374                    self.span,
375                    self.expr_ty,
376                    E0604,
377                    "only `u8` can be cast as `char`, not `{}`",
378                    self.expr_ty
379                );
380                err.span_label(self.span, "invalid cast");
381                if self.expr_ty.is_numeric() {
382                    if self.expr_ty == fcx.tcx.types.u32 {
383                        match fcx.tcx.sess.source_map().span_to_snippet(self.expr.span) {
384                            Ok(snippet) => err.span_suggestion(
385                                self.span,
386                                "try `char::from_u32` instead",
387                                format!("char::from_u32({snippet})"),
388                                Applicability::MachineApplicable,
389                            ),
390
391                            Err(_) => err.span_help(self.span, "try `char::from_u32` instead"),
392                        };
393                    } else if self.expr_ty == fcx.tcx.types.i8 {
394                        err.span_help(self.span, "try casting from `u8` instead");
395                    } else {
396                        err.span_help(self.span, "try `char::from_u32` instead (via a `u32`)");
397                    };
398                }
399                err.emit();
400            }
401            CastError::NonScalar => {
402                let mut err = type_error_struct!(
403                    fcx.dcx(),
404                    self.span,
405                    self.expr_ty,
406                    E0605,
407                    "non-primitive cast: `{}` as `{}`",
408                    self.expr_ty,
409                    fcx.ty_to_string(self.cast_ty)
410                );
411                let mut sugg = None;
412                let mut sugg_mutref = false;
413                if let ty::Ref(reg, cast_ty, mutbl) = *self.cast_ty.kind() {
414                    if let ty::RawPtr(expr_ty, _) = *self.expr_ty.kind()
415                        && fcx.may_coerce(
416                            Ty::new_ref(fcx.tcx, fcx.tcx.lifetimes.re_erased, expr_ty, mutbl),
417                            self.cast_ty,
418                        )
419                    {
420                        sugg = Some((format!("&{}*", mutbl.prefix_str()), cast_ty == expr_ty));
421                    } else if let ty::Ref(expr_reg, expr_ty, expr_mutbl) = *self.expr_ty.kind()
422                        && expr_mutbl == Mutability::Not
423                        && mutbl == Mutability::Mut
424                        && fcx.may_coerce(Ty::new_mut_ref(fcx.tcx, expr_reg, expr_ty), self.cast_ty)
425                    {
426                        sugg_mutref = true;
427                    }
428
429                    if !sugg_mutref
430                        && sugg == None
431                        && fcx.may_coerce(
432                            Ty::new_ref(fcx.tcx, reg, self.expr_ty, mutbl),
433                            self.cast_ty,
434                        )
435                    {
436                        sugg = Some((format!("&{}", mutbl.prefix_str()), false));
437                    }
438                } else if let ty::RawPtr(_, mutbl) = *self.cast_ty.kind()
439                    && fcx.may_coerce(
440                        Ty::new_ref(fcx.tcx, fcx.tcx.lifetimes.re_erased, self.expr_ty, mutbl),
441                        self.cast_ty,
442                    )
443                {
444                    sugg = Some((format!("&{}", mutbl.prefix_str()), false));
445                }
446                if sugg_mutref {
447                    err.span_label(self.span, "invalid cast");
448                    err.span_note(self.expr_span, "this reference is immutable");
449                    err.span_note(self.cast_span, "trying to cast to a mutable reference type");
450                } else if let Some((sugg, remove_cast)) = sugg {
451                    err.span_label(self.span, "invalid cast");
452
453                    let has_parens = fcx
454                        .tcx
455                        .sess
456                        .source_map()
457                        .span_to_snippet(self.expr_span)
458                        .is_ok_and(|snip| snip.starts_with('('));
459
460                    // Very crude check to see whether the expression must be wrapped
461                    // in parentheses for the suggestion to work (issue #89497).
462                    // Can/should be extended in the future.
463                    let needs_parens =
464                        !has_parens && matches!(self.expr.kind, hir::ExprKind::Cast(..));
465
466                    let mut suggestion = vec![(self.expr_span.shrink_to_lo(), sugg)];
467                    if needs_parens {
468                        suggestion[0].1 += "(";
469                        suggestion.push((self.expr_span.shrink_to_hi(), ")".to_string()));
470                    }
471                    if remove_cast {
472                        suggestion.push((
473                            self.expr_span.shrink_to_hi().to(self.cast_span),
474                            String::new(),
475                        ));
476                    }
477
478                    err.multipart_suggestion_verbose(
479                        "consider borrowing the value",
480                        suggestion,
481                        Applicability::MachineApplicable,
482                    );
483                } else if !matches!(
484                    self.cast_ty.kind(),
485                    ty::FnDef(..) | ty::FnPtr(..) | ty::Closure(..)
486                ) {
487                    let mut label = true;
488                    // Check `impl From<self.expr_ty> for self.cast_ty {}` for accurate suggestion:
489                    if let Ok(snippet) = fcx.tcx.sess.source_map().span_to_snippet(self.expr_span)
490                        && let Some(from_trait) = fcx.tcx.get_diagnostic_item(sym::From)
491                    {
492                        let ty = fcx.resolve_vars_if_possible(self.cast_ty);
493                        // Erase regions to avoid panic in `prove_value` when calling
494                        // `type_implements_trait`.
495                        let ty = fcx.tcx.erase_regions(ty);
496                        let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
497                        let expr_ty = fcx.tcx.erase_regions(expr_ty);
498                        if fcx
499                            .infcx
500                            .type_implements_trait(from_trait, [ty, expr_ty], fcx.param_env)
501                            .must_apply_modulo_regions()
502                        {
503                            label = false;
504                            err.span_suggestion(
505                                self.span,
506                                "consider using the `From` trait instead",
507                                format!("{}::from({})", self.cast_ty, snippet),
508                                Applicability::MaybeIncorrect,
509                            );
510                        }
511                    }
512
513                    let (msg, note) = if let ty::Adt(adt, _) = self.expr_ty.kind()
514                        && adt.is_enum()
515                        && self.cast_ty.is_numeric()
516                    {
517                        (
518                            "an `as` expression can be used to convert enum types to numeric \
519                             types only if the enum type is unit-only or field-less",
520                            Some(
521                                "see https://doc.rust-lang.org/reference/items/enumerations.html#casting for more information",
522                            ),
523                        )
524                    } else {
525                        (
526                            "an `as` expression can only be used to convert between primitive \
527                             types or to coerce to a specific trait object",
528                            None,
529                        )
530                    };
531
532                    if label {
533                        err.span_label(self.span, msg);
534                    } else {
535                        err.note(msg);
536                    }
537
538                    if let Some(note) = note {
539                        err.note(note);
540                    }
541                } else {
542                    err.span_label(self.span, "invalid cast");
543                }
544
545                fcx.suggest_no_capture_closure(&mut err, self.cast_ty, self.expr_ty);
546                self.try_suggest_collection_to_bool(fcx, &mut err);
547
548                err.emit();
549            }
550            CastError::SizedUnsizedCast => {
551                fcx.dcx().emit_err(errors::CastThinPointerToWidePointer {
552                    span: self.span,
553                    expr_ty: self.expr_ty,
554                    cast_ty: fcx.ty_to_string(self.cast_ty),
555                    teach: fcx.tcx.sess.teach(E0607),
556                });
557            }
558            CastError::IntToWideCast(known_metadata) => {
559                let expr_if_nightly = fcx.tcx.sess.is_nightly_build().then_some(self.expr_span);
560                let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
561                let expr_ty = fcx.ty_to_string(self.expr_ty);
562                let metadata = known_metadata.unwrap_or("type-specific metadata");
563                let known_wide = known_metadata.is_some();
564                let span = self.cast_span;
565                fcx.dcx().emit_err(errors::IntToWide {
566                    span,
567                    metadata,
568                    expr_ty,
569                    cast_ty,
570                    expr_if_nightly,
571                    known_wide,
572                });
573            }
574            CastError::UnknownCastPtrKind | CastError::UnknownExprPtrKind => {
575                let unknown_cast_to = match e {
576                    CastError::UnknownCastPtrKind => true,
577                    CastError::UnknownExprPtrKind => false,
578                    e => unreachable!("control flow means we should never encounter a {e:?}"),
579                };
580                let (span, sub) = if unknown_cast_to {
581                    (self.cast_span, errors::CastUnknownPointerSub::To(self.cast_span))
582                } else {
583                    (self.cast_span, errors::CastUnknownPointerSub::From(self.span))
584                };
585                fcx.dcx().emit_err(errors::CastUnknownPointer { span, to: unknown_cast_to, sub });
586            }
587            CastError::ForeignNonExhaustiveAdt => {
588                make_invalid_casting_error(
589                    self.span,
590                    self.expr_ty,
591                    self.cast_ty,
592                    fcx,
593                )
594                .with_note("cannot cast an enum with a non-exhaustive variant when it's defined in another crate")
595                .emit();
596            }
597        }
598    }
599
600    fn report_cast_to_unsized_type(&self, fcx: &FnCtxt<'a, 'tcx>) -> ErrorGuaranteed {
601        if let Err(err) = self.cast_ty.error_reported() {
602            return err;
603        }
604        if let Err(err) = self.expr_ty.error_reported() {
605            return err;
606        }
607
608        let tstr = fcx.ty_to_string(self.cast_ty);
609        let mut err = type_error_struct!(
610            fcx.dcx(),
611            self.span,
612            self.expr_ty,
613            E0620,
614            "cast to unsized type: `{}` as `{}`",
615            fcx.resolve_vars_if_possible(self.expr_ty),
616            tstr
617        );
618        match self.expr_ty.kind() {
619            ty::Ref(_, _, mt) => {
620                let mtstr = mt.prefix_str();
621                match fcx.tcx.sess.source_map().span_to_snippet(self.cast_span) {
622                    Ok(s) => {
623                        err.span_suggestion(
624                            self.cast_span,
625                            "try casting to a reference instead",
626                            format!("&{mtstr}{s}"),
627                            Applicability::MachineApplicable,
628                        );
629                    }
630                    Err(_) => {
631                        let msg = format!("did you mean `&{mtstr}{tstr}`?");
632                        err.span_help(self.cast_span, msg);
633                    }
634                }
635            }
636            ty::Adt(def, ..) if def.is_box() => {
637                match fcx.tcx.sess.source_map().span_to_snippet(self.cast_span) {
638                    Ok(s) => {
639                        err.span_suggestion(
640                            self.cast_span,
641                            "you can cast to a `Box` instead",
642                            format!("Box<{s}>"),
643                            Applicability::MachineApplicable,
644                        );
645                    }
646                    Err(_) => {
647                        err.span_help(
648                            self.cast_span,
649                            format!("you might have meant `Box<{tstr}>`"),
650                        );
651                    }
652                }
653            }
654            _ => {
655                err.span_help(self.expr_span, "consider using a box or reference as appropriate");
656            }
657        }
658        err.emit()
659    }
660
661    fn trivial_cast_lint(&self, fcx: &FnCtxt<'a, 'tcx>) {
662        let (numeric, lint) = if self.cast_ty.is_numeric() && self.expr_ty.is_numeric() {
663            (true, lint::builtin::TRIVIAL_NUMERIC_CASTS)
664        } else {
665            (false, lint::builtin::TRIVIAL_CASTS)
666        };
667        let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
668        let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
669        fcx.tcx.emit_node_span_lint(
670            lint,
671            self.expr.hir_id,
672            self.span,
673            errors::TrivialCast { numeric, expr_ty, cast_ty },
674        );
675    }
676
677    #[instrument(skip(fcx), level = "debug")]
678    pub(crate) fn check(mut self, fcx: &FnCtxt<'a, 'tcx>) {
679        self.expr_ty = fcx.structurally_resolve_type(self.expr_span, self.expr_ty);
680        self.cast_ty = fcx.structurally_resolve_type(self.cast_span, self.cast_ty);
681
682        debug!("check_cast({}, {:?} as {:?})", self.expr.hir_id, self.expr_ty, self.cast_ty);
683
684        if !fcx.type_is_sized_modulo_regions(fcx.param_env, self.cast_ty)
685            && !self.cast_ty.has_infer_types()
686        {
687            self.report_cast_to_unsized_type(fcx);
688        } else if self.expr_ty.references_error() || self.cast_ty.references_error() {
689            // No sense in giving duplicate error messages
690        } else {
691            match self.try_coercion_cast(fcx) {
692                Ok(()) => {
693                    if self.expr_ty.is_raw_ptr() && self.cast_ty.is_raw_ptr() {
694                        // When casting a raw pointer to another raw pointer, we cannot convert the cast into
695                        // a coercion because the pointee types might only differ in regions, which HIR typeck
696                        // cannot distinguish. This would cause us to erroneously discard a cast which will
697                        // lead to a borrowck error like #113257.
698                        // We still did a coercion above to unify inference variables for `ptr as _` casts.
699                        // This does cause us to miss some trivial casts in the trivial cast lint.
700                        debug!(" -> PointerCast");
701                    } else {
702                        self.trivial_cast_lint(fcx);
703                        debug!(" -> CoercionCast");
704                        fcx.typeck_results
705                            .borrow_mut()
706                            .set_coercion_cast(self.expr.hir_id.local_id);
707                    }
708                }
709                Err(_) => {
710                    match self.do_check(fcx) {
711                        Ok(k) => {
712                            debug!(" -> {:?}", k);
713                        }
714                        Err(e) => self.report_cast_error(fcx, e),
715                    };
716                }
717            };
718        }
719    }
720    /// Checks a cast, and report an error if one exists. In some cases, this
721    /// can return Ok and create type errors in the fcx rather than returning
722    /// directly. coercion-cast is handled in check instead of here.
723    fn do_check(&self, fcx: &FnCtxt<'a, 'tcx>) -> Result<CastKind, CastError<'tcx>> {
724        use rustc_middle::ty::cast::CastTy::*;
725        use rustc_middle::ty::cast::IntTy::*;
726
727        if self.cast_ty.is_dyn_star() {
728            // This coercion will fail if the feature is not enabled, OR
729            // if the coercion is (currently) illegal (e.g. `dyn* Foo + Send`
730            // to `dyn* Foo`). Report "casting is invalid" rather than
731            // "non-primitive cast".
732            return Err(CastError::IllegalCast);
733        }
734
735        let (t_from, t_cast) = match (CastTy::from_ty(self.expr_ty), CastTy::from_ty(self.cast_ty))
736        {
737            (Some(t_from), Some(t_cast)) => (t_from, t_cast),
738            // Function item types may need to be reified before casts.
739            (None, Some(t_cast)) => {
740                match *self.expr_ty.kind() {
741                    ty::FnDef(..) => {
742                        // Attempt a coercion to a fn pointer type.
743                        let f = fcx.normalize(self.expr_span, self.expr_ty.fn_sig(fcx.tcx));
744                        let res = fcx.coerce(
745                            self.expr,
746                            self.expr_ty,
747                            Ty::new_fn_ptr(fcx.tcx, f),
748                            AllowTwoPhase::No,
749                            None,
750                        );
751                        if let Err(TypeError::IntrinsicCast) = res {
752                            return Err(CastError::IllegalCast);
753                        }
754                        if res.is_err() {
755                            return Err(CastError::NonScalar);
756                        }
757                        (FnPtr, t_cast)
758                    }
759                    // Special case some errors for references, and check for
760                    // array-ptr-casts. `Ref` is not a CastTy because the cast
761                    // is split into a coercion to a pointer type, followed by
762                    // a cast.
763                    ty::Ref(_, inner_ty, mutbl) => {
764                        return match t_cast {
765                            Int(_) | Float => match *inner_ty.kind() {
766                                ty::Int(_)
767                                | ty::Uint(_)
768                                | ty::Float(_)
769                                | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(_)) => {
770                                    Err(CastError::NeedDeref)
771                                }
772                                _ => Err(CastError::NeedViaPtr),
773                            },
774                            // array-ptr-cast
775                            Ptr(mt) => {
776                                if !fcx.type_is_sized_modulo_regions(fcx.param_env, mt.ty) {
777                                    return Err(CastError::IllegalCast);
778                                }
779                                self.check_ref_cast(fcx, TypeAndMut { mutbl, ty: inner_ty }, mt)
780                            }
781                            _ => Err(CastError::NonScalar),
782                        };
783                    }
784                    _ => return Err(CastError::NonScalar),
785                }
786            }
787            _ => return Err(CastError::NonScalar),
788        };
789        if let ty::Adt(adt_def, _) = *self.expr_ty.kind()
790            && adt_def.did().krate != LOCAL_CRATE
791            && adt_def.variants().iter().any(VariantDef::is_field_list_non_exhaustive)
792        {
793            return Err(CastError::ForeignNonExhaustiveAdt);
794        }
795        match (t_from, t_cast) {
796            // These types have invariants! can't cast into them.
797            (_, Int(CEnum) | FnPtr) => Err(CastError::NonScalar),
798
799            // * -> Bool
800            (_, Int(Bool)) => Err(CastError::CastToBool),
801
802            // * -> Char
803            (Int(U(ty::UintTy::U8)), Int(Char)) => Ok(CastKind::U8CharCast), // u8-char-cast
804            (_, Int(Char)) => Err(CastError::CastToChar),
805
806            // prim -> float,ptr
807            (Int(Bool) | Int(CEnum) | Int(Char), Float) => Err(CastError::NeedViaInt),
808
809            (Int(Bool) | Int(CEnum) | Int(Char) | Float, Ptr(_)) | (Ptr(_) | FnPtr, Float) => {
810                Err(CastError::IllegalCast)
811            }
812
813            // ptr -> ptr
814            (Ptr(m_e), Ptr(m_c)) => self.check_ptr_ptr_cast(fcx, m_e, m_c), // ptr-ptr-cast
815
816            // ptr-addr-cast
817            (Ptr(m_expr), Int(t_c)) => {
818                self.lossy_provenance_ptr2int_lint(fcx, t_c);
819                self.check_ptr_addr_cast(fcx, m_expr)
820            }
821            (FnPtr, Int(_)) => {
822                // FIXME(#95489): there should eventually be a lint for these casts
823                Ok(CastKind::FnPtrAddrCast)
824            }
825            // addr-ptr-cast
826            (Int(_), Ptr(mt)) => {
827                self.fuzzy_provenance_int2ptr_lint(fcx);
828                self.check_addr_ptr_cast(fcx, mt)
829            }
830            // fn-ptr-cast
831            (FnPtr, Ptr(mt)) => self.check_fptr_ptr_cast(fcx, mt),
832
833            // prim -> prim
834            (Int(CEnum), Int(_)) => {
835                self.err_if_cenum_impl_drop(fcx);
836                Ok(CastKind::EnumCast)
837            }
838            (Int(Char) | Int(Bool), Int(_)) => Ok(CastKind::PrimIntCast),
839
840            (Int(_) | Float, Int(_) | Float) => Ok(CastKind::NumericCast),
841        }
842    }
843
844    fn check_ptr_ptr_cast(
845        &self,
846        fcx: &FnCtxt<'a, 'tcx>,
847        m_src: ty::TypeAndMut<'tcx>,
848        m_dst: ty::TypeAndMut<'tcx>,
849    ) -> Result<CastKind, CastError<'tcx>> {
850        debug!("check_ptr_ptr_cast m_src={m_src:?} m_dst={m_dst:?}");
851        // ptr-ptr cast. metadata must match.
852
853        let src_kind = fcx.tcx.erase_regions(fcx.pointer_kind(m_src.ty, self.span)?);
854        let dst_kind = fcx.tcx.erase_regions(fcx.pointer_kind(m_dst.ty, self.span)?);
855
856        // We can't cast if target pointer kind is unknown
857        let Some(dst_kind) = dst_kind else {
858            return Err(CastError::UnknownCastPtrKind);
859        };
860
861        // Cast to thin pointer is OK
862        if dst_kind == PointerKind::Thin {
863            return Ok(CastKind::PtrPtrCast);
864        }
865
866        // We can't cast to wide pointer if source pointer kind is unknown
867        let Some(src_kind) = src_kind else {
868            return Err(CastError::UnknownCastPtrKind);
869        };
870
871        match (src_kind, dst_kind) {
872            // thin -> fat? report invalid cast (don't complain about vtable kinds)
873            (PointerKind::Thin, _) => Err(CastError::SizedUnsizedCast),
874
875            // trait object -> trait object? need to do additional checks
876            (PointerKind::VTable(src_tty), PointerKind::VTable(dst_tty)) => {
877                match (src_tty.principal(), dst_tty.principal()) {
878                    // A<dyn Src<...> + SrcAuto> -> B<dyn Dst<...> + DstAuto>. need to make sure
879                    // - `Src` and `Dst` traits are the same
880                    // - traits have the same generic arguments
881                    // - projections are the same
882                    // - `SrcAuto` (+auto traits implied by `Src`) is a superset of `DstAuto`
883                    //
884                    // Note that trait upcasting goes through a different mechanism (`coerce_unsized`)
885                    // and is unaffected by this check.
886                    (Some(src_principal), Some(_)) => {
887                        let tcx = fcx.tcx;
888
889                        // We need to reconstruct trait object types.
890                        // `m_src` and `m_dst` won't work for us here because they will potentially
891                        // contain wrappers, which we do not care about.
892                        //
893                        // e.g. we want to allow `dyn T -> (dyn T,)`, etc.
894                        //
895                        // We also need to skip auto traits to emit an FCW and not an error.
896                        let src_obj = tcx.mk_ty_from_kind(ty::Dynamic(
897                            tcx.mk_poly_existential_predicates(
898                                &src_tty.without_auto_traits().collect::<Vec<_>>(),
899                            ),
900                            tcx.lifetimes.re_erased,
901                            ty::Dyn,
902                        ));
903                        let dst_obj = tcx.mk_ty_from_kind(ty::Dynamic(
904                            tcx.mk_poly_existential_predicates(
905                                &dst_tty.without_auto_traits().collect::<Vec<_>>(),
906                            ),
907                            tcx.lifetimes.re_erased,
908                            ty::Dyn,
909                        ));
910
911                        // `dyn Src = dyn Dst`, this checks for matching traits/generics/projections
912                        // This is `fcx.demand_eqtype`, but inlined to give a better error.
913                        let cause = fcx.misc(self.span);
914                        if fcx
915                            .at(&cause, fcx.param_env)
916                            .eq(DefineOpaqueTypes::Yes, src_obj, dst_obj)
917                            .map(|infer_ok| fcx.register_infer_ok_obligations(infer_ok))
918                            .is_err()
919                        {
920                            return Err(CastError::DifferingKinds { src_kind, dst_kind });
921                        }
922
923                        // Check that `SrcAuto` (+auto traits implied by `Src`) is a superset of `DstAuto`.
924                        // Emit an FCW otherwise.
925                        let src_auto: FxHashSet<_> = src_tty
926                            .auto_traits()
927                            .chain(
928                                elaborate::supertrait_def_ids(tcx, src_principal.def_id())
929                                    .filter(|def_id| tcx.trait_is_auto(*def_id)),
930                            )
931                            .collect();
932
933                        let added = dst_tty
934                            .auto_traits()
935                            .filter(|trait_did| !src_auto.contains(trait_did))
936                            .collect::<Vec<_>>();
937
938                        if !added.is_empty() {
939                            tcx.emit_node_span_lint(
940                                lint::builtin::PTR_CAST_ADD_AUTO_TO_OBJECT,
941                                self.expr.hir_id,
942                                self.span,
943                                errors::PtrCastAddAutoToObject {
944                                    traits_len: added.len(),
945                                    traits: {
946                                        let mut traits: Vec<_> = added
947                                            .into_iter()
948                                            .map(|trait_did| tcx.def_path_str(trait_did))
949                                            .collect();
950
951                                        traits.sort();
952                                        traits.into()
953                                    },
954                                },
955                            )
956                        }
957
958                        Ok(CastKind::PtrPtrCast)
959                    }
960
961                    // dyn Auto -> dyn Auto'? ok.
962                    (None, None) => Ok(CastKind::PtrPtrCast),
963
964                    // dyn Trait -> dyn Auto? not ok (for now).
965                    //
966                    // Although dropping the principal is already allowed for unsizing coercions
967                    // (e.g. `*const (dyn Trait + Auto)` to `*const dyn Auto`), dropping it is
968                    // currently **NOT** allowed for (non-coercion) ptr-to-ptr casts (e.g
969                    // `*const Foo` to `*const Bar` where `Foo` has a `dyn Trait + Auto` tail
970                    // and `Bar` has a `dyn Auto` tail), because the underlying MIR operations
971                    // currently work very differently:
972                    //
973                    // * A MIR unsizing coercion on raw pointers to trait objects (`*const dyn Src`
974                    //   to `*const dyn Dst`) is currently equivalent to downcasting the source to
975                    //   the concrete sized type that it was originally unsized from first (via a
976                    //   ptr-to-ptr cast from `*const Src` to `*const T` with `T: Sized`) and then
977                    //   unsizing this thin pointer to the target type (unsizing `*const T` to
978                    //   `*const Dst`). In particular, this means that the pointer's metadata
979                    //   (vtable) will semantically change, e.g. for const eval and miri, even
980                    //   though the vtables will always be merged for codegen.
981                    //
982                    // * A MIR ptr-to-ptr cast is currently equivalent to a transmute and does not
983                    //   change the pointer metadata (vtable) at all.
984                    //
985                    // In addition to this potentially surprising difference between coercion and
986                    // non-coercion casts, casting away the principal with a MIR ptr-to-ptr cast
987                    // is currently considered undefined behavior:
988                    //
989                    // As a validity invariant of pointers to trait objects, we currently require
990                    // that the principal of the vtable in the pointer metadata exactly matches
991                    // the principal of the pointee type, where "no principal" is also considered
992                    // a kind of principal.
993                    (Some(_), None) => Err(CastError::DifferingKinds { src_kind, dst_kind }),
994
995                    // dyn Auto -> dyn Trait? not ok.
996                    (None, Some(_)) => Err(CastError::DifferingKinds { src_kind, dst_kind }),
997                }
998            }
999
1000            // fat -> fat? metadata kinds must match
1001            (src_kind, dst_kind) if src_kind == dst_kind => Ok(CastKind::PtrPtrCast),
1002
1003            (_, _) => Err(CastError::DifferingKinds { src_kind, dst_kind }),
1004        }
1005    }
1006
1007    fn check_fptr_ptr_cast(
1008        &self,
1009        fcx: &FnCtxt<'a, 'tcx>,
1010        m_cast: ty::TypeAndMut<'tcx>,
1011    ) -> Result<CastKind, CastError<'tcx>> {
1012        // fptr-ptr cast. must be to thin ptr
1013
1014        match fcx.pointer_kind(m_cast.ty, self.span)? {
1015            None => Err(CastError::UnknownCastPtrKind),
1016            Some(PointerKind::Thin) => Ok(CastKind::FnPtrPtrCast),
1017            _ => Err(CastError::IllegalCast),
1018        }
1019    }
1020
1021    fn check_ptr_addr_cast(
1022        &self,
1023        fcx: &FnCtxt<'a, 'tcx>,
1024        m_expr: ty::TypeAndMut<'tcx>,
1025    ) -> Result<CastKind, CastError<'tcx>> {
1026        // ptr-addr cast. must be from thin ptr
1027
1028        match fcx.pointer_kind(m_expr.ty, self.span)? {
1029            None => Err(CastError::UnknownExprPtrKind),
1030            Some(PointerKind::Thin) => Ok(CastKind::PtrAddrCast),
1031            _ => Err(CastError::NeedViaThinPtr),
1032        }
1033    }
1034
1035    fn check_ref_cast(
1036        &self,
1037        fcx: &FnCtxt<'a, 'tcx>,
1038        m_expr: ty::TypeAndMut<'tcx>,
1039        m_cast: ty::TypeAndMut<'tcx>,
1040    ) -> Result<CastKind, CastError<'tcx>> {
1041        // array-ptr-cast: allow mut-to-mut, mut-to-const, const-to-const
1042        if m_expr.mutbl >= m_cast.mutbl {
1043            if let ty::Array(ety, _) = m_expr.ty.kind() {
1044                // Due to the limitations of LLVM global constants,
1045                // region pointers end up pointing at copies of
1046                // vector elements instead of the original values.
1047                // To allow raw pointers to work correctly, we
1048                // need to special-case obtaining a raw pointer
1049                // from a region pointer to a vector.
1050
1051                // Coerce to a raw pointer so that we generate RawPtr in MIR.
1052                let array_ptr_type = Ty::new_ptr(fcx.tcx, m_expr.ty, m_expr.mutbl);
1053                fcx.coerce(self.expr, self.expr_ty, array_ptr_type, AllowTwoPhase::No, None)
1054                    .unwrap_or_else(|_| {
1055                        bug!(
1056                        "could not cast from reference to array to pointer to array ({:?} to {:?})",
1057                        self.expr_ty,
1058                        array_ptr_type,
1059                    )
1060                    });
1061
1062                // this will report a type mismatch if needed
1063                fcx.demand_eqtype(self.span, *ety, m_cast.ty);
1064                return Ok(CastKind::ArrayPtrCast);
1065            }
1066        }
1067
1068        Err(CastError::IllegalCast)
1069    }
1070
1071    fn check_addr_ptr_cast(
1072        &self,
1073        fcx: &FnCtxt<'a, 'tcx>,
1074        m_cast: TypeAndMut<'tcx>,
1075    ) -> Result<CastKind, CastError<'tcx>> {
1076        // ptr-addr cast. pointer must be thin.
1077        match fcx.pointer_kind(m_cast.ty, self.span)? {
1078            None => Err(CastError::UnknownCastPtrKind),
1079            Some(PointerKind::Thin) => Ok(CastKind::AddrPtrCast),
1080            Some(PointerKind::VTable(_)) => Err(CastError::IntToWideCast(Some("a vtable"))),
1081            Some(PointerKind::Length) => Err(CastError::IntToWideCast(Some("a length"))),
1082            Some(PointerKind::OfAlias(_) | PointerKind::OfParam(_)) => {
1083                Err(CastError::IntToWideCast(None))
1084            }
1085        }
1086    }
1087
1088    fn try_coercion_cast(&self, fcx: &FnCtxt<'a, 'tcx>) -> Result<(), ty::error::TypeError<'tcx>> {
1089        match fcx.coerce(self.expr, self.expr_ty, self.cast_ty, AllowTwoPhase::No, None) {
1090            Ok(_) => Ok(()),
1091            Err(err) => Err(err),
1092        }
1093    }
1094
1095    fn err_if_cenum_impl_drop(&self, fcx: &FnCtxt<'a, 'tcx>) {
1096        if let ty::Adt(d, _) = self.expr_ty.kind()
1097            && d.has_dtor(fcx.tcx)
1098        {
1099            let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
1100            let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
1101
1102            fcx.dcx().emit_err(errors::CastEnumDrop { span: self.span, expr_ty, cast_ty });
1103        }
1104    }
1105
1106    fn lossy_provenance_ptr2int_lint(&self, fcx: &FnCtxt<'a, 'tcx>, t_c: ty::cast::IntTy) {
1107        let expr_prec = self.expr.precedence();
1108        let needs_parens = expr_prec < ExprPrecedence::Unambiguous;
1109
1110        let needs_cast = !matches!(t_c, ty::cast::IntTy::U(ty::UintTy::Usize));
1111        let cast_span = self.expr_span.shrink_to_hi().to(self.cast_span);
1112        let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
1113        let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
1114        let expr_span = self.expr_span.shrink_to_lo();
1115        let sugg = match (needs_parens, needs_cast) {
1116            (true, true) => errors::LossyProvenancePtr2IntSuggestion::NeedsParensCast {
1117                expr_span,
1118                cast_span,
1119                cast_ty,
1120            },
1121            (true, false) => {
1122                errors::LossyProvenancePtr2IntSuggestion::NeedsParens { expr_span, cast_span }
1123            }
1124            (false, true) => {
1125                errors::LossyProvenancePtr2IntSuggestion::NeedsCast { cast_span, cast_ty }
1126            }
1127            (false, false) => errors::LossyProvenancePtr2IntSuggestion::Other { cast_span },
1128        };
1129
1130        let lint = errors::LossyProvenancePtr2Int { expr_ty, cast_ty, sugg };
1131        fcx.tcx.emit_node_span_lint(
1132            lint::builtin::LOSSY_PROVENANCE_CASTS,
1133            self.expr.hir_id,
1134            self.span,
1135            lint,
1136        );
1137    }
1138
1139    fn fuzzy_provenance_int2ptr_lint(&self, fcx: &FnCtxt<'a, 'tcx>) {
1140        let sugg = errors::LossyProvenanceInt2PtrSuggestion {
1141            lo: self.expr_span.shrink_to_lo(),
1142            hi: self.expr_span.shrink_to_hi().to(self.cast_span),
1143        };
1144        let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
1145        let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
1146        let lint = errors::LossyProvenanceInt2Ptr { expr_ty, cast_ty, sugg };
1147        fcx.tcx.emit_node_span_lint(
1148            lint::builtin::FUZZY_PROVENANCE_CASTS,
1149            self.expr.hir_id,
1150            self.span,
1151            lint,
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| 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(errors::DerefImplsIsEmpty {
1168                        span: self.expr_span,
1169                        deref_ty: fcx.ty_to_string(deref_ty),
1170                    });
1171                }
1172
1173                // Create a multipart suggestion: add `!` and `.is_empty()` in
1174                // place of the cast.
1175                err.subdiagnostic(errors::UseIsEmpty {
1176                    lo: self.expr_span.shrink_to_lo(),
1177                    hi: self.span.with_lo(self.expr_span.hi()),
1178                    expr_ty: fcx.ty_to_string(self.expr_ty),
1179                });
1180            }
1181        }
1182    }
1183}