Skip to content

Commit 1372e2b

Browse files
committed
Migrate 'int to fat pointer' cast diagnostic
1 parent d5c1d48 commit 1372e2b

File tree

3 files changed

+39
-28
lines changed

3 files changed

+39
-28
lines changed

compiler/rustc_hir_typeck/messages.ftl

+8
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ hir_typeck_functional_record_update_on_non_struct =
5959
6060
hir_typeck_help_set_edition_cargo = set `edition = "{$edition}"` in `Cargo.toml`
6161
hir_typeck_help_set_edition_standalone = pass `--edition {$edition}` to `rustc`
62+
63+
hir_typeck_int_to_fat = cannot cast `{$expr_ty}` to a pointer that {$known_wide ->
64+
[true] is
65+
*[false] may be
66+
} wide
67+
hir_typeck_int_to_fat_label = creating a `{$cast_ty}` requires both an address and {$metadata}
68+
hir_typeck_int_to_fat_label_nightly = consider casting this expression to `*const ()`, then using `core::ptr::from_raw_parts`
69+
6270
hir_typeck_lang_start_expected_sig_note = the `start` lang item should have the signature `fn(fn() -> T, isize, *const *const u8, u8) -> isize`
6371
6472
hir_typeck_lang_start_incorrect_number_params = incorrect number of parameters for the `start` lang item

compiler/rustc_hir_typeck/src/cast.rs

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

compiler/rustc_hir_typeck/src/errors.rs

+14
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,20 @@ impl HelpUseLatestEdition {
315315
}
316316
}
317317

318+
#[derive(Diagnostic)]
319+
#[diag(hir_typeck_int_to_fat, code = "E0606")]
320+
pub struct IntToWide<'tcx> {
321+
#[primary_span]
322+
#[label(hir_typeck_int_to_fat_label)]
323+
pub span: Span,
324+
pub metadata: &'tcx str,
325+
pub expr_ty: String,
326+
pub cast_ty: Ty<'tcx>,
327+
#[label(hir_typeck_int_to_fat_label_nightly)]
328+
pub expr_if_nightly: Option<Span>,
329+
pub known_wide: bool,
330+
}
331+
318332
#[derive(Subdiagnostic)]
319333
pub enum OptionResultRefMismatch {
320334
#[suggestion(

0 commit comments

Comments
 (0)