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