Skip to content

Commit 82471e9

Browse files
committed
Migrate 'casting unknown pointer' diagnostic
1 parent 94920cc commit 82471e9

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;
@@ -540,27 +538,16 @@ impl<'a, 'tcx> CastCheck<'tcx> {
540538
CastError::UnknownExprPtrKind => false,
541539
_ => bug!(),
542540
};
543-
let mut err = struct_span_err!(
544-
fcx.tcx.sess,
545-
if unknown_cast_to { self.cast_span } else { self.span },
546-
E0641,
547-
"cannot cast {} a pointer of an unknown kind",
548-
if unknown_cast_to { "to" } else { "from" }
549-
);
550-
if unknown_cast_to {
551-
err.span_label(self.cast_span, "needs more type information");
552-
err.note(
553-
"the type information given here is insufficient to check whether \
554-
the pointer cast is valid",
555-
);
541+
let (span, sub) = if unknown_cast_to {
542+
(self.cast_span, errors::CastUnknownPointerSub::To(self.cast_span))
556543
} else {
557-
err.span_label(
558-
self.span,
559-
"the type information given here is insufficient to check whether \
560-
the pointer cast is valid",
561-
);
562-
}
563-
err.emit();
544+
(self.cast_span, errors::CastUnknownPointerSub::From(self.span))
545+
};
546+
fcx.tcx.sess.emit_err(errors::CastUnknownPointer {
547+
span,
548+
to: unknown_cast_to,
549+
sub,
550+
});
564551
}
565552
CastError::ForeignNonExhaustiveAdt => {
566553
make_invalid_casting_error(

compiler/rustc_hir_typeck/src/errors.rs

+38
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,44 @@ pub struct CannotCastToBool<'tcx> {
565565
pub help: CannotCastToBoolHelp,
566566
}
567567

568+
#[derive(Diagnostic)]
569+
#[diag(hir_typeck_cast_unknown_pointer, code = "E0641")]
570+
pub struct CastUnknownPointer {
571+
#[primary_span]
572+
pub span: Span,
573+
pub to: bool,
574+
#[subdiagnostic]
575+
pub sub: CastUnknownPointerSub,
576+
}
577+
578+
pub enum CastUnknownPointerSub {
579+
To(Span),
580+
From(Span),
581+
}
582+
583+
impl rustc_errors::AddToDiagnostic for CastUnknownPointerSub {
584+
fn add_to_diagnostic_with<F>(self, diag: &mut rustc_errors::Diagnostic, f: F)
585+
where
586+
F: Fn(
587+
&mut Diagnostic,
588+
rustc_errors::SubdiagnosticMessage,
589+
) -> rustc_errors::SubdiagnosticMessage,
590+
{
591+
match self {
592+
CastUnknownPointerSub::To(span) => {
593+
let msg = f(diag, crate::fluent_generated::hir_typeck_label_to.into());
594+
diag.span_label(span, msg);
595+
let msg = f(diag, crate::fluent_generated::hir_typeck_note.into());
596+
diag.note(msg);
597+
}
598+
CastUnknownPointerSub::From(span) => {
599+
let msg = f(diag, crate::fluent_generated::hir_typeck_label_from.into());
600+
diag.span_label(span, msg);
601+
}
602+
}
603+
}
604+
}
605+
568606
#[derive(Subdiagnostic)]
569607
pub enum CannotCastToBoolHelp {
570608
#[suggestion(

0 commit comments

Comments
 (0)