Skip to content

Commit cb8f360

Browse files
committed
Migrate 'casting unknown pointer' diagnostic
1 parent f485a9f commit cb8f360

File tree

3 files changed

+56
-23
lines changed

3 files changed

+56
-23
lines changed

compiler/rustc_hir_typeck/messages.ftl

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ hir_typeck_cannot_cast_to_bool = cannot cast `{$expr_ty}` as `bool`
2121
.help = compare with zero instead
2222
.label = unsupported cast
2323
24+
hir_typeck_cast_unknown_pointer = cannot cast {$to ->
25+
[true] to
26+
*[false] from
27+
} a pointer of an unknown kind
28+
.label_to = needs more type information
29+
.note = the type information given here is insufficient to check whether the pointer cast is valid
30+
.label_from = the type information given here is insufficient to check whether the pointer cast is valid
31+
2432
hir_typeck_const_select_must_be_const = this argument must be a `const fn`
2533
.help = consult the documentation on `const_eval_select` for more information
2634

compiler/rustc_hir_typeck/src/cast.rs

+10-23
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ use super::FnCtxt;
3333
use crate::errors;
3434
use crate::type_error_struct;
3535
use hir::ExprKind;
36-
use rustc_errors::{
37-
struct_span_err, Applicability, DelayDm, Diagnostic, DiagnosticBuilder, ErrorGuaranteed,
38-
};
36+
use rustc_errors::{Applicability, DelayDm, Diagnostic, DiagnosticBuilder, ErrorGuaranteed};
3937
use rustc_hir as hir;
4038
use rustc_macros::{TypeFoldable, TypeVisitable};
4139
use rustc_middle::mir::Mutability;
@@ -544,27 +542,16 @@ impl<'a, 'tcx> CastCheck<'tcx> {
544542
CastError::UnknownExprPtrKind => false,
545543
_ => bug!(),
546544
};
547-
let mut err = struct_span_err!(
548-
fcx.tcx.sess,
549-
if unknown_cast_to { self.cast_span } else { self.span },
550-
E0641,
551-
"cannot cast {} a pointer of an unknown kind",
552-
if unknown_cast_to { "to" } else { "from" }
553-
);
554-
if unknown_cast_to {
555-
err.span_label(self.cast_span, "needs more type information");
556-
err.note(
557-
"the type information given here is insufficient to check whether \
558-
the pointer cast is valid",
559-
);
545+
let (span, sub) = if unknown_cast_to {
546+
(self.cast_span, errors::CastUnknownPointerSub::To(self.cast_span))
560547
} else {
561-
err.span_label(
562-
self.span,
563-
"the type information given here is insufficient to check whether \
564-
the pointer cast is valid",
565-
);
566-
}
567-
err.emit();
548+
(self.cast_span, errors::CastUnknownPointerSub::From(self.span))
549+
};
550+
fcx.tcx.sess.emit_err(errors::CastUnknownPointer {
551+
span,
552+
to: unknown_cast_to,
553+
sub,
554+
});
568555
}
569556
CastError::ForeignNonExhaustiveAdt => {
570557
make_invalid_casting_error(

compiler/rustc_hir_typeck/src/errors.rs

+38
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,44 @@ pub struct CannotCastToBool<'tcx> {
519519
pub help: CannotCastToBoolHelp,
520520
}
521521

522+
#[derive(Diagnostic)]
523+
#[diag(hir_typeck_cast_unknown_pointer, code = "E0641")]
524+
pub struct CastUnknownPointer {
525+
#[primary_span]
526+
pub span: Span,
527+
pub to: bool,
528+
#[subdiagnostic]
529+
pub sub: CastUnknownPointerSub,
530+
}
531+
532+
pub enum CastUnknownPointerSub {
533+
To(Span),
534+
From(Span),
535+
}
536+
537+
impl rustc_errors::AddToDiagnostic for CastUnknownPointerSub {
538+
fn add_to_diagnostic_with<F>(self, diag: &mut rustc_errors::Diagnostic, f: F)
539+
where
540+
F: Fn(
541+
&mut Diagnostic,
542+
rustc_errors::SubdiagnosticMessage,
543+
) -> rustc_errors::SubdiagnosticMessage,
544+
{
545+
match self {
546+
CastUnknownPointerSub::To(span) => {
547+
let msg = f(diag, crate::fluent_generated::hir_typeck_label_to.into());
548+
diag.span_label(span, msg);
549+
let msg = f(diag, crate::fluent_generated::hir_typeck_note.into());
550+
diag.note(msg);
551+
}
552+
CastUnknownPointerSub::From(span) => {
553+
let msg = f(diag, crate::fluent_generated::hir_typeck_label_from.into());
554+
diag.span_label(span, msg);
555+
}
556+
}
557+
}
558+
}
559+
522560
#[derive(Subdiagnostic)]
523561
pub enum CannotCastToBoolHelp {
524562
#[suggestion(

0 commit comments

Comments
 (0)