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, _) => 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::Error(_) => {
147 let guar = self
148 .dcx()
149 .span_delayed_bug(span, format!("`{t:?}` should be sized but is not?"));
150 return Err(guar);
151 }
152 })
153 }
154}
155
156#[derive(Debug)]
157enum CastError<'tcx> {
158 ErrorGuaranteed(ErrorGuaranteed),
159
160 CastToBool,
161 CastToChar,
162 DifferingKinds {
163 src_kind: PointerKind<'tcx>,
164 dst_kind: PointerKind<'tcx>,
165 },
166 SizedUnsizedCast,
168 IllegalCast,
169 NeedDeref,
170 NeedViaPtr,
171 NeedViaThinPtr,
172 NeedViaInt,
173 NonScalar,
174 UnknownExprPtrKind,
175 UnknownCastPtrKind,
176 IntToWideCast(Option<&'static str>),
182 ForeignNonExhaustiveAdt,
183 PtrPtrAddingAutoTrait(Vec<DefId>),
184}
185
186impl From<ErrorGuaranteed> for CastError<'_> {
187 fn from(err: ErrorGuaranteed) -> Self {
188 CastError::ErrorGuaranteed(err)
189 }
190}
191
192fn make_invalid_casting_error<'a, 'tcx>(
193 span: Span,
194 expr_ty: Ty<'tcx>,
195 cast_ty: Ty<'tcx>,
196 fcx: &FnCtxt<'a, 'tcx>,
197) -> Diag<'a> {
198 type_error_struct!(
199 fcx.dcx(),
200 span,
201 expr_ty,
202 E0606,
203 "casting `{}` as `{}` is invalid",
204 fcx.ty_to_string(expr_ty),
205 fcx.ty_to_string(cast_ty)
206 )
207}
208
209pub fn check_cast<'tcx>(
214 tcx: TyCtxt<'tcx>,
215 param_env: ty::ParamEnv<'tcx>,
216 e: &'tcx hir::Expr<'tcx>,
217 from_ty: Ty<'tcx>,
218 to_ty: Ty<'tcx>,
219) -> Option<CastKind> {
220 let hir_id = e.hir_id;
221 let local_def_id = hir_id.owner.def_id;
222
223 let root_ctxt = crate::TypeckRootCtxt::new(tcx, local_def_id);
224 let fn_ctxt = FnCtxt::new(&root_ctxt, param_env, local_def_id);
225
226 if let Ok(check) = CastCheck::new(
227 &fn_ctxt, e, from_ty, to_ty,
228 DUMMY_SP, DUMMY_SP,
230 ) {
231 check.do_check(&fn_ctxt).ok()
232 } else {
233 None
234 }
235}
236
237impl<'a, 'tcx> CastCheck<'tcx> {
238 pub(crate) fn new(
239 fcx: &FnCtxt<'a, 'tcx>,
240 expr: &'tcx hir::Expr<'tcx>,
241 expr_ty: Ty<'tcx>,
242 cast_ty: Ty<'tcx>,
243 cast_span: Span,
244 span: Span,
245 ) -> Result<CastCheck<'tcx>, ErrorGuaranteed> {
246 let expr_span = expr.span.find_ancestor_inside(span).unwrap_or(expr.span);
247 let check = CastCheck { expr, expr_ty, expr_span, cast_ty, cast_span, span };
248
249 match cast_ty.kind() {
253 ty::Dynamic(_, _) | ty::Slice(..) => Err(check.report_cast_to_unsized_type(fcx)),
254 _ => Ok(check),
255 }
256 }
257
258 fn report_cast_error(&self, fcx: &FnCtxt<'a, 'tcx>, e: CastError<'tcx>) {
259 match e {
260 CastError::ErrorGuaranteed(_) => {
261 }
263 CastError::NeedDeref => {
264 let mut err =
265 make_invalid_casting_error(self.span, self.expr_ty, self.cast_ty, fcx);
266
267 if matches!(self.expr.kind, ExprKind::AddrOf(..)) {
268 let span = self.expr_span.with_hi(self.expr.peel_borrows().span.lo());
270 err.span_suggestion_verbose(
271 span,
272 "remove the unneeded borrow",
273 "",
274 Applicability::MachineApplicable,
275 );
276 } else {
277 err.span_suggestion_verbose(
278 self.expr_span.shrink_to_lo(),
279 "dereference the expression",
280 "*",
281 Applicability::MachineApplicable,
282 );
283 }
284
285 err.emit();
286 }
287 CastError::NeedViaThinPtr | CastError::NeedViaPtr => {
288 let mut err =
289 make_invalid_casting_error(self.span, self.expr_ty, self.cast_ty, fcx);
290 if self.cast_ty.is_integral() {
291 err.help(format!("cast through {} first", match e {
292 CastError::NeedViaPtr => "a raw pointer",
293 CastError::NeedViaThinPtr => "a thin pointer",
294 e => unreachable!("control flow means we should never encounter a {e:?}"),
295 }));
296 }
297
298 self.try_suggest_collection_to_bool(fcx, &mut err);
299
300 err.emit();
301 }
302 CastError::NeedViaInt => {
303 make_invalid_casting_error(self.span, self.expr_ty, self.cast_ty, fcx)
304 .with_help("cast through an integer first")
305 .emit();
306 }
307 CastError::IllegalCast => {
308 make_invalid_casting_error(self.span, self.expr_ty, self.cast_ty, fcx).emit();
309 }
310 CastError::DifferingKinds { src_kind, dst_kind } => {
311 let mut err =
312 make_invalid_casting_error(self.span, self.expr_ty, self.cast_ty, fcx);
313
314 match (src_kind, dst_kind) {
315 (PointerKind::VTable(_), PointerKind::VTable(_)) => {
316 err.note("the trait objects may have different vtables");
317 }
318 (
319 PointerKind::OfParam(_) | PointerKind::OfAlias(_),
320 PointerKind::OfParam(_)
321 | PointerKind::OfAlias(_)
322 | PointerKind::VTable(_)
323 | PointerKind::Length,
324 )
325 | (
326 PointerKind::VTable(_) | PointerKind::Length,
327 PointerKind::OfParam(_) | PointerKind::OfAlias(_),
328 ) => {
329 err.note("the pointers may have different metadata");
330 }
331 (PointerKind::VTable(_), PointerKind::Length)
332 | (PointerKind::Length, PointerKind::VTable(_)) => {
333 err.note("the pointers have different metadata");
334 }
335 (
336 PointerKind::Thin,
337 PointerKind::Thin
338 | PointerKind::VTable(_)
339 | PointerKind::Length
340 | PointerKind::OfParam(_)
341 | PointerKind::OfAlias(_),
342 )
343 | (
344 PointerKind::VTable(_)
345 | PointerKind::Length
346 | PointerKind::OfParam(_)
347 | PointerKind::OfAlias(_),
348 PointerKind::Thin,
349 )
350 | (PointerKind::Length, PointerKind::Length) => {
351 span_bug!(self.span, "unexpected cast error: {e:?}")
352 }
353 }
354
355 err.emit();
356 }
357 CastError::CastToBool => {
358 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
359 let help = if self.expr_ty.is_numeric() {
360 errors::CannotCastToBoolHelp::Numeric(
361 self.expr_span.shrink_to_hi().with_hi(self.span.hi()),
362 )
363 } else {
364 errors::CannotCastToBoolHelp::Unsupported(self.span)
365 };
366 fcx.dcx().emit_err(errors::CannotCastToBool { span: self.span, expr_ty, help });
367 }
368 CastError::CastToChar => {
369 let mut err = type_error_struct!(
370 fcx.dcx(),
371 self.span,
372 self.expr_ty,
373 E0604,
374 "only `u8` can be cast as `char`, not `{}`",
375 self.expr_ty
376 );
377 err.span_label(self.span, "invalid cast");
378 if self.expr_ty.is_numeric() {
379 if self.expr_ty == fcx.tcx.types.u32 {
380 err.multipart_suggestion(
381 "consider using `char::from_u32` instead",
382 vec![
383 (self.expr_span.shrink_to_lo(), "char::from_u32(".to_string()),
384 (self.expr_span.shrink_to_hi().to(self.cast_span), ")".to_string()),
385 ],
386 Applicability::MachineApplicable,
387 );
388 } else if self.expr_ty == fcx.tcx.types.i8 {
389 err.span_help(self.span, "consider casting from `u8` instead");
390 } else {
391 err.span_help(
392 self.span,
393 "consider using `char::from_u32` instead (via a `u32`)",
394 );
395 };
396 }
397 err.emit();
398 }
399 CastError::NonScalar => {
400 let mut err = type_error_struct!(
401 fcx.dcx(),
402 self.span,
403 self.expr_ty,
404 E0605,
405 "non-primitive cast: `{}` as `{}`",
406 self.expr_ty,
407 fcx.ty_to_string(self.cast_ty)
408 );
409
410 if let Ok(snippet) = fcx.tcx.sess.source_map().span_to_snippet(self.expr_span)
411 && matches!(self.expr.kind, ExprKind::AddrOf(..))
412 {
413 err.note(format!(
414 "casting reference expression `{}` because `&` binds tighter than `as`",
415 snippet
416 ));
417 }
418
419 let mut sugg = None;
420 let mut sugg_mutref = false;
421 if let ty::Ref(reg, cast_ty, mutbl) = *self.cast_ty.kind() {
422 if let ty::RawPtr(expr_ty, _) = *self.expr_ty.kind()
423 && fcx.may_coerce(
424 Ty::new_ref(fcx.tcx, fcx.tcx.lifetimes.re_erased, expr_ty, mutbl),
425 self.cast_ty,
426 )
427 {
428 sugg = Some((format!("&{}*", mutbl.prefix_str()), cast_ty == expr_ty));
429 } else if let ty::Ref(expr_reg, expr_ty, expr_mutbl) = *self.expr_ty.kind()
430 && expr_mutbl == Mutability::Not
431 && mutbl == Mutability::Mut
432 && fcx.may_coerce(Ty::new_mut_ref(fcx.tcx, expr_reg, expr_ty), self.cast_ty)
433 {
434 sugg_mutref = true;
435 }
436
437 if !sugg_mutref
438 && sugg == None
439 && fcx.may_coerce(
440 Ty::new_ref(fcx.tcx, reg, self.expr_ty, mutbl),
441 self.cast_ty,
442 )
443 {
444 sugg = Some((format!("&{}", mutbl.prefix_str()), false));
445 }
446 } else if let ty::RawPtr(_, mutbl) = *self.cast_ty.kind()
447 && fcx.may_coerce(
448 Ty::new_ref(fcx.tcx, fcx.tcx.lifetimes.re_erased, self.expr_ty, mutbl),
449 self.cast_ty,
450 )
451 {
452 sugg = Some((format!("&{}", mutbl.prefix_str()), false));
453 }
454 if sugg_mutref {
455 err.span_label(self.span, "invalid cast");
456 err.span_note(self.expr_span, "this reference is immutable");
457 err.span_note(self.cast_span, "trying to cast to a mutable reference type");
458 } else if let Some((sugg, remove_cast)) = sugg {
459 err.span_label(self.span, "invalid cast");
460
461 let has_parens = fcx
462 .tcx
463 .sess
464 .source_map()
465 .span_to_snippet(self.expr_span)
466 .is_ok_and(|snip| snip.starts_with('('));
467
468 let needs_parens =
472 !has_parens && matches!(self.expr.kind, hir::ExprKind::Cast(..));
473
474 let mut suggestion = vec![(self.expr_span.shrink_to_lo(), sugg)];
475 if needs_parens {
476 suggestion[0].1 += "(";
477 suggestion.push((self.expr_span.shrink_to_hi(), ")".to_string()));
478 }
479 if remove_cast {
480 suggestion.push((
481 self.expr_span.shrink_to_hi().to(self.cast_span),
482 String::new(),
483 ));
484 }
485
486 err.multipart_suggestion_verbose(
487 "consider borrowing the value",
488 suggestion,
489 Applicability::MachineApplicable,
490 );
491 } else if !matches!(
492 self.cast_ty.kind(),
493 ty::FnDef(..) | ty::FnPtr(..) | ty::Closure(..)
494 ) {
495 if let Some(from_trait) = fcx.tcx.get_diagnostic_item(sym::From) {
497 let ty = fcx.resolve_vars_if_possible(self.cast_ty);
498 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
499 if fcx
500 .infcx
501 .type_implements_trait(from_trait, [ty, expr_ty], fcx.param_env)
502 .must_apply_modulo_regions()
503 {
504 let to_ty = if let ty::Adt(def, args) = self.cast_ty.kind() {
505 fcx.tcx.value_path_str_with_args(def.did(), args)
506 } else {
507 self.cast_ty.to_string()
508 };
509 err.multipart_suggestion(
510 "consider using the `From` trait instead",
511 vec![
512 (self.expr_span.shrink_to_lo(), format!("{to_ty}::from(")),
513 (
514 self.expr_span.shrink_to_hi().to(self.cast_span),
515 ")".to_string(),
516 ),
517 ],
518 Applicability::MaybeIncorrect,
519 );
520 }
521 }
522
523 let (msg, note) = if let ty::Adt(adt, _) = self.expr_ty.kind()
524 && adt.is_enum()
525 && self.cast_ty.is_numeric()
526 {
527 (
528 "an `as` expression can be used to convert enum types to numeric \
529 types only if the enum type is unit-only or field-less",
530 Some(
531 "see https://doc.rust-lang.org/reference/items/enumerations.html#casting for more information",
532 ),
533 )
534 } else {
535 (
536 "an `as` expression can only be used to convert between primitive \
537 types or to coerce to a specific trait object",
538 None,
539 )
540 };
541
542 err.span_label(self.span, msg);
543
544 if let Some(note) = note {
545 err.note(note);
546 }
547 } else {
548 err.span_label(self.span, "invalid cast");
549 }
550
551 fcx.suggest_no_capture_closure(&mut err, self.cast_ty, self.expr_ty);
552 self.try_suggest_collection_to_bool(fcx, &mut err);
553
554 err.emit();
555 }
556 CastError::SizedUnsizedCast => {
557 let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
558 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
559 fcx.dcx().emit_err(errors::CastThinPointerToWidePointer {
560 span: self.span,
561 expr_ty,
562 cast_ty,
563 teach: fcx.tcx.sess.teach(E0607),
564 });
565 }
566 CastError::IntToWideCast(known_metadata) => {
567 let expr_if_nightly = fcx.tcx.sess.is_nightly_build().then_some(self.expr_span);
568 let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
569 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
570 let metadata = known_metadata.unwrap_or("type-specific metadata");
571 let known_wide = known_metadata.is_some();
572 let span = self.cast_span;
573 fcx.dcx().emit_err(errors::IntToWide {
574 span,
575 metadata,
576 expr_ty,
577 cast_ty,
578 expr_if_nightly,
579 known_wide,
580 });
581 }
582 CastError::UnknownCastPtrKind | CastError::UnknownExprPtrKind => {
583 let unknown_cast_to = match e {
584 CastError::UnknownCastPtrKind => true,
585 CastError::UnknownExprPtrKind => false,
586 e => unreachable!("control flow means we should never encounter a {e:?}"),
587 };
588 let (span, sub) = if unknown_cast_to {
589 (self.cast_span, errors::CastUnknownPointerSub::To(self.cast_span))
590 } else {
591 (self.cast_span, errors::CastUnknownPointerSub::From(self.span))
592 };
593 fcx.dcx().emit_err(errors::CastUnknownPointer { span, to: unknown_cast_to, sub });
594 }
595 CastError::ForeignNonExhaustiveAdt => {
596 make_invalid_casting_error(
597 self.span,
598 self.expr_ty,
599 self.cast_ty,
600 fcx,
601 )
602 .with_note("cannot cast an enum with a non-exhaustive variant when it's defined in another crate")
603 .emit();
604 }
605 CastError::PtrPtrAddingAutoTrait(added) => {
606 fcx.dcx().emit_err(errors::PtrCastAddAutoToObject {
607 span: self.span,
608 traits_len: added.len(),
609 traits: {
610 let mut traits: Vec<_> = added
611 .into_iter()
612 .map(|trait_did| fcx.tcx.def_path_str(trait_did))
613 .collect();
614
615 traits.sort();
616 traits.into()
617 },
618 });
619 }
620 }
621 }
622
623 fn report_cast_to_unsized_type(&self, fcx: &FnCtxt<'a, 'tcx>) -> ErrorGuaranteed {
624 if let Err(err) = self.cast_ty.error_reported() {
625 return err;
626 }
627 if let Err(err) = self.expr_ty.error_reported() {
628 return err;
629 }
630
631 let tstr = fcx.ty_to_string(self.cast_ty);
632 let mut err = type_error_struct!(
633 fcx.dcx(),
634 self.span,
635 self.expr_ty,
636 E0620,
637 "cast to unsized type: `{}` as `{}`",
638 fcx.resolve_vars_if_possible(self.expr_ty),
639 tstr
640 );
641 match self.expr_ty.kind() {
642 ty::Ref(_, _, mt) => {
643 let mtstr = mt.prefix_str();
644 err.span_suggestion_verbose(
645 self.cast_span.shrink_to_lo(),
646 "consider casting to a reference instead",
647 format!("&{mtstr}"),
648 Applicability::MachineApplicable,
649 );
650 }
651 ty::Adt(def, ..) if def.is_box() => {
652 err.multipart_suggestion(
653 "you can cast to a `Box` instead",
654 vec![
655 (self.cast_span.shrink_to_lo(), "Box<".to_string()),
656 (self.cast_span.shrink_to_hi(), ">".to_string()),
657 ],
658 Applicability::MachineApplicable,
659 );
660 }
661 _ => {
662 err.span_help(self.expr_span, "consider using a box or reference as appropriate");
663 }
664 }
665 err.emit()
666 }
667
668 fn trivial_cast_lint(&self, fcx: &FnCtxt<'a, 'tcx>) {
669 let (numeric, lint) = if self.cast_ty.is_numeric() && self.expr_ty.is_numeric() {
670 (true, lint::builtin::TRIVIAL_NUMERIC_CASTS)
671 } else {
672 (false, lint::builtin::TRIVIAL_CASTS)
673 };
674 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
675 let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
676 fcx.tcx.emit_node_span_lint(
677 lint,
678 self.expr.hir_id,
679 self.span,
680 errors::TrivialCast { numeric, expr_ty, cast_ty },
681 );
682 }
683
684 #[instrument(skip(fcx), level = "debug")]
685 pub(crate) fn check(mut self, fcx: &FnCtxt<'a, 'tcx>) {
686 self.expr_ty = fcx.structurally_resolve_type(self.expr_span, self.expr_ty);
687 self.cast_ty = fcx.structurally_resolve_type(self.cast_span, self.cast_ty);
688
689 debug!("check_cast({}, {:?} as {:?})", self.expr.hir_id, self.expr_ty, self.cast_ty);
690
691 if !fcx.type_is_sized_modulo_regions(fcx.param_env, self.cast_ty)
692 && !self.cast_ty.has_infer_types()
693 {
694 self.report_cast_to_unsized_type(fcx);
695 } else if self.expr_ty.references_error() || self.cast_ty.references_error() {
696 } else {
698 match self.try_coercion_cast(fcx) {
699 Ok(()) => {
700 if self.expr_ty.is_raw_ptr() && self.cast_ty.is_raw_ptr() {
701 debug!(" -> PointerCast");
708 } else {
709 self.trivial_cast_lint(fcx);
710 debug!(" -> CoercionCast");
711 fcx.typeck_results
712 .borrow_mut()
713 .set_coercion_cast(self.expr.hir_id.local_id);
714 }
715 }
716 Err(_) => {
717 match self.do_check(fcx) {
718 Ok(k) => {
719 debug!(" -> {:?}", k);
720 }
721 Err(e) => self.report_cast_error(fcx, e),
722 };
723 }
724 };
725 }
726 }
727 fn do_check(&self, fcx: &FnCtxt<'a, 'tcx>) -> Result<CastKind, CastError<'tcx>> {
731 use rustc_middle::ty::cast::CastTy::*;
732 use rustc_middle::ty::cast::IntTy::*;
733
734 let (t_from, t_cast) = match (CastTy::from_ty(self.expr_ty), CastTy::from_ty(self.cast_ty))
735 {
736 (Some(t_from), Some(t_cast)) => (t_from, t_cast),
737 (None, Some(t_cast)) => {
739 match *self.expr_ty.kind() {
740 ty::FnDef(..) => {
741 let f = fcx.normalize(self.expr_span, self.expr_ty.fn_sig(fcx.tcx));
743 let res = fcx.coerce(
744 self.expr,
745 self.expr_ty,
746 Ty::new_fn_ptr(fcx.tcx, f),
747 AllowTwoPhase::No,
748 None,
749 );
750 if let Err(TypeError::IntrinsicCast) = res {
751 return Err(CastError::IllegalCast);
752 }
753 if res.is_err() {
754 return Err(CastError::NonScalar);
755 }
756 (FnPtr, t_cast)
757 }
758 ty::Ref(_, inner_ty, mutbl) => {
763 return match t_cast {
764 Int(_) | Float => match *inner_ty.kind() {
765 ty::Int(_)
766 | ty::Uint(_)
767 | ty::Float(_)
768 | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(_)) => {
769 Err(CastError::NeedDeref)
770 }
771 _ => Err(CastError::NeedViaPtr),
772 },
773 Ptr(mt) => {
775 if !fcx.type_is_sized_modulo_regions(fcx.param_env, mt.ty) {
776 return Err(CastError::IllegalCast);
777 }
778 self.check_ref_cast(fcx, TypeAndMut { mutbl, ty: inner_ty }, mt)
779 }
780 _ => Err(CastError::NonScalar),
781 };
782 }
783 _ => return Err(CastError::NonScalar),
784 }
785 }
786 _ => return Err(CastError::NonScalar),
787 };
788 if let ty::Adt(adt_def, _) = *self.expr_ty.kind()
789 && !adt_def.did().is_local()
790 && adt_def.variants().iter().any(VariantDef::is_field_list_non_exhaustive)
791 {
792 return Err(CastError::ForeignNonExhaustiveAdt);
793 }
794 match (t_from, t_cast) {
795 (_, Int(CEnum) | FnPtr) => Err(CastError::NonScalar),
797
798 (_, Int(Bool)) => Err(CastError::CastToBool),
800
801 (Int(U(ty::UintTy::U8)), Int(Char)) => Ok(CastKind::U8CharCast), (_, Int(Char)) => Err(CastError::CastToChar),
804
805 (Int(Bool) | Int(CEnum) | Int(Char), Float) => Err(CastError::NeedViaInt),
807
808 (Int(Bool) | Int(CEnum) | Int(Char) | Float, Ptr(_)) | (Ptr(_) | FnPtr, Float) => {
809 Err(CastError::IllegalCast)
810 }
811
812 (Ptr(m_e), Ptr(m_c)) => self.check_ptr_ptr_cast(fcx, m_e, m_c), (Ptr(m_expr), Int(t_c)) => {
817 self.lossy_provenance_ptr2int_lint(fcx, t_c);
818 self.check_ptr_addr_cast(fcx, m_expr)
819 }
820 (FnPtr, Int(_)) => {
821 Ok(CastKind::FnPtrAddrCast)
823 }
824 (Int(_), Ptr(mt)) => {
826 self.fuzzy_provenance_int2ptr_lint(fcx);
827 self.check_addr_ptr_cast(fcx, mt)
828 }
829 (FnPtr, Ptr(mt)) => self.check_fptr_ptr_cast(fcx, mt),
831
832 (Int(CEnum), Int(_)) => {
834 self.err_if_cenum_impl_drop(fcx);
835 Ok(CastKind::EnumCast)
836 }
837 (Int(Char) | Int(Bool), Int(_)) => Ok(CastKind::PrimIntCast),
838
839 (Int(_) | Float, Int(_) | Float) => Ok(CastKind::NumericCast),
840 }
841 }
842
843 fn check_ptr_ptr_cast(
844 &self,
845 fcx: &FnCtxt<'a, 'tcx>,
846 m_src: ty::TypeAndMut<'tcx>,
847 m_dst: ty::TypeAndMut<'tcx>,
848 ) -> Result<CastKind, CastError<'tcx>> {
849 debug!("check_ptr_ptr_cast m_src={m_src:?} m_dst={m_dst:?}");
850 let src_kind = fcx.tcx.erase_and_anonymize_regions(fcx.pointer_kind(m_src.ty, self.span)?);
853 let dst_kind = fcx.tcx.erase_and_anonymize_regions(fcx.pointer_kind(m_dst.ty, self.span)?);
854
855 let Some(dst_kind) = dst_kind else {
857 return Err(CastError::UnknownCastPtrKind);
858 };
859
860 if dst_kind == PointerKind::Thin {
862 return Ok(CastKind::PtrPtrCast);
863 }
864
865 let Some(src_kind) = src_kind else {
867 return Err(CastError::UnknownCastPtrKind);
868 };
869
870 match (src_kind, dst_kind) {
871 (PointerKind::Thin, _) => Err(CastError::SizedUnsizedCast),
873
874 (PointerKind::VTable(src_tty), PointerKind::VTable(dst_tty)) => {
876 match (src_tty.principal(), dst_tty.principal()) {
877 (Some(src_principal), Some(_)) => {
886 let tcx = fcx.tcx;
887
888 let src_obj = Ty::new_dynamic(
896 tcx,
897 tcx.mk_poly_existential_predicates(
898 &src_tty.without_auto_traits().collect::<Vec<_>>(),
899 ),
900 tcx.lifetimes.re_erased,
901 );
902 let dst_obj = Ty::new_dynamic(
903 tcx,
904 tcx.mk_poly_existential_predicates(
905 &dst_tty.without_auto_traits().collect::<Vec<_>>(),
906 ),
907 tcx.lifetimes.re_erased,
908 );
909
910 let cause = fcx.misc(self.span);
913 if fcx
914 .at(&cause, fcx.param_env)
915 .eq(DefineOpaqueTypes::Yes, src_obj, dst_obj)
916 .map(|infer_ok| fcx.register_infer_ok_obligations(infer_ok))
917 .is_err()
918 {
919 return Err(CastError::DifferingKinds { src_kind, dst_kind });
920 }
921
922 let src_auto: FxHashSet<_> = src_tty
925 .auto_traits()
926 .chain(
927 elaborate::supertrait_def_ids(tcx, src_principal.def_id())
928 .filter(|def_id| tcx.trait_is_auto(*def_id)),
929 )
930 .collect();
931
932 let added = dst_tty
933 .auto_traits()
934 .filter(|trait_did| !src_auto.contains(trait_did))
935 .collect::<Vec<_>>();
936
937 if !added.is_empty() {
938 return Err(CastError::PtrPtrAddingAutoTrait(added));
939 }
940
941 Ok(CastKind::PtrPtrCast)
942 }
943
944 (None, None) => Ok(CastKind::PtrPtrCast),
946
947 (Some(_), None) => Err(CastError::DifferingKinds { src_kind, dst_kind }),
977
978 (None, Some(_)) => Err(CastError::DifferingKinds { src_kind, dst_kind }),
980 }
981 }
982
983 (src_kind, dst_kind) if src_kind == dst_kind => Ok(CastKind::PtrPtrCast),
985
986 (_, _) => Err(CastError::DifferingKinds { src_kind, dst_kind }),
987 }
988 }
989
990 fn check_fptr_ptr_cast(
991 &self,
992 fcx: &FnCtxt<'a, 'tcx>,
993 m_cast: ty::TypeAndMut<'tcx>,
994 ) -> Result<CastKind, CastError<'tcx>> {
995 match fcx.pointer_kind(m_cast.ty, self.span)? {
998 None => Err(CastError::UnknownCastPtrKind),
999 Some(PointerKind::Thin) => Ok(CastKind::FnPtrPtrCast),
1000 _ => Err(CastError::IllegalCast),
1001 }
1002 }
1003
1004 fn check_ptr_addr_cast(
1005 &self,
1006 fcx: &FnCtxt<'a, 'tcx>,
1007 m_expr: ty::TypeAndMut<'tcx>,
1008 ) -> Result<CastKind, CastError<'tcx>> {
1009 match fcx.pointer_kind(m_expr.ty, self.span)? {
1012 None => Err(CastError::UnknownExprPtrKind),
1013 Some(PointerKind::Thin) => Ok(CastKind::PtrAddrCast),
1014 _ => Err(CastError::NeedViaThinPtr),
1015 }
1016 }
1017
1018 fn check_ref_cast(
1019 &self,
1020 fcx: &FnCtxt<'a, 'tcx>,
1021 mut m_expr: ty::TypeAndMut<'tcx>,
1022 mut m_cast: ty::TypeAndMut<'tcx>,
1023 ) -> Result<CastKind, CastError<'tcx>> {
1024 m_expr.ty = fcx.try_structurally_resolve_type(self.expr_span, m_expr.ty);
1026 m_cast.ty = fcx.try_structurally_resolve_type(self.cast_span, m_cast.ty);
1027
1028 if m_expr.mutbl >= m_cast.mutbl
1029 && let ty::Array(ety, _) = m_expr.ty.kind()
1030 && fcx.can_eq(fcx.param_env, *ety, m_cast.ty)
1031 {
1032 let array_ptr_type = Ty::new_ptr(fcx.tcx, m_expr.ty, m_expr.mutbl);
1037 fcx.coerce(self.expr, self.expr_ty, array_ptr_type, AllowTwoPhase::No, None)
1038 .unwrap_or_else(|_| {
1039 bug!(
1040 "could not cast from reference to array to pointer to array ({:?} to {:?})",
1041 self.expr_ty,
1042 array_ptr_type,
1043 )
1044 });
1045
1046 fcx.demand_eqtype(self.span, *ety, m_cast.ty);
1048 return Ok(CastKind::ArrayPtrCast);
1049 }
1050
1051 Err(CastError::IllegalCast)
1052 }
1053
1054 fn check_addr_ptr_cast(
1055 &self,
1056 fcx: &FnCtxt<'a, 'tcx>,
1057 m_cast: TypeAndMut<'tcx>,
1058 ) -> Result<CastKind, CastError<'tcx>> {
1059 match fcx.pointer_kind(m_cast.ty, self.span)? {
1061 None => Err(CastError::UnknownCastPtrKind),
1062 Some(PointerKind::Thin) => Ok(CastKind::AddrPtrCast),
1063 Some(PointerKind::VTable(_)) => Err(CastError::IntToWideCast(Some("a vtable"))),
1064 Some(PointerKind::Length) => Err(CastError::IntToWideCast(Some("a length"))),
1065 Some(PointerKind::OfAlias(_) | PointerKind::OfParam(_)) => {
1066 Err(CastError::IntToWideCast(None))
1067 }
1068 }
1069 }
1070
1071 fn try_coercion_cast(&self, fcx: &FnCtxt<'a, 'tcx>) -> Result<(), ty::error::TypeError<'tcx>> {
1072 match fcx.coerce(self.expr, self.expr_ty, self.cast_ty, AllowTwoPhase::No, None) {
1073 Ok(_) => Ok(()),
1074 Err(err) => Err(err),
1075 }
1076 }
1077
1078 fn err_if_cenum_impl_drop(&self, fcx: &FnCtxt<'a, 'tcx>) {
1079 if let ty::Adt(d, _) = self.expr_ty.kind()
1080 && d.has_dtor(fcx.tcx)
1081 {
1082 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
1083 let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
1084
1085 fcx.dcx().emit_err(errors::CastEnumDrop { span: self.span, expr_ty, cast_ty });
1086 }
1087 }
1088
1089 fn lossy_provenance_ptr2int_lint(&self, fcx: &FnCtxt<'a, 'tcx>, t_c: ty::cast::IntTy) {
1090 let expr_prec = fcx.precedence(self.expr);
1091 let needs_parens = expr_prec < ExprPrecedence::Unambiguous;
1092
1093 let needs_cast = !matches!(t_c, ty::cast::IntTy::U(ty::UintTy::Usize));
1094 let cast_span = self.expr_span.shrink_to_hi().to(self.cast_span);
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 let expr_span = self.expr_span.shrink_to_lo();
1098 let sugg = match (needs_parens, needs_cast) {
1099 (true, true) => errors::LossyProvenancePtr2IntSuggestion::NeedsParensCast {
1100 expr_span,
1101 cast_span,
1102 cast_ty,
1103 },
1104 (true, false) => {
1105 errors::LossyProvenancePtr2IntSuggestion::NeedsParens { expr_span, cast_span }
1106 }
1107 (false, true) => {
1108 errors::LossyProvenancePtr2IntSuggestion::NeedsCast { cast_span, cast_ty }
1109 }
1110 (false, false) => errors::LossyProvenancePtr2IntSuggestion::Other { cast_span },
1111 };
1112
1113 let lint = errors::LossyProvenancePtr2Int { expr_ty, cast_ty, sugg };
1114 fcx.tcx.emit_node_span_lint(
1115 lint::builtin::LOSSY_PROVENANCE_CASTS,
1116 self.expr.hir_id,
1117 self.span,
1118 lint,
1119 );
1120 }
1121
1122 fn fuzzy_provenance_int2ptr_lint(&self, fcx: &FnCtxt<'a, 'tcx>) {
1123 let sugg = errors::LossyProvenanceInt2PtrSuggestion {
1124 lo: self.expr_span.shrink_to_lo(),
1125 hi: self.expr_span.shrink_to_hi().to(self.cast_span),
1126 };
1127 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
1128 let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
1129 let lint = errors::LossyProvenanceInt2Ptr { expr_ty, cast_ty, sugg };
1130 fcx.tcx.emit_node_span_lint(
1131 lint::builtin::FUZZY_PROVENANCE_CASTS,
1132 self.expr.hir_id,
1133 self.span,
1134 lint,
1135 );
1136 }
1137
1138 fn try_suggest_collection_to_bool(&self, fcx: &FnCtxt<'a, 'tcx>, err: &mut Diag<'_>) {
1141 if self.cast_ty.is_bool() {
1142 let derefed = fcx
1143 .autoderef(self.expr_span, self.expr_ty)
1144 .silence_errors()
1145 .find(|t| matches!(t.0.kind(), ty::Str | ty::Slice(..)));
1146
1147 if let Some((deref_ty, _)) = derefed {
1148 if deref_ty != self.expr_ty.peel_refs() {
1150 err.subdiagnostic(errors::DerefImplsIsEmpty { span: self.expr_span, deref_ty });
1151 }
1152
1153 err.subdiagnostic(errors::UseIsEmpty {
1156 lo: self.expr_span.shrink_to_lo(),
1157 hi: self.span.with_lo(self.expr_span.hi()),
1158 expr_ty: self.expr_ty,
1159 });
1160 }
1161 }
1162 }
1163}