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, LocalDefId};
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::{
44 self, Ty, TyCtxt, TypeAndMut, TypeVisitableExt, Unnormalized, VariantDef, elaborate,
45};
46use rustc_middle::{bug, span_bug};
47use rustc_session::lint;
48use rustc_span::{DUMMY_SP, Span, sym};
49use rustc_trait_selection::infer::InferCtxtExt;
50use tracing::{debug, instrument};
51
52use super::FnCtxt;
53use crate::{errors, type_error_struct};
54
55#[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)]
58pub(crate) struct CastCheck<'tcx> {
59 expr: &'tcx hir::Expr<'tcx>,
61 expr_ty: Ty<'tcx>,
63 expr_span: Span,
64 cast_ty: Ty<'tcx>,
66 cast_span: Span,
67 span: Span,
68 pub body_id: LocalDefId,
69}
70
71#[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)]
75enum PointerKind<'tcx> {
76 Thin,
78 VTable(&'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>),
80 Length,
82 OfAlias(ty::AliasTy<'tcx>),
84 OfParam(ty::ParamTy),
86}
87
88impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
89 fn pointer_kind(
92 &self,
93 t: Ty<'tcx>,
94 span: Span,
95 ) -> Result<Option<PointerKind<'tcx>>, ErrorGuaranteed> {
96 {
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:96",
"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(96u32),
::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);
97
98 let t = self.resolve_vars_if_possible(t);
99 t.error_reported()?;
100
101 if self.type_is_sized_modulo_regions(self.param_env, t) {
102 return Ok(Some(PointerKind::Thin));
103 }
104
105 let t = self.resolve_vars_with_obligations(t);
106
107 Ok(match *t.kind() {
108 ty::Slice(_) | ty::Str => Some(PointerKind::Length),
109 ty::Dynamic(tty, _) => Some(PointerKind::VTable(tty)),
110 ty::Adt(def, args) if def.is_struct() => match def.non_enum_variant().tail_opt() {
111 None => Some(PointerKind::Thin),
112 Some(f) => {
113 let field_ty = self.field_ty(span, f, args);
114 self.pointer_kind(field_ty, span)?
115 }
116 },
117 ty::Tuple(fields) => match fields.last() {
118 None => Some(PointerKind::Thin),
119 Some(&f) => self.pointer_kind(f, span)?,
120 },
121
122 ty::UnsafeBinder(_) => {
::core::panicking::panic_fmt(format_args!("not yet implemented: {0}",
format_args!("FIXME(unsafe_binder)")));
}todo!("FIXME(unsafe_binder)"),
123
124 ty::Foreign(..) => Some(PointerKind::Thin),
126 ty::Alias(pi) => Some(PointerKind::OfAlias(pi)),
128 ty::Param(p) => Some(PointerKind::OfParam(p)),
129 ty::Placeholder(..) | ty::Bound(..) | ty::Infer(_) => None,
131
132 ty::Bool
133 | ty::Char
134 | ty::Int(..)
135 | ty::Uint(..)
136 | ty::Float(_)
137 | ty::Array(..)
138 | ty::CoroutineWitness(..)
139 | ty::RawPtr(_, _)
140 | ty::Ref(..)
141 | ty::Pat(..)
142 | ty::FnDef(..)
143 | ty::FnPtr(..)
144 | ty::Closure(..)
145 | ty::CoroutineClosure(..)
146 | ty::Coroutine(..)
147 | ty::Adt(..)
148 | ty::Never
149 | ty::Error(_) => {
150 let guar = self
151 .dcx()
152 .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?"));
153 return Err(guar);
154 }
155 })
156 }
157}
158
159#[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::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)]
160enum CastError<'tcx> {
161 ErrorGuaranteed(ErrorGuaranteed),
162
163 CastToBool,
164 CastToChar,
165 DifferingKinds {
166 src_kind: PointerKind<'tcx>,
167 dst_kind: PointerKind<'tcx>,
168 },
169 SizedUnsizedCast,
171 IllegalCast,
172 NeedDeref,
173 NeedViaPtr,
174 NeedViaThinPtr,
175 NeedViaInt,
176 NonScalar,
177 UnknownExprPtrKind,
178 UnknownCastPtrKind,
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 errors::CannotCastToBoolHelp::Numeric(
393 self.expr_span.shrink_to_hi().with_hi(self.span.hi()),
394 )
395 } else {
396 errors::CannotCastToBoolHelp::Unsupported(self.span)
397 };
398 fcx.dcx().emit_err(errors::CannotCastToBool { span: self.span, expr_ty, help });
399 }
400 CastError::CastToChar => {
401 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!(
402 fcx.dcx(),
403 self.span,
404 self.expr_ty,
405 E0604,
406 "only `u8` can be cast as `char`, not `{}`",
407 self.expr_ty
408 );
409 err.span_label(self.span, "invalid cast");
410 if self.expr_ty.is_numeric() {
411 if self.expr_ty == fcx.tcx.types.u32 {
412 err.multipart_suggestion(
413 "consider using `char::from_u32` instead",
414 ::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![
415 (self.expr_span.shrink_to_lo(), "char::from_u32(".to_string()),
416 (self.expr_span.shrink_to_hi().to(self.cast_span), ")".to_string()),
417 ],
418 Applicability::MachineApplicable,
419 );
420 } else if self.expr_ty == fcx.tcx.types.i8 {
421 err.span_help(self.span, "consider casting from `u8` instead");
422 } else {
423 err.span_help(
424 self.span,
425 "consider using `char::from_u32` instead (via a `u32`)",
426 );
427 };
428 }
429 err.emit();
430 }
431 CastError::NonScalar => {
432 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!(
433 fcx.dcx(),
434 self.span,
435 self.expr_ty,
436 E0605,
437 "non-primitive cast: `{}` as `{}`",
438 self.expr_ty,
439 fcx.ty_to_string(self.cast_ty)
440 );
441
442 if let Ok(snippet) = fcx.tcx.sess.source_map().span_to_snippet(self.expr_span)
443 && #[allow(non_exhaustive_omitted_patterns)] match self.expr.kind {
ExprKind::AddrOf(..) => true,
_ => false,
}matches!(self.expr.kind, ExprKind::AddrOf(..))
444 {
445 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("casting reference expression `{0}` because `&` binds tighter than `as`",
snippet))
})format!(
446 "casting reference expression `{}` because `&` binds tighter than `as`",
447 snippet
448 ));
449 }
450
451 let mut sugg = None;
452 let mut sugg_mutref = false;
453 if let ty::Ref(reg, cast_ty, mutbl) = *self.cast_ty.kind() {
454 if let ty::RawPtr(expr_ty, _) = *self.expr_ty.kind()
455 && fcx.may_coerce(
456 Ty::new_ref(fcx.tcx, fcx.tcx.lifetimes.re_erased, expr_ty, mutbl),
457 self.cast_ty,
458 )
459 {
460 sugg = Some((::alloc::__export::must_use({
::alloc::fmt::format(format_args!("&{0}*", mutbl.prefix_str()))
})format!("&{}*", mutbl.prefix_str()), cast_ty == expr_ty));
461 } else if let ty::Ref(expr_reg, expr_ty, expr_mutbl) = *self.expr_ty.kind()
462 && expr_mutbl == Mutability::Not
463 && mutbl == Mutability::Mut
464 && fcx.may_coerce(Ty::new_mut_ref(fcx.tcx, expr_reg, expr_ty), self.cast_ty)
465 {
466 sugg_mutref = true;
467 }
468
469 if !sugg_mutref
470 && sugg == None
471 && fcx.may_coerce(
472 Ty::new_ref(fcx.tcx, reg, self.expr_ty, mutbl),
473 self.cast_ty,
474 )
475 {
476 sugg = Some((::alloc::__export::must_use({
::alloc::fmt::format(format_args!("&{0}", mutbl.prefix_str()))
})format!("&{}", mutbl.prefix_str()), false));
477 }
478 } else if let ty::RawPtr(_, mutbl) = *self.cast_ty.kind()
479 && fcx.may_coerce(
480 Ty::new_ref(fcx.tcx, fcx.tcx.lifetimes.re_erased, self.expr_ty, mutbl),
481 self.cast_ty,
482 )
483 {
484 sugg = Some((::alloc::__export::must_use({
::alloc::fmt::format(format_args!("&{0}", mutbl.prefix_str()))
})format!("&{}", mutbl.prefix_str()), false));
485 }
486 if sugg_mutref {
487 err.span_label(self.span, "invalid cast");
488 err.span_note(self.expr_span, "this reference is immutable");
489 err.span_note(self.cast_span, "trying to cast to a mutable reference type");
490 } else if let Some((sugg, remove_cast)) = sugg {
491 err.span_label(self.span, "invalid cast");
492
493 let has_parens = fcx
494 .tcx
495 .sess
496 .source_map()
497 .span_to_snippet(self.expr_span)
498 .is_ok_and(|snip| snip.starts_with('('));
499
500 let needs_parens =
504 !has_parens && #[allow(non_exhaustive_omitted_patterns)] match self.expr.kind {
hir::ExprKind::Cast(..) => true,
_ => false,
}matches!(self.expr.kind, hir::ExprKind::Cast(..));
505
506 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)];
507 if needs_parens {
508 suggestion[0].1 += "(";
509 suggestion.push((self.expr_span.shrink_to_hi(), ")".to_string()));
510 }
511 if remove_cast {
512 suggestion.push((
513 self.expr_span.shrink_to_hi().to(self.cast_span),
514 String::new(),
515 ));
516 }
517
518 err.multipart_suggestion(
519 "consider borrowing the value",
520 suggestion,
521 Applicability::MachineApplicable,
522 );
523 } else if !#[allow(non_exhaustive_omitted_patterns)] match self.cast_ty.kind() {
ty::FnDef(..) | ty::FnPtr(..) | ty::Closure(..) => true,
_ => false,
}matches!(
524 self.cast_ty.kind(),
525 ty::FnDef(..) | ty::FnPtr(..) | ty::Closure(..)
526 ) {
527 if let Some(from_trait) = fcx.tcx.get_diagnostic_item(sym::From) {
529 let ty = fcx.resolve_vars_if_possible(self.cast_ty);
530 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
531 if fcx
532 .infcx
533 .type_implements_trait(from_trait, [ty, expr_ty], fcx.param_env)
534 .must_apply_modulo_regions()
535 {
536 let to_ty = if let ty::Adt(def, args) = self.cast_ty.kind() {
537 fcx.tcx.value_path_str_with_args(def.did(), args)
538 } else {
539 self.cast_ty.to_string()
540 };
541 err.multipart_suggestion(
542 "consider using the `From` trait instead",
543 ::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![
544 (self.expr_span.shrink_to_lo(), format!("{to_ty}::from(")),
545 (
546 self.expr_span.shrink_to_hi().to(self.cast_span),
547 ")".to_string(),
548 ),
549 ],
550 Applicability::MaybeIncorrect,
551 );
552 }
553 }
554
555 let (msg, note) = if let ty::Adt(adt, _) = self.expr_ty.kind()
556 && adt.is_enum()
557 && self.cast_ty.is_numeric()
558 {
559 (
560 "an `as` expression can be used to convert enum types to numeric \
561 types only if the enum type is unit-only or field-less",
562 Some(
563 "see https://doc.rust-lang.org/reference/items/enumerations.html#casting for more information",
564 ),
565 )
566 } else {
567 (
568 "an `as` expression can only be used to convert between primitive \
569 types or to coerce to a specific trait object",
570 None,
571 )
572 };
573
574 err.span_label(self.span, msg);
575
576 if let Some(note) = note {
577 err.note(note);
578 }
579 } else {
580 err.span_label(self.span, "invalid cast");
581 }
582
583 fcx.suggest_no_capture_closure(&mut err, self.cast_ty, self.expr_ty);
584 self.try_suggest_collection_to_bool(fcx, &mut err);
585
586 err.emit();
587 }
588 CastError::SizedUnsizedCast => {
589 let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
590 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
591 fcx.dcx().emit_err(errors::CastThinPointerToWidePointer {
592 span: self.span,
593 expr_ty,
594 cast_ty,
595 teach: fcx.tcx.sess.teach(E0607),
596 });
597 }
598 CastError::IntToWideCast(known_metadata) => {
599 let expr_if_nightly = fcx.tcx.sess.is_nightly_build().then_some(self.expr_span);
600 let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
601 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
602 let metadata = known_metadata.unwrap_or("type-specific metadata");
603 let known_wide = known_metadata.is_some();
604 let span = self.cast_span;
605 let param_note = (!known_wide)
606 .then(|| match cast_ty.kind() {
607 ty::RawPtr(pointee, _) => match pointee.kind() {
608 ty::Param(param) => {
609 Some(errors::IntToWideParamNote { param: param.name })
610 }
611 _ => None,
612 },
613 _ => None,
614 })
615 .flatten();
616 fcx.dcx().emit_err(errors::IntToWide {
617 span,
618 metadata,
619 expr_ty,
620 cast_ty,
621 expr_if_nightly,
622 known_wide,
623 param_note,
624 });
625 }
626 CastError::UnknownCastPtrKind | CastError::UnknownExprPtrKind => {
627 let unknown_cast_to = match e {
628 CastError::UnknownCastPtrKind => true,
629 CastError::UnknownExprPtrKind => false,
630 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:?}"),
631 };
632 let (span, sub) = if unknown_cast_to {
633 (self.cast_span, errors::CastUnknownPointerSub::To(self.cast_span))
634 } else {
635 (self.cast_span, errors::CastUnknownPointerSub::From(self.span))
636 };
637 fcx.dcx().emit_err(errors::CastUnknownPointer { span, to: unknown_cast_to, sub });
638 }
639 CastError::ForeignNonExhaustiveAdt => {
640 make_invalid_casting_error(
641 self.span,
642 self.expr_ty,
643 self.cast_ty,
644 fcx,
645 )
646 .with_note("cannot cast an enum with a non-exhaustive variant when it's defined in another crate")
647 .emit();
648 }
649 CastError::PtrPtrAddingAutoTrait(added) => {
650 fcx.dcx().emit_err(errors::PtrCastAddAutoToObject {
651 span: self.span,
652 traits_len: added.len(),
653 traits: {
654 let mut traits: Vec<_> = added
655 .into_iter()
656 .map(|trait_did| fcx.tcx.def_path_str(trait_did))
657 .collect();
658
659 traits.sort();
660 traits.into()
661 },
662 });
663 }
664 }
665 }
666
667 fn report_cast_to_unsized_type(&self, fcx: &FnCtxt<'a, 'tcx>) -> ErrorGuaranteed {
668 if let Err(err) = self.cast_ty.error_reported() {
669 return err;
670 }
671 if let Err(err) = self.expr_ty.error_reported() {
672 return err;
673 }
674
675 let tstr = fcx.ty_to_string(self.cast_ty);
676 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!(
677 fcx.dcx(),
678 self.span,
679 self.expr_ty,
680 E0620,
681 "cast to unsized type: `{}` as `{}`",
682 fcx.resolve_vars_if_possible(self.expr_ty),
683 tstr
684 );
685 match self.expr_ty.kind() {
686 ty::Ref(_, _, mt) => {
687 let mtstr = mt.prefix_str();
688 err.span_suggestion_verbose(
689 self.cast_span.shrink_to_lo(),
690 "consider casting to a reference instead",
691 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("&{0}", mtstr))
})format!("&{mtstr}"),
692 Applicability::MachineApplicable,
693 );
694 }
695 ty::Adt(def, ..) if def.is_box() => {
696 err.multipart_suggestion(
697 "you can cast to a `Box` instead",
698 ::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![
699 (self.cast_span.shrink_to_lo(), "Box<".to_string()),
700 (self.cast_span.shrink_to_hi(), ">".to_string()),
701 ],
702 Applicability::MachineApplicable,
703 );
704 }
705 _ => {
706 err.span_help(self.expr_span, "consider using a box or reference as appropriate");
707 }
708 }
709 err.emit()
710 }
711
712 fn trivial_cast_lint(&self, fcx: &FnCtxt<'a, 'tcx>) {
713 let (numeric, lint) = if self.cast_ty.is_numeric() && self.expr_ty.is_numeric() {
714 (true, lint::builtin::TRIVIAL_NUMERIC_CASTS)
715 } else {
716 (false, lint::builtin::TRIVIAL_CASTS)
717 };
718 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
719 let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
720 fcx.tcx.emit_node_span_lint(
721 lint,
722 self.expr.hir_id,
723 self.span,
724 errors::TrivialCast { numeric, expr_ty, cast_ty },
725 );
726 }
727
728 #[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(728u32),
::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:733",
"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(733u32),
::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:751",
"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(751u32),
::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:754",
"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(754u32),
::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:763",
"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(763u32),
::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")]
729 pub(crate) fn check(mut self, fcx: &FnCtxt<'a, 'tcx>) {
730 self.expr_ty = fcx.structurally_resolve_type(self.expr_span, self.expr_ty);
731 self.cast_ty = fcx.structurally_resolve_type(self.cast_span, self.cast_ty);
732
733 debug!("check_cast({}, {:?} as {:?})", self.expr.hir_id, self.expr_ty, self.cast_ty);
734
735 if !fcx.type_is_sized_modulo_regions(fcx.param_env, self.cast_ty)
736 && !self.cast_ty.has_infer_types()
737 {
738 self.report_cast_to_unsized_type(fcx);
739 } else if self.expr_ty.references_error() || self.cast_ty.references_error() {
740 } else {
742 match self.try_coercion_cast(fcx) {
743 Ok(()) => {
744 if self.expr_ty.is_raw_ptr() && self.cast_ty.is_raw_ptr() {
745 debug!(" -> PointerCast");
752 } else {
753 self.trivial_cast_lint(fcx);
754 debug!(" -> CoercionCast");
755 fcx.typeck_results
756 .borrow_mut()
757 .set_coercion_cast(self.expr.hir_id.local_id);
758 }
759 }
760 Err(_) => {
761 match self.do_check(fcx) {
762 Ok(k) => {
763 debug!(" -> {:?}", k);
764 }
765 Err(e) => self.report_cast_error(fcx, e),
766 };
767 }
768 };
769 }
770 }
771 fn do_check(&self, fcx: &FnCtxt<'a, 'tcx>) -> Result<CastKind, CastError<'tcx>> {
775 use rustc_middle::ty::cast::CastTy::*;
776 use rustc_middle::ty::cast::IntTy::*;
777
778 let (t_from, t_cast) = match (CastTy::from_ty(self.expr_ty), CastTy::from_ty(self.cast_ty))
779 {
780 (Some(t_from), Some(t_cast)) => (t_from, t_cast),
781 (None, Some(t_cast)) => {
783 match *self.expr_ty.kind() {
784 ty::FnDef(..) => {
785 let f = fcx.normalize(
787 self.expr_span,
788 Unnormalized::new_wip(self.expr_ty.fn_sig(fcx.tcx)),
789 );
790 let res = fcx.coerce(
791 self.expr,
792 self.expr_ty,
793 Ty::new_fn_ptr(fcx.tcx, f),
794 AllowTwoPhase::No,
795 None,
796 );
797 if let Err(TypeError::IntrinsicCast) = res {
798 return Err(CastError::IllegalCast);
799 }
800 if res.is_err() {
801 return Err(CastError::NonScalar);
802 }
803 (FnPtr, t_cast)
804 }
805 ty::Ref(_, inner_ty, mutbl) => {
810 return match t_cast {
811 Int(_) | Float => match *inner_ty.kind() {
812 ty::Int(_)
813 | ty::Uint(_)
814 | ty::Float(_)
815 | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(_)) => {
816 Err(CastError::NeedDeref)
817 }
818 _ => Err(CastError::NeedViaPtr),
819 },
820 Ptr(mt) => {
822 if !fcx.type_is_sized_modulo_regions(fcx.param_env, mt.ty) {
823 return Err(CastError::IllegalCast);
824 }
825 self.check_ref_cast(fcx, TypeAndMut { mutbl, ty: inner_ty }, mt)
826 }
827 _ => Err(CastError::NonScalar),
828 };
829 }
830 _ => return Err(CastError::NonScalar),
831 }
832 }
833 _ => return Err(CastError::NonScalar),
834 };
835 if let ty::Adt(adt_def, _) = *self.expr_ty.kind()
836 && !adt_def.did().is_local()
837 && adt_def.variants().iter().any(VariantDef::is_field_list_non_exhaustive)
838 {
839 return Err(CastError::ForeignNonExhaustiveAdt);
840 }
841 match (t_from, t_cast) {
842 (_, Int(CEnum) | FnPtr) => Err(CastError::NonScalar),
844
845 (_, Int(Bool)) => Err(CastError::CastToBool),
847
848 (Int(U(ty::UintTy::U8)), Int(Char)) => Ok(CastKind::U8CharCast), (_, Int(Char)) => Err(CastError::CastToChar),
851
852 (Int(Bool) | Int(CEnum) | Int(Char), Float) => Err(CastError::NeedViaInt),
854
855 (Int(Bool) | Int(CEnum) | Int(Char) | Float, Ptr(_)) | (Ptr(_) | FnPtr, Float) => {
856 Err(CastError::IllegalCast)
857 }
858
859 (Ptr(m_e), Ptr(m_c)) => self.check_ptr_ptr_cast(fcx, m_e, m_c), (Ptr(m_expr), Int(t_c)) => {
864 self.lossy_provenance_ptr2int_lint(fcx, t_c);
865 self.check_ptr_addr_cast(fcx, m_expr)
866 }
867 (FnPtr, Int(_)) => {
868 Ok(CastKind::FnPtrAddrCast)
870 }
871 (Int(_), Ptr(mt)) => {
873 self.fuzzy_provenance_int2ptr_lint(fcx);
874 self.check_addr_ptr_cast(fcx, mt)
875 }
876 (FnPtr, Ptr(mt)) => self.check_fptr_ptr_cast(fcx, mt),
878
879 (Int(CEnum), Int(_)) => {
881 self.err_if_cenum_impl_drop(fcx);
882 Ok(CastKind::EnumCast)
883 }
884 (Int(Char) | Int(Bool), Int(_)) => Ok(CastKind::PrimIntCast),
885
886 (Int(_) | Float, Int(_) | Float) => Ok(CastKind::NumericCast),
887 }
888 }
889
890 fn check_ptr_ptr_cast(
891 &self,
892 fcx: &FnCtxt<'a, 'tcx>,
893 m_src: ty::TypeAndMut<'tcx>,
894 m_dst: ty::TypeAndMut<'tcx>,
895 ) -> Result<CastKind, CastError<'tcx>> {
896 {
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:896",
"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(896u32),
::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:?}");
897 let src_kind = fcx.tcx.erase_and_anonymize_regions(fcx.pointer_kind(m_src.ty, self.span)?);
900 let dst_kind = fcx.tcx.erase_and_anonymize_regions(fcx.pointer_kind(m_dst.ty, self.span)?);
901
902 let Some(dst_kind) = dst_kind else {
904 return Err(CastError::UnknownCastPtrKind);
905 };
906
907 if dst_kind == PointerKind::Thin {
909 return Ok(CastKind::PtrPtrCast);
910 }
911
912 let Some(src_kind) = src_kind else {
914 return Err(CastError::UnknownCastPtrKind);
915 };
916
917 match (src_kind, dst_kind) {
918 (PointerKind::Thin, _) => Err(CastError::SizedUnsizedCast),
920
921 (PointerKind::VTable(src_tty), PointerKind::VTable(dst_tty)) => {
923 match (src_tty.principal(), dst_tty.principal()) {
924 (Some(src_principal), Some(_)) => {
933 let tcx = fcx.tcx;
934
935 let src_obj = Ty::new_dynamic(
943 tcx,
944 tcx.mk_poly_existential_predicates(
945 &src_tty.without_auto_traits().collect::<Vec<_>>(),
946 ),
947 tcx.lifetimes.re_erased,
948 );
949 let dst_obj = Ty::new_dynamic(
950 tcx,
951 tcx.mk_poly_existential_predicates(
952 &dst_tty.without_auto_traits().collect::<Vec<_>>(),
953 ),
954 tcx.lifetimes.re_erased,
955 );
956
957 let cause = fcx.misc(self.span);
960 if fcx
961 .at(&cause, fcx.param_env)
962 .eq(DefineOpaqueTypes::Yes, src_obj, dst_obj)
963 .map(|infer_ok| fcx.register_infer_ok_obligations(infer_ok))
964 .is_err()
965 {
966 return Err(CastError::DifferingKinds { src_kind, dst_kind });
967 }
968
969 let src_auto: FxHashSet<_> = src_tty
972 .auto_traits()
973 .chain(
974 elaborate::supertrait_def_ids(tcx, src_principal.def_id())
975 .filter(|def_id| tcx.trait_is_auto(*def_id)),
976 )
977 .collect();
978
979 let added = dst_tty
980 .auto_traits()
981 .filter(|trait_did| !src_auto.contains(trait_did))
982 .collect::<Vec<_>>();
983
984 if !added.is_empty() {
985 return Err(CastError::PtrPtrAddingAutoTrait(added));
986 }
987
988 Ok(CastKind::PtrPtrCast)
989 }
990
991 (None, None) => Ok(CastKind::PtrPtrCast),
993
994 (Some(_), None) => Err(CastError::DifferingKinds { src_kind, dst_kind }),
1024
1025 (None, Some(_)) => Err(CastError::DifferingKinds { src_kind, dst_kind }),
1027 }
1028 }
1029
1030 (src_kind, dst_kind) if src_kind == dst_kind => Ok(CastKind::PtrPtrCast),
1032
1033 (_, _) => Err(CastError::DifferingKinds { src_kind, dst_kind }),
1034 }
1035 }
1036
1037 fn check_fptr_ptr_cast(
1038 &self,
1039 fcx: &FnCtxt<'a, 'tcx>,
1040 m_cast: ty::TypeAndMut<'tcx>,
1041 ) -> Result<CastKind, CastError<'tcx>> {
1042 match fcx.pointer_kind(m_cast.ty, self.span)? {
1045 None => Err(CastError::UnknownCastPtrKind),
1046 Some(PointerKind::Thin) => Ok(CastKind::FnPtrPtrCast),
1047 _ => Err(CastError::IllegalCast),
1048 }
1049 }
1050
1051 fn check_ptr_addr_cast(
1052 &self,
1053 fcx: &FnCtxt<'a, 'tcx>,
1054 m_expr: ty::TypeAndMut<'tcx>,
1055 ) -> Result<CastKind, CastError<'tcx>> {
1056 match fcx.pointer_kind(m_expr.ty, self.span)? {
1059 None => Err(CastError::UnknownExprPtrKind),
1060 Some(PointerKind::Thin) => Ok(CastKind::PtrAddrCast),
1061 _ => Err(CastError::NeedViaThinPtr),
1062 }
1063 }
1064
1065 fn check_ref_cast(
1066 &self,
1067 fcx: &FnCtxt<'a, 'tcx>,
1068 mut m_expr: ty::TypeAndMut<'tcx>,
1069 mut m_cast: ty::TypeAndMut<'tcx>,
1070 ) -> Result<CastKind, CastError<'tcx>> {
1071 m_expr.ty = fcx.resolve_vars_with_obligations(m_expr.ty);
1073 m_cast.ty = fcx.resolve_vars_with_obligations(m_cast.ty);
1074
1075 if m_expr.mutbl >= m_cast.mutbl
1076 && let ty::Array(ety, _) = m_expr.ty.kind()
1077 && fcx.can_eq(fcx.param_env, *ety, m_cast.ty)
1078 {
1079 let array_ptr_type = Ty::new_ptr(fcx.tcx, m_expr.ty, m_expr.mutbl);
1084 fcx.coerce(self.expr, self.expr_ty, array_ptr_type, AllowTwoPhase::No, None)
1085 .unwrap_or_else(|_| {
1086 ::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!(
1087 "could not cast from reference to array to pointer to array ({:?} to {:?})",
1088 self.expr_ty,
1089 array_ptr_type,
1090 )
1091 });
1092
1093 fcx.demand_eqtype(self.span, *ety, m_cast.ty);
1095 return Ok(CastKind::ArrayPtrCast);
1096 }
1097
1098 Err(CastError::IllegalCast)
1099 }
1100
1101 fn check_addr_ptr_cast(
1102 &self,
1103 fcx: &FnCtxt<'a, 'tcx>,
1104 m_cast: TypeAndMut<'tcx>,
1105 ) -> Result<CastKind, CastError<'tcx>> {
1106 match fcx.pointer_kind(m_cast.ty, self.span)? {
1108 None => Err(CastError::UnknownCastPtrKind),
1109 Some(PointerKind::Thin) => Ok(CastKind::AddrPtrCast),
1110 Some(PointerKind::VTable(_)) => Err(CastError::IntToWideCast(Some("a vtable"))),
1111 Some(PointerKind::Length) => Err(CastError::IntToWideCast(Some("a length"))),
1112 Some(PointerKind::OfAlias(_) | PointerKind::OfParam(_)) => {
1113 Err(CastError::IntToWideCast(None))
1114 }
1115 }
1116 }
1117
1118 fn try_coercion_cast(&self, fcx: &FnCtxt<'a, 'tcx>) -> Result<(), ty::error::TypeError<'tcx>> {
1119 match fcx.coerce(self.expr, self.expr_ty, self.cast_ty, AllowTwoPhase::No, None) {
1120 Ok(_) => Ok(()),
1121 Err(err) => Err(err),
1122 }
1123 }
1124
1125 fn err_if_cenum_impl_drop(&self, fcx: &FnCtxt<'a, 'tcx>) {
1126 if let ty::Adt(d, _) = self.expr_ty.kind()
1127 && d.has_dtor(fcx.tcx)
1128 {
1129 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
1130 let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
1131
1132 fcx.dcx().emit_err(errors::CastEnumDrop { span: self.span, expr_ty, cast_ty });
1133 }
1134 }
1135
1136 fn lossy_provenance_ptr2int_lint(&self, fcx: &FnCtxt<'a, 'tcx>, t_c: ty::cast::IntTy) {
1137 let expr_prec = fcx.precedence(self.expr);
1138 let needs_parens = expr_prec < ExprPrecedence::Unambiguous;
1139
1140 let needs_cast = !#[allow(non_exhaustive_omitted_patterns)] match t_c {
ty::cast::IntTy::U(ty::UintTy::Usize) => true,
_ => false,
}matches!(t_c, ty::cast::IntTy::U(ty::UintTy::Usize));
1141 let cast_span = self.expr_span.shrink_to_hi().to(self.cast_span);
1142 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
1143 let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
1144 let expr_span = self.expr_span.shrink_to_lo();
1145 let sugg = match (needs_parens, needs_cast) {
1146 (true, true) => errors::LossyProvenancePtr2IntSuggestion::NeedsParensCast {
1147 expr_span,
1148 cast_span,
1149 cast_ty,
1150 },
1151 (true, false) => {
1152 errors::LossyProvenancePtr2IntSuggestion::NeedsParens { expr_span, cast_span }
1153 }
1154 (false, true) => {
1155 errors::LossyProvenancePtr2IntSuggestion::NeedsCast { cast_span, cast_ty }
1156 }
1157 (false, false) => errors::LossyProvenancePtr2IntSuggestion::Other { cast_span },
1158 };
1159
1160 let lint = errors::LossyProvenancePtr2Int { expr_ty, cast_ty, sugg };
1161 fcx.tcx.emit_node_span_lint(
1162 lint::builtin::LOSSY_PROVENANCE_CASTS,
1163 self.expr.hir_id,
1164 self.span,
1165 lint,
1166 );
1167 }
1168
1169 fn fuzzy_provenance_int2ptr_lint(&self, fcx: &FnCtxt<'a, 'tcx>) {
1170 let sugg = errors::LossyProvenanceInt2PtrSuggestion {
1171 lo: self.expr_span.shrink_to_lo(),
1172 hi: self.expr_span.shrink_to_hi().to(self.cast_span),
1173 };
1174 let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
1175 let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
1176 let lint = errors::LossyProvenanceInt2Ptr { expr_ty, cast_ty, sugg };
1177 fcx.tcx.emit_node_span_lint(
1178 lint::builtin::FUZZY_PROVENANCE_CASTS,
1179 self.expr.hir_id,
1180 self.span,
1181 lint,
1182 );
1183 }
1184
1185 fn try_suggest_collection_to_bool(&self, fcx: &FnCtxt<'a, 'tcx>, err: &mut Diag<'_>) {
1188 if self.cast_ty.is_bool() {
1189 let derefed = fcx
1190 .autoderef(self.expr_span, self.expr_ty)
1191 .silence_errors()
1192 .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(..)));
1193
1194 if let Some((deref_ty, _)) = derefed {
1195 if deref_ty != self.expr_ty.peel_refs() {
1197 err.subdiagnostic(errors::DerefImplsIsEmpty { span: self.expr_span, deref_ty });
1198 }
1199
1200 err.subdiagnostic(errors::UseIsEmpty {
1203 lo: self.expr_span.shrink_to_lo(),
1204 hi: self.span.with_lo(self.expr_span.hi()),
1205 expr_ty: self.expr_ty,
1206 });
1207 }
1208 }
1209 }
1210}