Skip to content

Commit 8dcdba1

Browse files
committed
Migrate 'casting unknown pointer' diagnostic
1 parent 1372e2b commit 8dcdba1

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