1use rustc_data_structures::fx::FxHashSet;
32use rustc_errors::codes::*;
33use rustc_errors::{Applicability, Diag, ErrorGuaranteed};
34use rustc_hir::def_id::{DefId, LocalDefId};
35use rustc_hir::{self as hir, ExprKind};
36use rustc_infer::infer::DefineOpaqueTypes;
37use rustc_macros::{TypeFoldable, TypeVisitable};
38use rustc_middle::mir::Mutability;
39use rustc_middle::ty::adjustment::AllowTwoPhase;
40use rustc_middle::ty::cast::{CastKind, CastTy};
41use rustc_middle::ty::error::TypeError;
42use rustc_middle::ty::{
43 self, Ty, TyCtxt, TypeAndMut, TypeVisitableExt, Unnormalized, VariantDef, elaborate,
44};
45use rustc_middle::{bug, span_bug};
46use rustc_session::lint;
47use rustc_span::{DUMMY_SP, Span, sym};
48use rustc_trait_selection::infer::InferCtxtExt;
49use tracing::{debug, instrument};
50
51use super::FnCtxt;
52use crate::{diagnostics, type_error_struct};
53
54#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for CastCheck<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
let names: &'static _ =
&["expr", "expr_ty", "expr_span", "cast_ty", "cast_span", "span",
"body_id"];
let values: &[&dyn ::core::fmt::Debug] =
&[&self.expr, &self.expr_ty, &self.expr_span, &self.cast_ty,
&self.cast_span, &self.span, &&self.body_id];
::core::fmt::Formatter::debug_struct_fields_finish(f, "CastCheck",
names, values)
}
}Debug)]
57pub(crate) struct CastCheck<'tcx> {
58 expr: &'tcx hir::Expr<'tcx>,
60 expr_ty: Ty<'tcx>,
62 expr_span: Span,
63 cast_ty: Ty<'tcx>,
65 cast_span: Span,
66 span: Span,
67 pub body_id: LocalDefId,
68}
69
70#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for PointerKind<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
PointerKind::Thin => ::core::fmt::Formatter::write_str(f, "Thin"),
PointerKind::VTable(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "VTable",
&__self_0),
PointerKind::Length =>
::core::fmt::Formatter::write_str(f, "Length"),
PointerKind::OfAlias(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"OfAlias", &__self_0),
PointerKind::OfParam(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"OfParam", &__self_0),
}
}
}Debug, #[automatically_derived]
impl<'tcx> ::core::marker::Copy for PointerKind<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for PointerKind<'tcx> {
#[inline]
fn clone(&self) -> PointerKind<'tcx> {
let _:
::core::clone::AssertParamIsClone<&'tcx ty::List<ty::Binder<'tcx,
ty::ExistentialPredicate<'tcx>>>>;
let _: ::core::clone::AssertParamIsClone<ty::AliasTy<'tcx>>;
let _: ::core::clone::AssertParamIsClone<ty::ParamTy>;
*self
}
}Clone, #[automatically_derived]
impl<'tcx> ::core::cmp::PartialEq for PointerKind<'tcx> {
#[inline]
fn eq(&self, other: &PointerKind<'tcx>) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(PointerKind::VTable(__self_0), PointerKind::VTable(__arg1_0))
=> __self_0 == __arg1_0,
(PointerKind::OfAlias(__self_0),
PointerKind::OfAlias(__arg1_0)) => __self_0 == __arg1_0,
(PointerKind::OfParam(__self_0),
PointerKind::OfParam(__arg1_0)) => __self_0 == __arg1_0,
_ => true,
}
}
}PartialEq, #[automatically_derived]
impl<'tcx> ::core::cmp::Eq for PointerKind<'tcx> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _:
::core::cmp::AssertParamIsEq<&'tcx ty::List<ty::Binder<'tcx,
ty::ExistentialPredicate<'tcx>>>>;
let _: ::core::cmp::AssertParamIsEq<ty::AliasTy<'tcx>>;
let _: ::core::cmp::AssertParamIsEq<ty::ParamTy>;
}
}Eq, const _: () =
{
impl<'tcx>
::rustc_middle::ty::TypeVisitable<::rustc_middle::ty::TyCtxt<'tcx>>
for PointerKind<'tcx> {
fn visit_with<__V: ::rustc_middle::ty::TypeVisitor<::rustc_middle::ty::TyCtxt<'tcx>>>(&self,
__visitor: &mut __V) -> __V::Result {
match *self {
PointerKind::Thin => {}
PointerKind::VTable(ref __binding_0) => {
{
match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_0,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_middle::ty::VisitorResult::from_residual(r);
}
}
}
}
PointerKind::Length => {}
PointerKind::OfAlias(ref __binding_0) => {
{
match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_0,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_middle::ty::VisitorResult::from_residual(r);
}
}
}
}
PointerKind::OfParam(ref __binding_0) => {
{
match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_0,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_middle::ty::VisitorResult::from_residual(r);
}
}
}
}
}
<__V::Result as ::rustc_middle::ty::VisitorResult>::output()
}
}
};TypeVisitable, const _: () =
{
impl<'tcx>
::rustc_middle::ty::TypeFoldable<::rustc_middle::ty::TyCtxt<'tcx>>
for PointerKind<'tcx> {
fn try_fold_with<__F: ::rustc_middle::ty::FallibleTypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(self,
__folder: &mut __F) -> Result<Self, __F::Error> {
Ok(match self {
PointerKind::Thin => { PointerKind::Thin }
PointerKind::VTable(__binding_0) => {
PointerKind::VTable(::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_0,
__folder)?)
}
PointerKind::Length => { PointerKind::Length }
PointerKind::OfAlias(__binding_0) => {
PointerKind::OfAlias(::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_0,
__folder)?)
}
PointerKind::OfParam(__binding_0) => {
PointerKind::OfParam(::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_0,
__folder)?)
}
})
}
fn fold_with<__F: ::rustc_middle::ty::TypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(self,
__folder: &mut __F) -> Self {
match self {
PointerKind::Thin => { PointerKind::Thin }
PointerKind::VTable(__binding_0) => {
PointerKind::VTable(::rustc_middle::ty::TypeFoldable::fold_with(__binding_0,
__folder))
}
PointerKind::Length => { PointerKind::Length }
PointerKind::OfAlias(__binding_0) => {
PointerKind::OfAlias(::rustc_middle::ty::TypeFoldable::fold_with(__binding_0,
__folder))
}
PointerKind::OfParam(__binding_0) => {
PointerKind::OfParam(::rustc_middle::ty::TypeFoldable::fold_with(__binding_0,
__folder))
}
}
}
}
};TypeFoldable)]
74enum PointerKind<'tcx> {
75 Thin,
77 VTable(&'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>),
79 Length,
81 OfAlias(ty::AliasTy<'tcx>),
83 OfParam(ty::ParamTy),
85}
86
87impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
88 fn pointer_kind(
91 &self,
92 t: Ty<'tcx>,
93 span: Span,
94 ) -> Result<Option<PointerKind<'tcx>>, ErrorGuaranteed> {
95 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/cast.rs:95",
"rustc_hir_typeck::cast", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/cast.rs"),
::tracing_core::__macro_support::Option::Some(95u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::cast"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("pointer_kind({0:?}, {1:?})",
t, span) as &dyn Value))])
});
} else { ; }
};debug!("pointer_kind({:?}, {:?})", t, span);
96
97 let t = self.resolve_vars_if_possible(t);
98 t.error_reported()?;
99
100 if self.type_is_sized_modulo_regions(self.param_env, t) {
101 return Ok(Some(PointerKind::Thin));
102 }
103
104 let t = self.resolve_vars_with_obligations(t);
105
106 Ok(match *t.kind() {
107 ty::Slice(_) | ty::Str => Some(PointerKind::Length),
108 ty::Dynamic(tty, _) => Some(PointerKind::VTable(tty)),
109 ty::Adt(def, args) if def.is_struct() => match def.non_enum_variant().tail_opt() {
110 None => Some(PointerKind::Thin),
111 Some(f) => {
112 let field_ty = self.field_ty(span, f, args);
113 self.pointer_kind(field_ty, span)?
114 }
115 },
116 ty::Tuple(fields) => match fields.last() {
117 None => Some(PointerKind::Thin),
118 Some(&f) => self.pointer_kind(f, span)?,
119 },
120
121 ty::UnsafeBinder(_) => {
::core::panicking::panic_fmt(format_args!("not yet implemented: {0}",
format_args!("FIXME(unsafe_binder)")));
}todo!("FIXME(unsafe_binder)"),
122
123 ty::Foreign(..) => Some(PointerKind::Thin),
125 ty::Alias(pi) => Some(PointerKind::OfAlias(pi)),
127 ty::Param(p) => Some(PointerKind::OfParam(p)),
128 ty::Placeholder(..) | ty::Bound(..) | ty::Infer(_) => None,
130
131 ty::Bool
132 | ty::Char
133 | ty::Int(..)
134 | ty::Uint(..)
135 | ty::Float(_)
136 | ty::Array(..)
137 | ty::CoroutineWitness(..)
138 | ty::RawPtr(_, _)
139 | ty::Ref(..)
140 | ty::Pat(..)
141 | ty::FnDef(..)
142 | ty::FnPtr(..)
143 | ty::Closure(..)
144 | ty::CoroutineClosure(..)
145 | ty::Coroutine(..)
146 | ty::Adt(..)
147 | ty::Never
148 | ty::Error(_) => {
149 let guar = self
150 .dcx()
151 .span_delayed_bug(span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0:?}` should be sized but is not?",
t))
})format!("`{t:?}` should be sized but is not?"));
152 return Err(guar);
153 }
154 })
155 }
156}
157
158#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for CastError<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
CastError::ErrorGuaranteed(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"ErrorGuaranteed", &__self_0),
CastError::CastToBool =>
::core::fmt::Formatter::write_str(f, "CastToBool"),
CastError::CastToChar =>
::core::fmt::Formatter::write_str(f, "CastToChar"),
CastError::DifferingKinds { src_kind: __self_0, dst_kind: __self_1
} =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"DifferingKinds", "src_kind", __self_0, "dst_kind",
&__self_1),
CastError::SizedUnsizedCast =>
::core::fmt::Formatter::write_str(f, "SizedUnsizedCast"),
CastError::IllegalCast =>
::core::fmt::Formatter::write_str(f, "IllegalCast"),
CastError::NeedDeref =>
::core::fmt::Formatter::write_str(f, "NeedDeref"),
CastError::NeedViaPtr =>
::core::fmt::Formatter::write_str(f, "NeedViaPtr"),
CastError::NeedViaThinPtr =>
::core::fmt::Formatter::write_str(f, "NeedViaThinPtr"),
CastError::NeedViaInt =>
::core::fmt::Formatter::write_str(f, "NeedViaInt"),
CastError::NonScalar =>
::core::fmt::Formatter::write_str(f, "NonScalar"),
CastError::UnknownExprPtrKind =>
::core::fmt::Formatter::write_str(f, "UnknownExprPtrKind"),
CastError::UnknownCastPtrKind =>
::core::fmt::Formatter::write_str(f, "UnknownCastPtrKind"),
CastError::CastEnumDrop =>
::core::fmt::Formatter::write_str(f, "CastEnumDrop"),
CastError::IntToWideCast(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"IntToWideCast", &__self_0),
CastError::ForeignNonExhaustiveAdt =>
::core::fmt::Formatter::write_str(f,
"ForeignNonExhaustiveAdt"),
CastError::PtrPtrAddingAutoTrait(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"PtrPtrAddingAutoTrait", &__self_0),
}
}
}Debug)]
159enum CastError<'tcx> {
160 ErrorGuaranteed(ErrorGuaranteed),
161
162 CastToBool,
163 CastToChar,
164 DifferingKinds {
165 src_kind: PointerKind<'tcx>,
166 dst_kind: PointerKind<'tcx>,
167 },
168 SizedUnsizedCast,
170 IllegalCast,
171 NeedDeref,
172 NeedViaPtr,
173 NeedViaThinPtr,
174 NeedViaInt,
175 NonScalar,
176 UnknownExprPtrKind,
177 UnknownCastPtrKind,
178 CastEnumDrop,
179 IntToWideCast(Option<&'static str>),
185 ForeignNonExhaustiveAdt,
186 PtrPtrAddingAutoTrait(Vec<DefId>),
187}
188
189impl From<ErrorGuaranteed> for CastError<'_> {
190 fn from(err: ErrorGuaranteed) -> Self {
191 CastError::ErrorGuaranteed(err)
192 }
193}
194
195fn make_invalid_casting_error<'a, 'tcx>(
196 span: Span,
197 expr_ty: Ty<'tcx>,
198 cast_ty: Ty<'tcx>,
199 fcx: &FnCtxt<'a, 'tcx>,
200) -> Diag<'a> {
201 {
let mut err =
{
fcx.dcx().struct_span_err(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("casting `{0}` as `{1}` is invalid",
fcx.ty_to_string(expr_ty), fcx.ty_to_string(cast_ty)))
})).with_code(E0606)
};
if expr_ty.references_error() { err.downgrade_to_delayed_bug(); }
err
}type_error_struct!(
202 fcx.dcx(),
203 span,
204 expr_ty,
205 E0606,
206 "casting `{}` as `{}` is invalid",
207 fcx.ty_to_string(expr_ty),
208 fcx.ty_to_string(cast_ty)
209 )
210}
211
212pub fn check_cast<'tcx>(
217 tcx: TyCtxt<'tcx>,
218 param_env: ty::ParamEnv<'tcx>,
219 e: &'tcx hir::Expr<'tcx>,
220 from_ty: Ty<'tcx>,
221 to_ty: Ty<'tcx>,
222) -> Option<CastKind> {
223 let hir_id = e.hir_id;
224 let local_def_id = hir_id.owner.def_id;
225
226 let root_ctxt = crate::TypeckRootCtxt::new(tcx, local_def_id);
227 let fn_ctxt = FnCtxt::new(&root_ctxt, param_env, local_def_id);
228
229 if let Ok(check) = CastCheck::new(
230 &fn_ctxt, e, from_ty, to_ty,
231 DUMMY_SP, DUMMY_SP,
233 ) {
234 check.do_check(&fn_ctxt).ok()
235 } else {
236 None
237 }
238}
239
240impl<'a, 'tcx> CastCheck<'tcx> {
241 pub(crate) fn new(
242 fcx: &FnCtxt<'a, 'tcx>,
243 expr: &'tcx hir::Expr<'tcx>,
244 expr_ty: Ty<'tcx>,
245 cast_ty: Ty<'tcx>,
246 cast_span: Span,
247 span: Span,
248 ) -> Result<CastCheck<'tcx>, ErrorGuaranteed> {
249 let expr_span = expr.span.find_ancestor_inside(span).unwrap_or(expr.span);
250 let check =
251 CastCheck { expr, expr_ty, expr_span, cast_ty, cast_span, span, body_id: fcx.body_id };
252
253 match cast_ty.kind() {
257 ty::Dynamic(_, _) | ty::Slice(..) => Err(check.report_cast_to_unsized_type(fcx)),
258 _ => Ok(check),
259 }
260 }
261
262 fn report_cast_error(&self, fcx: &FnCtxt<'a, 'tcx>, e: CastError<'tcx>) {
263 match e {
264 CastError::ErrorGuaranteed(_) => {
265 }
267 CastError::NeedDeref => {
268 let mut err =
269 make_invalid_casting_error(self.span, self.expr_ty, self.cast_ty, fcx);
270
271 if #[allow(non_exhaustive_omitted_patterns)] match self.expr.kind {
ExprKind::AddrOf(..) => true,
_ => false,
}matches!(self.expr.kind, ExprKind::AddrOf(..)) {
272 let span = self.expr_span.with_hi(self.expr.peel_borrows().span.lo());
274 err.span_suggestion_verbose(
275 span,
276 "remove the unneeded borrow",
277 "",
278 Applicability::MachineApplicable,
279 );
280 } else {
281 err.span_suggestion_verbose(
282 self.expr_span.shrink_to_lo(),
283 "dereference the expression",
284 "*",
285 Applicability::MachineApplicable,
286 );
287 }
288
289 err.emit();
290 }
291 CastError::NeedViaThinPtr | CastError::NeedViaPtr => {
292 let mut err =
293 make_invalid_casting_error(self.span, self.expr_ty, self.cast_ty, fcx);
294
295 if self.cast_ty.is_integral() {
296 if !#[allow(non_exhaustive_omitted_patterns)] match self.expr.kind {
ExprKind::AddrOf(..) => true,
_ => false,
}matches!(self.expr.kind, ExprKind::AddrOf(..))
297 && let ty::Ref(_, inner_ty, _) = *self.expr_ty.kind()
298 && let ty::Adt(adt_def, _) = *inner_ty.kind()
299 && adt_def.is_enum()
300 && adt_def.is_payloadfree()
301 {
302 err.span_suggestion_verbose(
303 self.expr_span.shrink_to_lo(),
304 "try dereferencing before the cast",
305 "*",
306 Applicability::MaybeIncorrect,
307 );
308 if !fcx.type_is_copy_modulo_regions(fcx.param_env, inner_ty) {
309 err.span_suggestion_verbose(
310 fcx.tcx.def_span(adt_def.did()).shrink_to_lo(),
311 "add `#[derive(Copy, Clone)]` to the enum definition",
312 "#[derive(Copy, Clone)]\n",
313 Applicability::MaybeIncorrect,
314 );
315 }
316 } else {
317 err.help(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("cast through {0} first",
match e {
CastError::NeedViaPtr => "a raw pointer",
CastError::NeedViaThinPtr => "a thin pointer",
e => {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("control flow means we should never encounter a {0:?}",
e)));
}
}))
})format!(
318 "cast through {} first",
319 match e {
320 CastError::NeedViaPtr => "a raw pointer",
321 CastError::NeedViaThinPtr => "a thin pointer",
322 e => unreachable!(
323 "control flow means we should never encounter a {e:?}"
324 ),
325 }
326 ));
327 }
328 }
329
330 self.try_suggest_collection_to_bool(fcx, &mut err);
331
332 err.emit();
333 }
334 CastError::NeedViaInt => {
335 make_invalid_casting_error(self.span, self.expr_ty, self.cast_ty, fcx)
336 .with_help("cast through an integer first")
337 .emit();
338 }
339 CastError::IllegalCast => {
340 make_invalid_casting_error(self.span, self.expr_ty, self.cast_ty, fcx).emit();
341 }
342 CastError::DifferingKinds { src_kind, dst_kind } => {
343 let mut err =
344 make_invalid_casting_error(self.span, self.expr_ty, self.cast_ty, fcx);
345
346 match (src_kind, dst_kind) {
347 (PointerKind::VTable(_), PointerKind::VTable(_)) => {
348 err.note("the trait objects may have different vtables");
349 }
350 (
351 PointerKind::OfParam(_) | PointerKind::OfAlias(_),
352 PointerKind::OfParam(_)
353 | PointerKind::OfAlias(_)
354 | PointerKind::VTable(_)
355 | PointerKind::Length,
356 )
357 | (
358 PointerKind::VTable(_) | PointerKind::Length,
359 PointerKind::OfParam(_) | PointerKind::OfAlias(_),
360 ) => {
361 err.note("the pointers may have different metadata");
362 }
363 (PointerKind::VTable(_), PointerKind::Length)
364 | (PointerKind::Length, PointerKind::VTable(_)) => {
365 err.note("the pointers have different metadata");
366 }
367 (
368 PointerKind::Thin,
369 PointerKind::Thin
370 | PointerKind::VTable(_)
371 | PointerKind::Length
372 | PointerKind::OfParam(_)
373 | PointerKind::OfAlias(_),
374 )
375 | (
376 PointerKind::VTable(_)
377 | PointerKind::Length
378 | PointerKind::OfParam(_)
379 | PointerKind::OfAlias(_),
380 PointerKind::Thin,
381 )
382 | (PointerKind::Length, PointerKind::Length) => {
383 ::rustc_middle::util::bug::span_bug_fmt(self.span,
format_args!("unexpected cast error: {0:?}", e))span_bug!(self.span, "unexpected cast error: {e:?}")
384 }
385 }
386
387 err.emit();
388 }
389 CastError::CastToBool => {
390 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
391 let help = if self.expr_ty.is_numeric() {
392 diagnostics::CannotCastToBoolHelp::Numeric(
393 self.expr_span.shrink_to_hi().with_hi(self.span.hi()),
394 )
395 } else {
396 diagnostics::CannotCastToBoolHelp::Unsupported(self.span)
397 };
398 fcx.dcx().emit_err(diagnostics::CannotCastToBool {
399 span: self.span,
400 expr_ty,
401 help,
402 });
403 }
404 CastError::CastToChar => {
405 let mut err = {
let mut err =
{
fcx.dcx().struct_span_err(self.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("only `u8` can be cast as `char`, not `{0}`",
self.expr_ty))
})).with_code(E0604)
};
if self.expr_ty.references_error() { err.downgrade_to_delayed_bug(); }
err
}type_error_struct!(
406 fcx.dcx(),
407 self.span,
408 self.expr_ty,
409 E0604,
410 "only `u8` can be cast as `char`, not `{}`",
411 self.expr_ty
412 );
413 err.span_label(self.span, "invalid cast");
414 if self.expr_ty.is_numeric() {
415 if self.expr_ty == fcx.tcx.types.u32 {
416 err.multipart_suggestion(
417 "consider using `char::from_u32` instead",
418 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(self.expr_span.shrink_to_lo(), "char::from_u32(".to_string()),
(self.expr_span.shrink_to_hi().to(self.cast_span),
")".to_string())]))vec![
419 (self.expr_span.shrink_to_lo(), "char::from_u32(".to_string()),
420 (self.expr_span.shrink_to_hi().to(self.cast_span), ")".to_string()),
421 ],
422 Applicability::MachineApplicable,
423 );
424 } else if self.expr_ty == fcx.tcx.types.i8 {
425 err.span_help(self.span, "consider casting from `u8` instead");
426 } else {
427 err.span_help(
428 self.span,
429 "consider using `char::from_u32` instead (via a `u32`)",
430 );
431 };
432 }
433 err.emit();
434 }
435 CastError::NonScalar => {
436 let mut err = {
let mut err =
{
fcx.dcx().struct_span_err(self.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("non-primitive cast: `{0}` as `{1}`",
self.expr_ty, fcx.ty_to_string(self.cast_ty)))
})).with_code(E0605)
};
if self.expr_ty.references_error() { err.downgrade_to_delayed_bug(); }
err
}type_error_struct!(
437 fcx.dcx(),
438 self.span,
439 self.expr_ty,
440 E0605,
441 "non-primitive cast: `{}` as `{}`",
442 self.expr_ty,
443 fcx.ty_to_string(self.cast_ty)
444 );
445
446 if let Ok(snippet) = fcx.tcx.sess.source_map().span_to_snippet(self.expr_span)
447 && #[allow(non_exhaustive_omitted_patterns)] match self.expr.kind {
ExprKind::AddrOf(..) => true,
_ => false,
}matches!(self.expr.kind, ExprKind::AddrOf(..))
448 {
449 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("casting reference expression `{0}` because `&` binds tighter than `as`",
snippet))
})format!(
450 "casting reference expression `{}` because `&` binds tighter than `as`",
451 snippet
452 ));
453 }
454
455 let mut sugg = None;
456 let mut sugg_mutref = false;
457 if let ty::Ref(reg, cast_ty, mutbl) = *self.cast_ty.kind() {
458 if let ty::RawPtr(expr_ty, _) = *self.expr_ty.kind()
459 && fcx.may_coerce(
460 Ty::new_ref(fcx.tcx, fcx.tcx.lifetimes.re_erased, expr_ty, mutbl),
461 self.cast_ty,
462 )
463 {
464 sugg = Some((::alloc::__export::must_use({
::alloc::fmt::format(format_args!("&{0}*", mutbl.prefix_str()))
})format!("&{}*", mutbl.prefix_str()), cast_ty == expr_ty));
465 } else if let ty::Ref(expr_reg, expr_ty, expr_mutbl) = *self.expr_ty.kind()
466 && expr_mutbl == Mutability::Not
467 && mutbl == Mutability::Mut
468 && fcx.may_coerce(Ty::new_mut_ref(fcx.tcx, expr_reg, expr_ty), self.cast_ty)
469 {
470 sugg_mutref = true;
471 }
472
473 if !sugg_mutref
474 && sugg == None
475 && fcx.may_coerce(
476 Ty::new_ref(fcx.tcx, reg, self.expr_ty, mutbl),
477 self.cast_ty,
478 )
479 {
480 sugg = Some((::alloc::__export::must_use({
::alloc::fmt::format(format_args!("&{0}", mutbl.prefix_str()))
})format!("&{}", mutbl.prefix_str()), false));
481 }
482 } else if let ty::RawPtr(_, mutbl) = *self.cast_ty.kind()
483 && fcx.may_coerce(
484 Ty::new_ref(fcx.tcx, fcx.tcx.lifetimes.re_erased, self.expr_ty, mutbl),
485 self.cast_ty,
486 )
487 {
488 sugg = Some((::alloc::__export::must_use({
::alloc::fmt::format(format_args!("&{0}", mutbl.prefix_str()))
})format!("&{}", mutbl.prefix_str()), false));
489 }
490 if sugg_mutref {
491 err.span_label(self.span, "invalid cast");
492 err.span_note(self.expr_span, "this reference is immutable");
493 err.span_note(self.cast_span, "trying to cast to a mutable reference type");
494 } else if let Some((sugg, remove_cast)) = sugg {
495 err.span_label(self.span, "invalid cast");
496
497 let has_parens = fcx
498 .tcx
499 .sess
500 .source_map()
501 .span_to_snippet(self.expr_span)
502 .is_ok_and(|snip| snip.starts_with('('));
503
504 let needs_parens =
508 !has_parens && #[allow(non_exhaustive_omitted_patterns)] match self.expr.kind {
hir::ExprKind::Cast(..) => true,
_ => false,
}matches!(self.expr.kind, hir::ExprKind::Cast(..));
509
510 let mut suggestion = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(self.expr_span.shrink_to_lo(), sugg)]))vec![(self.expr_span.shrink_to_lo(), sugg)];
511 if needs_parens {
512 suggestion[0].1 += "(";
513 suggestion.push((self.expr_span.shrink_to_hi(), ")".to_string()));
514 }
515 if remove_cast {
516 suggestion.push((
517 self.expr_span.shrink_to_hi().to(self.cast_span),
518 String::new(),
519 ));
520 }
521
522 err.multipart_suggestion(
523 "consider borrowing the value",
524 suggestion,
525 Applicability::MachineApplicable,
526 );
527 } else if !#[allow(non_exhaustive_omitted_patterns)] match self.cast_ty.kind() {
ty::FnDef(..) | ty::FnPtr(..) | ty::Closure(..) => true,
_ => false,
}matches!(
528 self.cast_ty.kind(),
529 ty::FnDef(..) | ty::FnPtr(..) | ty::Closure(..)
530 ) {
531 if let Some(from_trait) = fcx.tcx.get_diagnostic_item(sym::From) {
533 let ty = fcx.resolve_vars_if_possible(self.cast_ty);
534 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
535 if fcx
536 .infcx
537 .type_implements_trait(from_trait, [ty, expr_ty], fcx.param_env)
538 .must_apply_modulo_regions()
539 {
540 let to_ty = if let ty::Adt(def, args) = self.cast_ty.kind() {
541 fcx.tcx.value_path_str_with_args(def.did(), args)
542 } else {
543 self.cast_ty.to_string()
544 };
545 err.multipart_suggestion(
546 "consider using the `From` trait instead",
547 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(self.expr_span.shrink_to_lo(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}::from(", to_ty))
})),
(self.expr_span.shrink_to_hi().to(self.cast_span),
")".to_string())]))vec![
548 (self.expr_span.shrink_to_lo(), format!("{to_ty}::from(")),
549 (
550 self.expr_span.shrink_to_hi().to(self.cast_span),
551 ")".to_string(),
552 ),
553 ],
554 Applicability::MaybeIncorrect,
555 );
556 }
557 }
558
559 let (msg, note) = if let ty::Adt(adt, _) = self.expr_ty.kind()
560 && adt.is_enum()
561 && self.cast_ty.is_numeric()
562 {
563 (
564 "an `as` expression can be used to convert enum types to numeric \
565 types only if the enum type is unit-only or field-less",
566 Some(
567 "see https://doc.rust-lang.org/reference/items/enumerations.html#casting for more information",
568 ),
569 )
570 } else {
571 (
572 "an `as` expression can only be used to convert between primitive \
573 types or to coerce to a specific trait object",
574 None,
575 )
576 };
577
578 err.span_label(self.span, msg);
579
580 if let Some(note) = note {
581 err.note(note);
582 }
583 } else {
584 err.span_label(self.span, "invalid cast");
585 }
586
587 fcx.suggest_no_capture_closure(&mut err, self.cast_ty, self.expr_ty);
588 self.try_suggest_collection_to_bool(fcx, &mut err);
589
590 err.emit();
591 }
592 CastError::SizedUnsizedCast => {
593 let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
594 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
595 fcx.dcx().emit_err(diagnostics::CastThinPointerToWidePointer {
596 span: self.span,
597 expr_ty,
598 cast_ty,
599 teach: fcx.tcx.sess.teach(E0607),
600 });
601 }
602 CastError::IntToWideCast(known_metadata) => {
603 let expr_if_nightly = fcx.tcx.sess.is_nightly_build().then_some(self.expr_span);
604 let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
605 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
606 let metadata = known_metadata.unwrap_or("type-specific metadata");
607 let known_wide = known_metadata.is_some();
608 let span = self.cast_span;
609 let param_note = (!known_wide)
610 .then(|| match cast_ty.kind() {
611 ty::RawPtr(pointee, _) => match pointee.kind() {
612 ty::Param(param) => {
613 Some(diagnostics::IntToWideParamNote { param: param.name })
614 }
615 _ => None,
616 },
617 _ => None,
618 })
619 .flatten();
620 fcx.dcx().emit_err(diagnostics::IntToWide {
621 span,
622 metadata,
623 expr_ty,
624 cast_ty,
625 expr_if_nightly,
626 known_wide,
627 param_note,
628 });
629 }
630 CastError::UnknownCastPtrKind | CastError::UnknownExprPtrKind => {
631 let unknown_cast_to = match e {
632 CastError::UnknownCastPtrKind => true,
633 CastError::UnknownExprPtrKind => false,
634 e => {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("control flow means we should never encounter a {0:?}",
e)));
}unreachable!("control flow means we should never encounter a {e:?}"),
635 };
636 let (span, sub) = if unknown_cast_to {
637 (self.cast_span, diagnostics::CastUnknownPointerSub::To(self.cast_span))
638 } else {
639 (self.cast_span, diagnostics::CastUnknownPointerSub::From(self.span))
640 };
641 fcx.dcx().emit_err(diagnostics::CastUnknownPointer {
642 span,
643 to: unknown_cast_to,
644 sub,
645 });
646 }
647 CastError::CastEnumDrop => {
648 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
649 let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
650
651 fcx.dcx().emit_err(diagnostics::CastEnumDrop { span: self.span, expr_ty, cast_ty });
652 }
653 CastError::ForeignNonExhaustiveAdt => {
654 make_invalid_casting_error(
655 self.span,
656 self.expr_ty,
657 self.cast_ty,
658 fcx,
659 )
660 .with_note("cannot cast an enum with a non-exhaustive variant when it's defined in another crate")
661 .emit();
662 }
663 CastError::PtrPtrAddingAutoTrait(added) => {
664 fcx.dcx().emit_err(diagnostics::PtrCastAddAutoToObject {
665 span: self.span,
666 traits_len: added.len(),
667 traits: {
668 let mut traits: Vec<_> = added
669 .into_iter()
670 .map(|trait_did| fcx.tcx.def_path_str(trait_did))
671 .collect();
672
673 traits.sort();
674 traits.into()
675 },
676 });
677 }
678 }
679 }
680
681 fn report_cast_to_unsized_type(&self, fcx: &FnCtxt<'a, 'tcx>) -> ErrorGuaranteed {
682 if let Err(err) = self.cast_ty.error_reported() {
683 return err;
684 }
685 if let Err(err) = self.expr_ty.error_reported() {
686 return err;
687 }
688
689 let tstr = fcx.ty_to_string(self.cast_ty);
690 let mut err = {
let mut err =
{
fcx.dcx().struct_span_err(self.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("cast to unsized type: `{0}` as `{1}`",
fcx.resolve_vars_if_possible(self.expr_ty), tstr))
})).with_code(E0620)
};
if self.expr_ty.references_error() { err.downgrade_to_delayed_bug(); }
err
}type_error_struct!(
691 fcx.dcx(),
692 self.span,
693 self.expr_ty,
694 E0620,
695 "cast to unsized type: `{}` as `{}`",
696 fcx.resolve_vars_if_possible(self.expr_ty),
697 tstr
698 );
699 match self.expr_ty.kind() {
700 ty::Ref(_, _, mt) => {
701 let mtstr = mt.prefix_str();
702 err.span_suggestion_verbose(
703 self.cast_span.shrink_to_lo(),
704 "consider casting to a reference instead",
705 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("&{0}", mtstr))
})format!("&{mtstr}"),
706 Applicability::MachineApplicable,
707 );
708 }
709 ty::Adt(def, ..) if def.is_box() => {
710 err.multipart_suggestion(
711 "you can cast to a `Box` instead",
712 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(self.cast_span.shrink_to_lo(), "Box<".to_string()),
(self.cast_span.shrink_to_hi(), ">".to_string())]))vec![
713 (self.cast_span.shrink_to_lo(), "Box<".to_string()),
714 (self.cast_span.shrink_to_hi(), ">".to_string()),
715 ],
716 Applicability::MachineApplicable,
717 );
718 }
719 _ => {
720 err.span_help(self.expr_span, "consider using a box or reference as appropriate");
721 }
722 }
723 err.emit()
724 }
725
726 fn trivial_cast_lint(&self, fcx: &FnCtxt<'a, 'tcx>) {
727 let (numeric, lint) = if self.cast_ty.is_numeric() && self.expr_ty.is_numeric() {
728 (true, lint::builtin::TRIVIAL_NUMERIC_CASTS)
729 } else {
730 (false, lint::builtin::TRIVIAL_CASTS)
731 };
732 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
733 let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
734 fcx.tcx.emit_node_span_lint(
735 lint,
736 self.expr.hir_id,
737 self.span,
738 diagnostics::TrivialCast { numeric, expr_ty, cast_ty },
739 );
740 }
741
742 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("check",
"rustc_hir_typeck::cast", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/cast.rs"),
::tracing_core::__macro_support::Option::Some(742u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::cast"),
::tracing_core::field::FieldSet::new(&["self"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&self)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: () = loop {};
return __tracing_attr_fake_return;
}
{
self.expr_ty =
fcx.structurally_resolve_type(self.expr_span, self.expr_ty);
self.cast_ty =
fcx.structurally_resolve_type(self.cast_span, self.cast_ty);
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/cast.rs:747",
"rustc_hir_typeck::cast", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/cast.rs"),
::tracing_core::__macro_support::Option::Some(747u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::cast"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("check_cast({0}, {1:?} as {2:?})",
self.expr.hir_id, self.expr_ty, self.cast_ty) as
&dyn Value))])
});
} else { ; }
};
if !fcx.type_is_sized_modulo_regions(fcx.param_env, self.cast_ty)
&& !self.cast_ty.has_infer_types() {
self.report_cast_to_unsized_type(fcx);
} else if self.expr_ty.references_error() ||
self.cast_ty.references_error()
{} else {
match self.try_coercion_cast(fcx) {
Ok(()) => {
if self.expr_ty.is_raw_ptr() && self.cast_ty.is_raw_ptr() {
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/cast.rs:765",
"rustc_hir_typeck::cast", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/cast.rs"),
::tracing_core::__macro_support::Option::Some(765u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::cast"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!(" -> PointerCast")
as &dyn Value))])
});
} else { ; }
};
} else {
self.trivial_cast_lint(fcx);
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/cast.rs:768",
"rustc_hir_typeck::cast", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/cast.rs"),
::tracing_core::__macro_support::Option::Some(768u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::cast"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!(" -> CoercionCast")
as &dyn Value))])
});
} else { ; }
};
fcx.typeck_results.borrow_mut().set_coercion_cast(self.expr.hir_id.local_id);
}
}
Err(_) => {
match self.do_check(fcx) {
Ok(k) => {
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/cast.rs:777",
"rustc_hir_typeck::cast", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/cast.rs"),
::tracing_core::__macro_support::Option::Some(777u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::cast"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!(" -> {0:?}",
k) as &dyn Value))])
});
} else { ; }
};
}
Err(e) => self.report_cast_error(fcx, e),
};
}
};
}
}
}
}#[instrument(skip(fcx), level = "debug")]
743 pub(crate) fn check(mut self, fcx: &FnCtxt<'a, 'tcx>) {
744 self.expr_ty = fcx.structurally_resolve_type(self.expr_span, self.expr_ty);
745 self.cast_ty = fcx.structurally_resolve_type(self.cast_span, self.cast_ty);
746
747 debug!("check_cast({}, {:?} as {:?})", self.expr.hir_id, self.expr_ty, self.cast_ty);
748
749 if !fcx.type_is_sized_modulo_regions(fcx.param_env, self.cast_ty)
750 && !self.cast_ty.has_infer_types()
751 {
752 self.report_cast_to_unsized_type(fcx);
753 } else if self.expr_ty.references_error() || self.cast_ty.references_error() {
754 } else {
756 match self.try_coercion_cast(fcx) {
757 Ok(()) => {
758 if self.expr_ty.is_raw_ptr() && self.cast_ty.is_raw_ptr() {
759 debug!(" -> PointerCast");
766 } else {
767 self.trivial_cast_lint(fcx);
768 debug!(" -> CoercionCast");
769 fcx.typeck_results
770 .borrow_mut()
771 .set_coercion_cast(self.expr.hir_id.local_id);
772 }
773 }
774 Err(_) => {
775 match self.do_check(fcx) {
776 Ok(k) => {
777 debug!(" -> {:?}", k);
778 }
779 Err(e) => self.report_cast_error(fcx, e),
780 };
781 }
782 };
783 }
784 }
785 fn do_check(&self, fcx: &FnCtxt<'a, 'tcx>) -> Result<CastKind, CastError<'tcx>> {
789 use rustc_middle::ty::cast::CastTy::*;
790 use rustc_middle::ty::cast::IntTy::*;
791
792 let (t_from, t_cast) = match (CastTy::from_ty(self.expr_ty), CastTy::from_ty(self.cast_ty))
793 {
794 (Some(t_from), Some(t_cast)) => (t_from, t_cast),
795 (None, Some(t_cast)) => {
797 match *self.expr_ty.kind() {
798 ty::FnDef(..) => {
799 let f = fcx.normalize(
801 self.expr_span,
802 Unnormalized::new_wip(self.expr_ty.fn_sig(fcx.tcx)),
803 );
804 let res = fcx.coerce(
805 self.expr,
806 self.expr_ty,
807 Ty::new_fn_ptr(fcx.tcx, f),
808 AllowTwoPhase::No,
809 None,
810 );
811 if let Err(TypeError::IntrinsicCast) = res {
812 return Err(CastError::IllegalCast);
813 }
814 if res.is_err() {
815 return Err(CastError::NonScalar);
816 }
817 (FnPtr, t_cast)
818 }
819 ty::Ref(_, inner_ty, mutbl) => {
824 return match t_cast {
825 Int(_) | Float => match *inner_ty.kind() {
826 ty::Int(_)
827 | ty::Uint(_)
828 | ty::Float(_)
829 | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(_)) => {
830 Err(CastError::NeedDeref)
831 }
832 _ => Err(CastError::NeedViaPtr),
833 },
834 Ptr(mt) => {
836 if !fcx.type_is_sized_modulo_regions(fcx.param_env, mt.ty) {
837 return Err(CastError::IllegalCast);
838 }
839 self.check_ref_cast(fcx, TypeAndMut { mutbl, ty: inner_ty }, mt)
840 }
841 _ => Err(CastError::NonScalar),
842 };
843 }
844 _ => return Err(CastError::NonScalar),
845 }
846 }
847 _ => return Err(CastError::NonScalar),
848 };
849 if let ty::Adt(adt_def, _) = *self.expr_ty.kind()
850 && !adt_def.did().is_local()
851 && adt_def.variants().iter().any(VariantDef::is_field_list_non_exhaustive)
852 {
853 return Err(CastError::ForeignNonExhaustiveAdt);
854 }
855 match (t_from, t_cast) {
856 (_, Int(CEnum) | FnPtr) => Err(CastError::NonScalar),
858
859 (_, Int(Bool)) => Err(CastError::CastToBool),
861
862 (Int(U(ty::UintTy::U8)), Int(Char)) => Ok(CastKind::U8CharCast), (_, Int(Char)) => Err(CastError::CastToChar),
865
866 (Int(Bool) | Int(CEnum) | Int(Char), Float) => Err(CastError::NeedViaInt),
868
869 (Int(Bool) | Int(CEnum) | Int(Char) | Float, Ptr(_)) | (Ptr(_) | FnPtr, Float) => {
870 Err(CastError::IllegalCast)
871 }
872
873 (Ptr(m_e), Ptr(m_c)) => self.check_ptr_ptr_cast(fcx, m_e, m_c), (Ptr(m_expr), Int(_)) => self.check_ptr_addr_cast(fcx, m_expr),
878
879 (FnPtr, Int(_)) => {
880 Ok(CastKind::FnPtrAddrCast)
882 }
883 (Int(_), Ptr(mt)) => self.check_addr_ptr_cast(fcx, mt),
885 (FnPtr, Ptr(mt)) => self.check_fptr_ptr_cast(fcx, mt),
887
888 (Int(CEnum), Int(_)) => self.check_enum_cast(fcx),
890
891 (Int(Char) | Int(Bool), Int(_)) => Ok(CastKind::PrimIntCast),
893
894 (Int(_) | Float, Int(_) | Float) => Ok(CastKind::NumericCast),
895 }
896 }
897
898 fn check_ptr_ptr_cast(
899 &self,
900 fcx: &FnCtxt<'a, 'tcx>,
901 m_src: ty::TypeAndMut<'tcx>,
902 m_dst: ty::TypeAndMut<'tcx>,
903 ) -> Result<CastKind, CastError<'tcx>> {
904 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/cast.rs:904",
"rustc_hir_typeck::cast", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/cast.rs"),
::tracing_core::__macro_support::Option::Some(904u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::cast"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("check_ptr_ptr_cast m_src={0:?} m_dst={1:?}",
m_src, m_dst) as &dyn Value))])
});
} else { ; }
};debug!("check_ptr_ptr_cast m_src={m_src:?} m_dst={m_dst:?}");
905 let src_kind = fcx.tcx.erase_and_anonymize_regions(fcx.pointer_kind(m_src.ty, self.span)?);
908 let dst_kind = fcx.tcx.erase_and_anonymize_regions(fcx.pointer_kind(m_dst.ty, self.span)?);
909
910 let Some(dst_kind) = dst_kind else {
912 return Err(CastError::UnknownCastPtrKind);
913 };
914
915 if dst_kind == PointerKind::Thin {
917 return Ok(CastKind::PtrPtrCast);
918 }
919
920 let Some(src_kind) = src_kind else {
922 return Err(CastError::UnknownCastPtrKind);
923 };
924
925 match (src_kind, dst_kind) {
926 (PointerKind::Thin, _) => Err(CastError::SizedUnsizedCast),
928
929 (PointerKind::VTable(src_tty), PointerKind::VTable(dst_tty)) => {
931 match (src_tty.principal(), dst_tty.principal()) {
932 (Some(src_principal), Some(_)) => {
941 let tcx = fcx.tcx;
942
943 let src_obj = Ty::new_dynamic(
951 tcx,
952 tcx.mk_poly_existential_predicates(
953 &src_tty.without_auto_traits().collect::<Vec<_>>(),
954 ),
955 tcx.lifetimes.re_erased,
956 );
957 let dst_obj = Ty::new_dynamic(
958 tcx,
959 tcx.mk_poly_existential_predicates(
960 &dst_tty.without_auto_traits().collect::<Vec<_>>(),
961 ),
962 tcx.lifetimes.re_erased,
963 );
964
965 let cause = fcx.misc(self.span);
968 if fcx
969 .at(&cause, fcx.param_env)
970 .eq(DefineOpaqueTypes::Yes, src_obj, dst_obj)
971 .map(|infer_ok| fcx.register_infer_ok_obligations(infer_ok))
972 .is_err()
973 {
974 return Err(CastError::DifferingKinds { src_kind, dst_kind });
975 }
976
977 let src_auto: FxHashSet<_> = src_tty
980 .auto_traits()
981 .chain(
982 elaborate::supertrait_def_ids(tcx, src_principal.def_id())
983 .filter(|def_id| tcx.trait_is_auto(*def_id)),
984 )
985 .collect();
986
987 let added = dst_tty
988 .auto_traits()
989 .filter(|trait_did| !src_auto.contains(trait_did))
990 .collect::<Vec<_>>();
991
992 if !added.is_empty() {
993 return Err(CastError::PtrPtrAddingAutoTrait(added));
994 }
995
996 Ok(CastKind::PtrPtrCast)
997 }
998
999 (None, None) => Ok(CastKind::PtrPtrCast),
1001
1002 (Some(_), None) => Err(CastError::DifferingKinds { src_kind, dst_kind }),
1032
1033 (None, Some(_)) => Err(CastError::DifferingKinds { src_kind, dst_kind }),
1035 }
1036 }
1037
1038 (src_kind, dst_kind) if src_kind == dst_kind => Ok(CastKind::PtrPtrCast),
1040
1041 (_, _) => Err(CastError::DifferingKinds { src_kind, dst_kind }),
1042 }
1043 }
1044
1045 fn check_fptr_ptr_cast(
1046 &self,
1047 fcx: &FnCtxt<'a, 'tcx>,
1048 m_cast: ty::TypeAndMut<'tcx>,
1049 ) -> Result<CastKind, CastError<'tcx>> {
1050 match fcx.pointer_kind(m_cast.ty, self.span)? {
1053 None => Err(CastError::UnknownCastPtrKind),
1054 Some(PointerKind::Thin) => Ok(CastKind::FnPtrPtrCast),
1055 _ => Err(CastError::IllegalCast),
1056 }
1057 }
1058
1059 fn check_ptr_addr_cast(
1060 &self,
1061 fcx: &FnCtxt<'a, 'tcx>,
1062 m_expr: ty::TypeAndMut<'tcx>,
1063 ) -> Result<CastKind, CastError<'tcx>> {
1064 match fcx.pointer_kind(m_expr.ty, self.span)? {
1067 None => Err(CastError::UnknownExprPtrKind),
1068 Some(PointerKind::Thin) => Ok(CastKind::PtrAddrCast),
1069 _ => Err(CastError::NeedViaThinPtr),
1070 }
1071 }
1072
1073 fn check_ref_cast(
1074 &self,
1075 fcx: &FnCtxt<'a, 'tcx>,
1076 mut m_expr: ty::TypeAndMut<'tcx>,
1077 mut m_cast: ty::TypeAndMut<'tcx>,
1078 ) -> Result<CastKind, CastError<'tcx>> {
1079 m_expr.ty = fcx.resolve_vars_with_obligations(m_expr.ty);
1081 m_cast.ty = fcx.resolve_vars_with_obligations(m_cast.ty);
1082
1083 if m_expr.mutbl >= m_cast.mutbl
1084 && let ty::Array(ety, _) = m_expr.ty.kind()
1085 && fcx.can_eq(fcx.param_env, *ety, m_cast.ty)
1086 {
1087 let array_ptr_type = Ty::new_ptr(fcx.tcx, m_expr.ty, m_expr.mutbl);
1092 fcx.coerce(self.expr, self.expr_ty, array_ptr_type, AllowTwoPhase::No, None)
1093 .unwrap_or_else(|_| {
1094 ::rustc_middle::util::bug::bug_fmt(format_args!("could not cast from reference to array to pointer to array ({0:?} to {1:?})",
self.expr_ty, array_ptr_type))bug!(
1095 "could not cast from reference to array to pointer to array ({:?} to {:?})",
1096 self.expr_ty,
1097 array_ptr_type,
1098 )
1099 });
1100
1101 fcx.demand_eqtype(self.span, *ety, m_cast.ty);
1103 return Ok(CastKind::ArrayPtrCast);
1104 }
1105
1106 Err(CastError::IllegalCast)
1107 }
1108
1109 fn check_addr_ptr_cast(
1110 &self,
1111 fcx: &FnCtxt<'a, 'tcx>,
1112 m_cast: TypeAndMut<'tcx>,
1113 ) -> Result<CastKind, CastError<'tcx>> {
1114 match fcx.pointer_kind(m_cast.ty, self.span)? {
1116 None => Err(CastError::UnknownCastPtrKind),
1117 Some(PointerKind::Thin) => Ok(CastKind::AddrPtrCast),
1118 Some(PointerKind::VTable(_)) => Err(CastError::IntToWideCast(Some("a vtable"))),
1119 Some(PointerKind::Length) => Err(CastError::IntToWideCast(Some("a length"))),
1120 Some(PointerKind::OfAlias(_) | PointerKind::OfParam(_)) => {
1121 Err(CastError::IntToWideCast(None))
1122 }
1123 }
1124 }
1125
1126 fn check_enum_cast(&self, fcx: &FnCtxt<'a, 'tcx>) -> Result<CastKind, CastError<'tcx>> {
1127 if let ty::Adt(d, _) = self.expr_ty.kind()
1128 && d.has_dtor(fcx.tcx)
1129 {
1130 Err(CastError::CastEnumDrop)
1131 } else {
1132 Ok(CastKind::EnumCast)
1133 }
1134 }
1135
1136 fn try_coercion_cast(&self, fcx: &FnCtxt<'a, 'tcx>) -> Result<(), ty::error::TypeError<'tcx>> {
1137 match fcx.coerce(self.expr, self.expr_ty, self.cast_ty, AllowTwoPhase::No, None) {
1138 Ok(_) => Ok(()),
1139 Err(err) => Err(err),
1140 }
1141 }
1142
1143 fn try_suggest_collection_to_bool(&self, fcx: &FnCtxt<'a, 'tcx>, err: &mut Diag<'_>) {
1146 if self.cast_ty.is_bool() {
1147 let derefed = fcx
1148 .autoderef(self.expr_span, self.expr_ty)
1149 .silence_errors()
1150 .find(|t| #[allow(non_exhaustive_omitted_patterns)] match t.0.kind() {
ty::Str | ty::Slice(..) => true,
_ => false,
}matches!(t.0.kind(), ty::Str | ty::Slice(..)));
1151
1152 if let Some((deref_ty, _)) = derefed {
1153 if deref_ty != self.expr_ty.peel_refs() {
1155 err.subdiagnostic(diagnostics::DerefImplsIsEmpty {
1156 span: self.expr_span,
1157 deref_ty,
1158 });
1159 }
1160
1161 err.subdiagnostic(diagnostics::UseIsEmpty {
1164 lo: self.expr_span.shrink_to_lo(),
1165 hi: self.span.with_lo(self.expr_span.hi()),
1166 expr_ty: self.expr_ty,
1167 });
1168 }
1169 }
1170 }
1171}