1use rustc_ast::util::parser::ExprPrecedence;
32use rustc_data_structures::fx::FxHashSet;
33use rustc_errors::codes::*;
34use rustc_errors::{Applicability, Diag, ErrorGuaranteed};
35use rustc_hir::def_id::DefId;
36use rustc_hir::{self as hir, ExprKind};
37use rustc_infer::infer::DefineOpaqueTypes;
38use rustc_macros::{TypeFoldable, TypeVisitable};
39use rustc_middle::mir::Mutability;
40use rustc_middle::ty::adjustment::AllowTwoPhase;
41use rustc_middle::ty::cast::{CastKind, CastTy};
42use rustc_middle::ty::error::TypeError;
43use rustc_middle::ty::{self, Ty, TyCtxt, TypeAndMut, TypeVisitableExt, VariantDef, elaborate};
44use rustc_middle::{bug, span_bug};
45use rustc_session::lint;
46use rustc_span::{DUMMY_SP, Span, sym};
47use rustc_trait_selection::infer::InferCtxtExt;
48use tracing::{debug, instrument};
49
50use super::FnCtxt;
51use crate::{errors, type_error_struct};
52
53#[derive(Debug)]
56pub(crate) struct CastCheck<'tcx> {
57 expr: &'tcx hir::Expr<'tcx>,
59 expr_ty: Ty<'tcx>,
61 expr_span: Span,
62 cast_ty: Ty<'tcx>,
64 cast_span: Span,
65 span: Span,
66}
67
68#[derive(Debug, Copy, Clone, PartialEq, Eq, TypeVisitable, TypeFoldable)]
72enum PointerKind<'tcx> {
73 Thin,
75 VTable(&'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>),
77 Length,
79 OfAlias(ty::AliasTy<'tcx>),
81 OfParam(ty::ParamTy),
83}
84
85impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
86 fn pointer_kind(
89 &self,
90 t: Ty<'tcx>,
91 span: Span,
92 ) -> Result<Option<PointerKind<'tcx>>, ErrorGuaranteed> {
93 debug!("pointer_kind({:?}, {:?})", t, span);
94
95 let t = self.resolve_vars_if_possible(t);
96 t.error_reported()?;
97
98 if self.type_is_sized_modulo_regions(self.param_env, t) {
99 return Ok(Some(PointerKind::Thin));
100 }
101
102 let t = self.try_structurally_resolve_type(span, t);
103
104 Ok(match *t.kind() {
105 ty::Slice(_) | ty::Str => Some(PointerKind::Length),
106 ty::Dynamic(tty, _, ty::Dyn) => Some(PointerKind::VTable(tty)),
107 ty::Adt(def, args) if def.is_struct() => match def.non_enum_variant().tail_opt() {
108 None => Some(PointerKind::Thin),
109 Some(f) => {
110 let field_ty = self.field_ty(span, f, args);
111 self.pointer_kind(field_ty, span)?
112 }
113 },
114 ty::Tuple(fields) => match fields.last() {
115 None => Some(PointerKind::Thin),
116 Some(&f) => self.pointer_kind(f, span)?,
117 },
118
119 ty::UnsafeBinder(_) => todo!("FIXME(unsafe_binder)"),
120
121 ty::Foreign(..) => Some(PointerKind::Thin),
123 ty::Alias(_, pi) => Some(PointerKind::OfAlias(pi)),
125 ty::Param(p) => Some(PointerKind::OfParam(p)),
126 ty::Placeholder(..) | ty::Bound(..) | ty::Infer(_) => None,
128
129 ty::Bool
130 | ty::Char
131 | ty::Int(..)
132 | ty::Uint(..)
133 | ty::Float(_)
134 | ty::Array(..)
135 | ty::CoroutineWitness(..)
136 | ty::RawPtr(_, _)
137 | ty::Ref(..)
138 | ty::Pat(..)
139 | ty::FnDef(..)
140 | ty::FnPtr(..)
141 | ty::Closure(..)
142 | ty::CoroutineClosure(..)
143 | ty::Coroutine(..)
144 | ty::Adt(..)
145 | ty::Never
146 | ty::Dynamic(_, _, ty::DynStar)
147 | ty::Error(_) => {
148 let guar = self
149 .dcx()
150 .span_delayed_bug(span, format!("`{t:?}` should be sized but is not?"));
151 return Err(guar);
152 }
153 })
154 }
155}
156
157#[derive(Debug)]
158enum CastError<'tcx> {
159 ErrorGuaranteed(ErrorGuaranteed),
160
161 CastToBool,
162 CastToChar,
163 DifferingKinds {
164 src_kind: PointerKind<'tcx>,
165 dst_kind: PointerKind<'tcx>,
166 },
167 SizedUnsizedCast,
169 IllegalCast,
170 NeedDeref,
171 NeedViaPtr,
172 NeedViaThinPtr,
173 NeedViaInt,
174 NonScalar,
175 UnknownExprPtrKind,
176 UnknownCastPtrKind,
177 IntToWideCast(Option<&'static str>),
183 ForeignNonExhaustiveAdt,
184 PtrPtrAddingAutoTrait(Vec<DefId>),
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
210pub 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 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 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 }
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 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 err.multipart_suggestion(
384 "consider using `char::from_u32` instead",
385 vec![
386 (self.expr_span.shrink_to_lo(), "char::from_u32(".to_string()),
387 (self.expr_span.shrink_to_hi().to(self.cast_span), ")".to_string()),
388 ],
389 Applicability::MachineApplicable,
390 );
391 } else if self.expr_ty == fcx.tcx.types.i8 {
392 err.span_help(self.span, "consider casting from `u8` instead");
393 } else {
394 err.span_help(
395 self.span,
396 "consider using `char::from_u32` instead (via a `u32`)",
397 );
398 };
399 }
400 err.emit();
401 }
402 CastError::NonScalar => {
403 let mut err = type_error_struct!(
404 fcx.dcx(),
405 self.span,
406 self.expr_ty,
407 E0605,
408 "non-primitive cast: `{}` as `{}`",
409 self.expr_ty,
410 fcx.ty_to_string(self.cast_ty)
411 );
412
413 if let Ok(snippet) = fcx.tcx.sess.source_map().span_to_snippet(self.expr_span)
414 && matches!(self.expr.kind, ExprKind::AddrOf(..))
415 {
416 err.note(format!(
417 "casting reference expression `{}` because `&` binds tighter than `as`",
418 snippet
419 ));
420 }
421
422 let mut sugg = None;
423 let mut sugg_mutref = false;
424 if let ty::Ref(reg, cast_ty, mutbl) = *self.cast_ty.kind() {
425 if let ty::RawPtr(expr_ty, _) = *self.expr_ty.kind()
426 && fcx.may_coerce(
427 Ty::new_ref(fcx.tcx, fcx.tcx.lifetimes.re_erased, expr_ty, mutbl),
428 self.cast_ty,
429 )
430 {
431 sugg = Some((format!("&{}*", mutbl.prefix_str()), cast_ty == expr_ty));
432 } else if let ty::Ref(expr_reg, expr_ty, expr_mutbl) = *self.expr_ty.kind()
433 && expr_mutbl == Mutability::Not
434 && mutbl == Mutability::Mut
435 && fcx.may_coerce(Ty::new_mut_ref(fcx.tcx, expr_reg, expr_ty), self.cast_ty)
436 {
437 sugg_mutref = true;
438 }
439
440 if !sugg_mutref
441 && sugg == None
442 && fcx.may_coerce(
443 Ty::new_ref(fcx.tcx, reg, self.expr_ty, mutbl),
444 self.cast_ty,
445 )
446 {
447 sugg = Some((format!("&{}", mutbl.prefix_str()), false));
448 }
449 } else if let ty::RawPtr(_, mutbl) = *self.cast_ty.kind()
450 && fcx.may_coerce(
451 Ty::new_ref(fcx.tcx, fcx.tcx.lifetimes.re_erased, self.expr_ty, mutbl),
452 self.cast_ty,
453 )
454 {
455 sugg = Some((format!("&{}", mutbl.prefix_str()), false));
456 }
457 if sugg_mutref {
458 err.span_label(self.span, "invalid cast");
459 err.span_note(self.expr_span, "this reference is immutable");
460 err.span_note(self.cast_span, "trying to cast to a mutable reference type");
461 } else if let Some((sugg, remove_cast)) = sugg {
462 err.span_label(self.span, "invalid cast");
463
464 let has_parens = fcx
465 .tcx
466 .sess
467 .source_map()
468 .span_to_snippet(self.expr_span)
469 .is_ok_and(|snip| snip.starts_with('('));
470
471 let needs_parens =
475 !has_parens && matches!(self.expr.kind, hir::ExprKind::Cast(..));
476
477 let mut suggestion = vec![(self.expr_span.shrink_to_lo(), sugg)];
478 if needs_parens {
479 suggestion[0].1 += "(";
480 suggestion.push((self.expr_span.shrink_to_hi(), ")".to_string()));
481 }
482 if remove_cast {
483 suggestion.push((
484 self.expr_span.shrink_to_hi().to(self.cast_span),
485 String::new(),
486 ));
487 }
488
489 err.multipart_suggestion_verbose(
490 "consider borrowing the value",
491 suggestion,
492 Applicability::MachineApplicable,
493 );
494 } else if !matches!(
495 self.cast_ty.kind(),
496 ty::FnDef(..) | ty::FnPtr(..) | ty::Closure(..)
497 ) {
498 if let Some(from_trait) = fcx.tcx.get_diagnostic_item(sym::From) {
500 let ty = fcx.resolve_vars_if_possible(self.cast_ty);
501 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
502 if fcx
503 .infcx
504 .type_implements_trait(from_trait, [ty, expr_ty], fcx.param_env)
505 .must_apply_modulo_regions()
506 {
507 let to_ty = if let ty::Adt(def, args) = self.cast_ty.kind() {
508 fcx.tcx.value_path_str_with_args(def.did(), args)
509 } else {
510 self.cast_ty.to_string()
511 };
512 err.multipart_suggestion(
513 "consider using the `From` trait instead",
514 vec![
515 (self.expr_span.shrink_to_lo(), format!("{to_ty}::from(")),
516 (
517 self.expr_span.shrink_to_hi().to(self.cast_span),
518 ")".to_string(),
519 ),
520 ],
521 Applicability::MaybeIncorrect,
522 );
523 }
524 }
525
526 let (msg, note) = if let ty::Adt(adt, _) = self.expr_ty.kind()
527 && adt.is_enum()
528 && self.cast_ty.is_numeric()
529 {
530 (
531 "an `as` expression can be used to convert enum types to numeric \
532 types only if the enum type is unit-only or field-less",
533 Some(
534 "see https://doc.rust-lang.org/reference/items/enumerations.html#casting for more information",
535 ),
536 )
537 } else {
538 (
539 "an `as` expression can only be used to convert between primitive \
540 types or to coerce to a specific trait object",
541 None,
542 )
543 };
544
545 err.span_label(self.span, msg);
546
547 if let Some(note) = note {
548 err.note(note);
549 }
550 } else {
551 err.span_label(self.span, "invalid cast");
552 }
553
554 fcx.suggest_no_capture_closure(&mut err, self.cast_ty, self.expr_ty);
555 self.try_suggest_collection_to_bool(fcx, &mut err);
556
557 err.emit();
558 }
559 CastError::SizedUnsizedCast => {
560 let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
561 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
562 fcx.dcx().emit_err(errors::CastThinPointerToWidePointer {
563 span: self.span,
564 expr_ty,
565 cast_ty,
566 teach: fcx.tcx.sess.teach(E0607),
567 });
568 }
569 CastError::IntToWideCast(known_metadata) => {
570 let expr_if_nightly = fcx.tcx.sess.is_nightly_build().then_some(self.expr_span);
571 let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
572 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
573 let metadata = known_metadata.unwrap_or("type-specific metadata");
574 let known_wide = known_metadata.is_some();
575 let span = self.cast_span;
576 fcx.dcx().emit_err(errors::IntToWide {
577 span,
578 metadata,
579 expr_ty,
580 cast_ty,
581 expr_if_nightly,
582 known_wide,
583 });
584 }
585 CastError::UnknownCastPtrKind | CastError::UnknownExprPtrKind => {
586 let unknown_cast_to = match e {
587 CastError::UnknownCastPtrKind => true,
588 CastError::UnknownExprPtrKind => false,
589 e => unreachable!("control flow means we should never encounter a {e:?}"),
590 };
591 let (span, sub) = if unknown_cast_to {
592 (self.cast_span, errors::CastUnknownPointerSub::To(self.cast_span))
593 } else {
594 (self.cast_span, errors::CastUnknownPointerSub::From(self.span))
595 };
596 fcx.dcx().emit_err(errors::CastUnknownPointer { span, to: unknown_cast_to, sub });
597 }
598 CastError::ForeignNonExhaustiveAdt => {
599 make_invalid_casting_error(
600 self.span,
601 self.expr_ty,
602 self.cast_ty,
603 fcx,
604 )
605 .with_note("cannot cast an enum with a non-exhaustive variant when it's defined in another crate")
606 .emit();
607 }
608 CastError::PtrPtrAddingAutoTrait(added) => {
609 fcx.dcx().emit_err(errors::PtrCastAddAutoToObject {
610 span: self.span,
611 traits_len: added.len(),
612 traits: {
613 let mut traits: Vec<_> = added
614 .into_iter()
615 .map(|trait_did| fcx.tcx.def_path_str(trait_did))
616 .collect();
617
618 traits.sort();
619 traits.into()
620 },
621 });
622 }
623 }
624 }
625
626 fn report_cast_to_unsized_type(&self, fcx: &FnCtxt<'a, 'tcx>) -> ErrorGuaranteed {
627 if let Err(err) = self.cast_ty.error_reported() {
628 return err;
629 }
630 if let Err(err) = self.expr_ty.error_reported() {
631 return err;
632 }
633
634 let tstr = fcx.ty_to_string(self.cast_ty);
635 let mut err = type_error_struct!(
636 fcx.dcx(),
637 self.span,
638 self.expr_ty,
639 E0620,
640 "cast to unsized type: `{}` as `{}`",
641 fcx.resolve_vars_if_possible(self.expr_ty),
642 tstr
643 );
644 match self.expr_ty.kind() {
645 ty::Ref(_, _, mt) => {
646 let mtstr = mt.prefix_str();
647 err.span_suggestion_verbose(
648 self.cast_span.shrink_to_lo(),
649 "consider casting to a reference instead",
650 format!("&{mtstr}"),
651 Applicability::MachineApplicable,
652 );
653 }
654 ty::Adt(def, ..) if def.is_box() => {
655 err.multipart_suggestion(
656 "you can cast to a `Box` instead",
657 vec![
658 (self.cast_span.shrink_to_lo(), "Box<".to_string()),
659 (self.cast_span.shrink_to_hi(), ">".to_string()),
660 ],
661 Applicability::MachineApplicable,
662 );
663 }
664 _ => {
665 err.span_help(self.expr_span, "consider using a box or reference as appropriate");
666 }
667 }
668 err.emit()
669 }
670
671 fn trivial_cast_lint(&self, fcx: &FnCtxt<'a, 'tcx>) {
672 let (numeric, lint) = if self.cast_ty.is_numeric() && self.expr_ty.is_numeric() {
673 (true, lint::builtin::TRIVIAL_NUMERIC_CASTS)
674 } else {
675 (false, lint::builtin::TRIVIAL_CASTS)
676 };
677 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
678 let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
679 fcx.tcx.emit_node_span_lint(
680 lint,
681 self.expr.hir_id,
682 self.span,
683 errors::TrivialCast { numeric, expr_ty, cast_ty },
684 );
685 }
686
687 #[instrument(skip(fcx), level = "debug")]
688 pub(crate) fn check(mut self, fcx: &FnCtxt<'a, 'tcx>) {
689 self.expr_ty = fcx.structurally_resolve_type(self.expr_span, self.expr_ty);
690 self.cast_ty = fcx.structurally_resolve_type(self.cast_span, self.cast_ty);
691
692 debug!("check_cast({}, {:?} as {:?})", self.expr.hir_id, self.expr_ty, self.cast_ty);
693
694 if !fcx.type_is_sized_modulo_regions(fcx.param_env, self.cast_ty)
695 && !self.cast_ty.has_infer_types()
696 {
697 self.report_cast_to_unsized_type(fcx);
698 } else if self.expr_ty.references_error() || self.cast_ty.references_error() {
699 } else {
701 match self.try_coercion_cast(fcx) {
702 Ok(()) => {
703 if self.expr_ty.is_raw_ptr() && self.cast_ty.is_raw_ptr() {
704 debug!(" -> PointerCast");
711 } else {
712 self.trivial_cast_lint(fcx);
713 debug!(" -> CoercionCast");
714 fcx.typeck_results
715 .borrow_mut()
716 .set_coercion_cast(self.expr.hir_id.local_id);
717 }
718 }
719 Err(_) => {
720 match self.do_check(fcx) {
721 Ok(k) => {
722 debug!(" -> {:?}", k);
723 }
724 Err(e) => self.report_cast_error(fcx, e),
725 };
726 }
727 };
728 }
729 }
730 fn do_check(&self, fcx: &FnCtxt<'a, 'tcx>) -> Result<CastKind, CastError<'tcx>> {
734 use rustc_middle::ty::cast::CastTy::*;
735 use rustc_middle::ty::cast::IntTy::*;
736
737 if self.cast_ty.is_dyn_star() {
738 return Err(CastError::IllegalCast);
743 }
744
745 let (t_from, t_cast) = match (CastTy::from_ty(self.expr_ty), CastTy::from_ty(self.cast_ty))
746 {
747 (Some(t_from), Some(t_cast)) => (t_from, t_cast),
748 (None, Some(t_cast)) => {
750 match *self.expr_ty.kind() {
751 ty::FnDef(..) => {
752 let f = fcx.normalize(self.expr_span, self.expr_ty.fn_sig(fcx.tcx));
754 let res = fcx.coerce(
755 self.expr,
756 self.expr_ty,
757 Ty::new_fn_ptr(fcx.tcx, f),
758 AllowTwoPhase::No,
759 None,
760 );
761 if let Err(TypeError::IntrinsicCast) = res {
762 return Err(CastError::IllegalCast);
763 }
764 if res.is_err() {
765 return Err(CastError::NonScalar);
766 }
767 (FnPtr, t_cast)
768 }
769 ty::Ref(_, inner_ty, mutbl) => {
774 return match t_cast {
775 Int(_) | Float => match *inner_ty.kind() {
776 ty::Int(_)
777 | ty::Uint(_)
778 | ty::Float(_)
779 | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(_)) => {
780 Err(CastError::NeedDeref)
781 }
782 _ => Err(CastError::NeedViaPtr),
783 },
784 Ptr(mt) => {
786 if !fcx.type_is_sized_modulo_regions(fcx.param_env, mt.ty) {
787 return Err(CastError::IllegalCast);
788 }
789 self.check_ref_cast(fcx, TypeAndMut { mutbl, ty: inner_ty }, mt)
790 }
791 _ => Err(CastError::NonScalar),
792 };
793 }
794 _ => return Err(CastError::NonScalar),
795 }
796 }
797 _ => return Err(CastError::NonScalar),
798 };
799 if let ty::Adt(adt_def, _) = *self.expr_ty.kind()
800 && !adt_def.did().is_local()
801 && adt_def.variants().iter().any(VariantDef::is_field_list_non_exhaustive)
802 {
803 return Err(CastError::ForeignNonExhaustiveAdt);
804 }
805 match (t_from, t_cast) {
806 (_, Int(CEnum) | FnPtr) => Err(CastError::NonScalar),
808
809 (_, Int(Bool)) => Err(CastError::CastToBool),
811
812 (Int(U(ty::UintTy::U8)), Int(Char)) => Ok(CastKind::U8CharCast), (_, Int(Char)) => Err(CastError::CastToChar),
815
816 (Int(Bool) | Int(CEnum) | Int(Char), Float) => Err(CastError::NeedViaInt),
818
819 (Int(Bool) | Int(CEnum) | Int(Char) | Float, Ptr(_)) | (Ptr(_) | FnPtr, Float) => {
820 Err(CastError::IllegalCast)
821 }
822
823 (Ptr(m_e), Ptr(m_c)) => self.check_ptr_ptr_cast(fcx, m_e, m_c), (Ptr(m_expr), Int(t_c)) => {
828 self.lossy_provenance_ptr2int_lint(fcx, t_c);
829 self.check_ptr_addr_cast(fcx, m_expr)
830 }
831 (FnPtr, Int(_)) => {
832 Ok(CastKind::FnPtrAddrCast)
834 }
835 (Int(_), Ptr(mt)) => {
837 self.fuzzy_provenance_int2ptr_lint(fcx);
838 self.check_addr_ptr_cast(fcx, mt)
839 }
840 (FnPtr, Ptr(mt)) => self.check_fptr_ptr_cast(fcx, mt),
842
843 (Int(CEnum), Int(_)) => {
845 self.err_if_cenum_impl_drop(fcx);
846 Ok(CastKind::EnumCast)
847 }
848 (Int(Char) | Int(Bool), Int(_)) => Ok(CastKind::PrimIntCast),
849
850 (Int(_) | Float, Int(_) | Float) => Ok(CastKind::NumericCast),
851 }
852 }
853
854 fn check_ptr_ptr_cast(
855 &self,
856 fcx: &FnCtxt<'a, 'tcx>,
857 m_src: ty::TypeAndMut<'tcx>,
858 m_dst: ty::TypeAndMut<'tcx>,
859 ) -> Result<CastKind, CastError<'tcx>> {
860 debug!("check_ptr_ptr_cast m_src={m_src:?} m_dst={m_dst:?}");
861 let src_kind = fcx.tcx.erase_regions(fcx.pointer_kind(m_src.ty, self.span)?);
864 let dst_kind = fcx.tcx.erase_regions(fcx.pointer_kind(m_dst.ty, self.span)?);
865
866 let Some(dst_kind) = dst_kind else {
868 return Err(CastError::UnknownCastPtrKind);
869 };
870
871 if dst_kind == PointerKind::Thin {
873 return Ok(CastKind::PtrPtrCast);
874 }
875
876 let Some(src_kind) = src_kind else {
878 return Err(CastError::UnknownCastPtrKind);
879 };
880
881 match (src_kind, dst_kind) {
882 (PointerKind::Thin, _) => Err(CastError::SizedUnsizedCast),
884
885 (PointerKind::VTable(src_tty), PointerKind::VTable(dst_tty)) => {
887 match (src_tty.principal(), dst_tty.principal()) {
888 (Some(src_principal), Some(_)) => {
897 let tcx = fcx.tcx;
898
899 let src_obj = Ty::new_dynamic(
907 tcx,
908 tcx.mk_poly_existential_predicates(
909 &src_tty.without_auto_traits().collect::<Vec<_>>(),
910 ),
911 tcx.lifetimes.re_erased,
912 ty::Dyn,
913 );
914 let dst_obj = Ty::new_dynamic(
915 tcx,
916 tcx.mk_poly_existential_predicates(
917 &dst_tty.without_auto_traits().collect::<Vec<_>>(),
918 ),
919 tcx.lifetimes.re_erased,
920 ty::Dyn,
921 );
922
923 let cause = fcx.misc(self.span);
926 if fcx
927 .at(&cause, fcx.param_env)
928 .eq(DefineOpaqueTypes::Yes, src_obj, dst_obj)
929 .map(|infer_ok| fcx.register_infer_ok_obligations(infer_ok))
930 .is_err()
931 {
932 return Err(CastError::DifferingKinds { src_kind, dst_kind });
933 }
934
935 let src_auto: FxHashSet<_> = src_tty
938 .auto_traits()
939 .chain(
940 elaborate::supertrait_def_ids(tcx, src_principal.def_id())
941 .filter(|def_id| tcx.trait_is_auto(*def_id)),
942 )
943 .collect();
944
945 let added = dst_tty
946 .auto_traits()
947 .filter(|trait_did| !src_auto.contains(trait_did))
948 .collect::<Vec<_>>();
949
950 if !added.is_empty() {
951 return Err(CastError::PtrPtrAddingAutoTrait(added));
952 }
953
954 Ok(CastKind::PtrPtrCast)
955 }
956
957 (None, None) => Ok(CastKind::PtrPtrCast),
959
960 (Some(_), None) => Err(CastError::DifferingKinds { src_kind, dst_kind }),
990
991 (None, Some(_)) => Err(CastError::DifferingKinds { src_kind, dst_kind }),
993 }
994 }
995
996 (src_kind, dst_kind) if src_kind == dst_kind => Ok(CastKind::PtrPtrCast),
998
999 (_, _) => Err(CastError::DifferingKinds { src_kind, dst_kind }),
1000 }
1001 }
1002
1003 fn check_fptr_ptr_cast(
1004 &self,
1005 fcx: &FnCtxt<'a, 'tcx>,
1006 m_cast: ty::TypeAndMut<'tcx>,
1007 ) -> Result<CastKind, CastError<'tcx>> {
1008 match fcx.pointer_kind(m_cast.ty, self.span)? {
1011 None => Err(CastError::UnknownCastPtrKind),
1012 Some(PointerKind::Thin) => Ok(CastKind::FnPtrPtrCast),
1013 _ => Err(CastError::IllegalCast),
1014 }
1015 }
1016
1017 fn check_ptr_addr_cast(
1018 &self,
1019 fcx: &FnCtxt<'a, 'tcx>,
1020 m_expr: ty::TypeAndMut<'tcx>,
1021 ) -> Result<CastKind, CastError<'tcx>> {
1022 match fcx.pointer_kind(m_expr.ty, self.span)? {
1025 None => Err(CastError::UnknownExprPtrKind),
1026 Some(PointerKind::Thin) => Ok(CastKind::PtrAddrCast),
1027 _ => Err(CastError::NeedViaThinPtr),
1028 }
1029 }
1030
1031 fn check_ref_cast(
1032 &self,
1033 fcx: &FnCtxt<'a, 'tcx>,
1034 mut m_expr: ty::TypeAndMut<'tcx>,
1035 mut m_cast: ty::TypeAndMut<'tcx>,
1036 ) -> Result<CastKind, CastError<'tcx>> {
1037 m_expr.ty = fcx.try_structurally_resolve_type(self.expr_span, m_expr.ty);
1039 m_cast.ty = fcx.try_structurally_resolve_type(self.cast_span, m_cast.ty);
1040
1041 if m_expr.mutbl >= m_cast.mutbl
1042 && let ty::Array(ety, _) = m_expr.ty.kind()
1043 && fcx.can_eq(fcx.param_env, *ety, m_cast.ty)
1044 {
1045 let array_ptr_type = Ty::new_ptr(fcx.tcx, m_expr.ty, m_expr.mutbl);
1050 fcx.coerce(self.expr, self.expr_ty, array_ptr_type, AllowTwoPhase::No, None)
1051 .unwrap_or_else(|_| {
1052 bug!(
1053 "could not cast from reference to array to pointer to array ({:?} to {:?})",
1054 self.expr_ty,
1055 array_ptr_type,
1056 )
1057 });
1058
1059 fcx.demand_eqtype(self.span, *ety, m_cast.ty);
1061 return Ok(CastKind::ArrayPtrCast);
1062 }
1063
1064 Err(CastError::IllegalCast)
1065 }
1066
1067 fn check_addr_ptr_cast(
1068 &self,
1069 fcx: &FnCtxt<'a, 'tcx>,
1070 m_cast: TypeAndMut<'tcx>,
1071 ) -> Result<CastKind, CastError<'tcx>> {
1072 match fcx.pointer_kind(m_cast.ty, self.span)? {
1074 None => Err(CastError::UnknownCastPtrKind),
1075 Some(PointerKind::Thin) => Ok(CastKind::AddrPtrCast),
1076 Some(PointerKind::VTable(_)) => Err(CastError::IntToWideCast(Some("a vtable"))),
1077 Some(PointerKind::Length) => Err(CastError::IntToWideCast(Some("a length"))),
1078 Some(PointerKind::OfAlias(_) | PointerKind::OfParam(_)) => {
1079 Err(CastError::IntToWideCast(None))
1080 }
1081 }
1082 }
1083
1084 fn try_coercion_cast(&self, fcx: &FnCtxt<'a, 'tcx>) -> Result<(), ty::error::TypeError<'tcx>> {
1085 match fcx.coerce(self.expr, self.expr_ty, self.cast_ty, AllowTwoPhase::No, None) {
1086 Ok(_) => Ok(()),
1087 Err(err) => Err(err),
1088 }
1089 }
1090
1091 fn err_if_cenum_impl_drop(&self, fcx: &FnCtxt<'a, 'tcx>) {
1092 if let ty::Adt(d, _) = self.expr_ty.kind()
1093 && d.has_dtor(fcx.tcx)
1094 {
1095 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
1096 let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
1097
1098 fcx.dcx().emit_err(errors::CastEnumDrop { span: self.span, expr_ty, cast_ty });
1099 }
1100 }
1101
1102 fn lossy_provenance_ptr2int_lint(&self, fcx: &FnCtxt<'a, 'tcx>, t_c: ty::cast::IntTy) {
1103 let expr_prec = fcx.precedence(self.expr);
1104 let needs_parens = expr_prec < ExprPrecedence::Unambiguous;
1105
1106 let needs_cast = !matches!(t_c, ty::cast::IntTy::U(ty::UintTy::Usize));
1107 let cast_span = self.expr_span.shrink_to_hi().to(self.cast_span);
1108 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
1109 let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
1110 let expr_span = self.expr_span.shrink_to_lo();
1111 let sugg = match (needs_parens, needs_cast) {
1112 (true, true) => errors::LossyProvenancePtr2IntSuggestion::NeedsParensCast {
1113 expr_span,
1114 cast_span,
1115 cast_ty,
1116 },
1117 (true, false) => {
1118 errors::LossyProvenancePtr2IntSuggestion::NeedsParens { expr_span, cast_span }
1119 }
1120 (false, true) => {
1121 errors::LossyProvenancePtr2IntSuggestion::NeedsCast { cast_span, cast_ty }
1122 }
1123 (false, false) => errors::LossyProvenancePtr2IntSuggestion::Other { cast_span },
1124 };
1125
1126 let lint = errors::LossyProvenancePtr2Int { expr_ty, cast_ty, sugg };
1127 fcx.tcx.emit_node_span_lint(
1128 lint::builtin::LOSSY_PROVENANCE_CASTS,
1129 self.expr.hir_id,
1130 self.span,
1131 lint,
1132 );
1133 }
1134
1135 fn fuzzy_provenance_int2ptr_lint(&self, fcx: &FnCtxt<'a, 'tcx>) {
1136 let sugg = errors::LossyProvenanceInt2PtrSuggestion {
1137 lo: self.expr_span.shrink_to_lo(),
1138 hi: self.expr_span.shrink_to_hi().to(self.cast_span),
1139 };
1140 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
1141 let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
1142 let lint = errors::LossyProvenanceInt2Ptr { expr_ty, cast_ty, sugg };
1143 fcx.tcx.emit_node_span_lint(
1144 lint::builtin::FUZZY_PROVENANCE_CASTS,
1145 self.expr.hir_id,
1146 self.span,
1147 lint,
1148 );
1149 }
1150
1151 fn try_suggest_collection_to_bool(&self, fcx: &FnCtxt<'a, 'tcx>, err: &mut Diag<'_>) {
1154 if self.cast_ty.is_bool() {
1155 let derefed = fcx
1156 .autoderef(self.expr_span, self.expr_ty)
1157 .silence_errors()
1158 .find(|t| matches!(t.0.kind(), ty::Str | ty::Slice(..)));
1159
1160 if let Some((deref_ty, _)) = derefed {
1161 if deref_ty != self.expr_ty.peel_refs() {
1163 err.subdiagnostic(errors::DerefImplsIsEmpty { span: self.expr_span, deref_ty });
1164 }
1165
1166 err.subdiagnostic(errors::UseIsEmpty {
1169 lo: self.expr_span.shrink_to_lo(),
1170 hi: self.span.with_lo(self.expr_span.hi()),
1171 expr_ty: self.expr_ty,
1172 });
1173 }
1174 }
1175 }
1176}