Skip to content

Commit 94920cc

Browse files
committed
Migrate 'int to fat pointer' cast diagnostic
1 parent c2841e2 commit 94920cc

File tree

3 files changed

+38
-28
lines changed

3 files changed

+38
-28
lines changed

compiler/rustc_hir_typeck/messages.ftl

+7
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ hir_typeck_functional_record_update_on_non_struct =
6464
hir_typeck_help_set_edition_cargo = set `edition = "{$edition}"` in `Cargo.toml`
6565
hir_typeck_help_set_edition_standalone = pass `--edition {$edition}` to `rustc`
6666
67+
hir_typeck_int_to_fat = cannot cast `{$expr_ty}` to a pointer that {$known_wide ->
68+
[true] is
69+
*[false] may be
70+
} wide
71+
hir_typeck_int_to_fat_label = creating a `{$cast_ty}` requires both an address and {$metadata}
72+
hir_typeck_int_to_fat_label_nightly = consider casting this expression to `*const ()`, then using `core::ptr::from_raw_parts`
73+
6774
hir_typeck_invalid_callee = expected function, found {$ty}
6875
6976
hir_typeck_lang_start_expected_sig_note = the `start` lang item should have the signature `fn(fn() -> T, isize, *const *const u8, u8) -> isize`

compiler/rustc_hir_typeck/src/cast.rs

+17-28
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,9 @@ impl<'a, 'tcx> CastCheck<'tcx> {
324324
CastError::CastToBool => {
325325
let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
326326
let help = if self.expr_ty.is_numeric() {
327-
errors::CannotCastToBoolHelp::Numeric(self.expr_span.shrink_to_hi().with_hi(self.span.hi()))
327+
errors::CannotCastToBoolHelp::Numeric(
328+
self.expr_span.shrink_to_hi().with_hi(self.span.hi()),
329+
)
328330
} else {
329331
errors::CannotCastToBoolHelp::Unsupported(self.span)
330332
};
@@ -517,33 +519,20 @@ impl<'a, 'tcx> CastCheck<'tcx> {
517519
.emit();
518520
}
519521
CastError::IntToFatCast(known_metadata) => {
520-
let mut err = struct_span_err!(
521-
fcx.tcx.sess,
522-
self.cast_span,
523-
E0606,
524-
"cannot cast `{}` to a pointer that {} wide",
525-
fcx.ty_to_string(self.expr_ty),
526-
if known_metadata.is_some() { "is" } else { "may be" }
527-
);
528-
529-
err.span_label(
530-
self.cast_span,
531-
format!(
532-
"creating a `{}` requires both an address and {}",
533-
self.cast_ty,
534-
known_metadata.unwrap_or("type-specific metadata"),
535-
),
536-
);
537-
538-
if fcx.tcx.sess.is_nightly_build() {
539-
err.span_label(
540-
self.expr_span,
541-
"consider casting this expression to `*const ()`, \
542-
then using `core::ptr::from_raw_parts`",
543-
);
544-
}
545-
546-
err.emit();
522+
let expr_if_nightly = fcx.tcx.sess.is_nightly_build().then_some(self.expr_span);
523+
let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
524+
let expr_ty = fcx.ty_to_string(self.expr_ty);
525+
let metadata = known_metadata.unwrap_or("type-specific metadata");
526+
let known_wide = known_metadata.is_some();
527+
let span = self.cast_span;
528+
fcx.tcx.sess.emit_err(errors::IntToWide {
529+
span,
530+
metadata,
531+
expr_ty,
532+
cast_ty,
533+
expr_if_nightly,
534+
known_wide,
535+
});
547536
}
548537
CastError::UnknownCastPtrKind | CastError::UnknownExprPtrKind => {
549538
let unknown_cast_to = match e {

compiler/rustc_hir_typeck/src/errors.rs

+14
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,20 @@ pub struct InvalidCallee {
361361
pub ty: String,
362362
}
363363

364+
#[derive(Diagnostic)]
365+
#[diag(hir_typeck_int_to_fat, code = "E0606")]
366+
pub struct IntToWide<'tcx> {
367+
#[primary_span]
368+
#[label(hir_typeck_int_to_fat_label)]
369+
pub span: Span,
370+
pub metadata: &'tcx str,
371+
pub expr_ty: String,
372+
pub cast_ty: Ty<'tcx>,
373+
#[label(hir_typeck_int_to_fat_label_nightly)]
374+
pub expr_if_nightly: Option<Span>,
375+
pub known_wide: bool,
376+
}
377+
364378
#[derive(Subdiagnostic)]
365379
pub enum OptionResultRefMismatch {
366380
#[suggestion(

0 commit comments

Comments
 (0)