Migrate 'casting unknown pointer' diagnostic

This commit is contained in:
clubby789 2023-09-15 16:28:55 +00:00
parent 94920cc6e0
commit 82471e9f6c
3 changed files with 56 additions and 23 deletions

View File

@ -21,6 +21,14 @@ hir_typeck_cannot_cast_to_bool = cannot cast `{$expr_ty}` as `bool`
.help = compare with zero instead .help = compare with zero instead
.label = unsupported cast .label = unsupported cast
hir_typeck_cast_unknown_pointer = cannot cast {$to ->
[true] to
*[false] from
} a pointer of an unknown kind
.label_to = needs more type information
.note = the type information given here is insufficient to check whether the pointer cast is valid
.label_from = the type information given here is insufficient to check whether the pointer cast is valid
hir_typeck_const_select_must_be_const = this argument must be a `const fn` hir_typeck_const_select_must_be_const = this argument must be a `const fn`
.help = consult the documentation on `const_eval_select` for more information .help = consult the documentation on `const_eval_select` for more information

View File

@ -33,9 +33,7 @@
use crate::errors; use crate::errors;
use crate::type_error_struct; use crate::type_error_struct;
use hir::ExprKind; use hir::ExprKind;
use rustc_errors::{ use rustc_errors::{Applicability, DelayDm, Diagnostic, DiagnosticBuilder, ErrorGuaranteed};
struct_span_err, Applicability, DelayDm, Diagnostic, DiagnosticBuilder, ErrorGuaranteed,
};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_macros::{TypeFoldable, TypeVisitable}; use rustc_macros::{TypeFoldable, TypeVisitable};
use rustc_middle::mir::Mutability; use rustc_middle::mir::Mutability;
@ -540,27 +538,16 @@ fn report_cast_error(&self, fcx: &FnCtxt<'a, 'tcx>, e: CastError) {
CastError::UnknownExprPtrKind => false, CastError::UnknownExprPtrKind => false,
_ => bug!(), _ => bug!(),
}; };
let mut err = struct_span_err!( let (span, sub) = if unknown_cast_to {
fcx.tcx.sess, (self.cast_span, errors::CastUnknownPointerSub::To(self.cast_span))
if unknown_cast_to { self.cast_span } else { self.span },
E0641,
"cannot cast {} a pointer of an unknown kind",
if unknown_cast_to { "to" } else { "from" }
);
if unknown_cast_to {
err.span_label(self.cast_span, "needs more type information");
err.note(
"the type information given here is insufficient to check whether \
the pointer cast is valid",
);
} else { } else {
err.span_label( (self.cast_span, errors::CastUnknownPointerSub::From(self.span))
self.span, };
"the type information given here is insufficient to check whether \ fcx.tcx.sess.emit_err(errors::CastUnknownPointer {
the pointer cast is valid", span,
); to: unknown_cast_to,
} sub,
err.emit(); });
} }
CastError::ForeignNonExhaustiveAdt => { CastError::ForeignNonExhaustiveAdt => {
make_invalid_casting_error( make_invalid_casting_error(

View File

@ -565,6 +565,44 @@ pub struct CannotCastToBool<'tcx> {
pub help: CannotCastToBoolHelp, pub help: CannotCastToBoolHelp,
} }
#[derive(Diagnostic)]
#[diag(hir_typeck_cast_unknown_pointer, code = "E0641")]
pub struct CastUnknownPointer {
#[primary_span]
pub span: Span,
pub to: bool,
#[subdiagnostic]
pub sub: CastUnknownPointerSub,
}
pub enum CastUnknownPointerSub {
To(Span),
From(Span),
}
impl rustc_errors::AddToDiagnostic for CastUnknownPointerSub {
fn add_to_diagnostic_with<F>(self, diag: &mut rustc_errors::Diagnostic, f: F)
where
F: Fn(
&mut Diagnostic,
rustc_errors::SubdiagnosticMessage,
) -> rustc_errors::SubdiagnosticMessage,
{
match self {
CastUnknownPointerSub::To(span) => {
let msg = f(diag, crate::fluent_generated::hir_typeck_label_to.into());
diag.span_label(span, msg);
let msg = f(diag, crate::fluent_generated::hir_typeck_note.into());
diag.note(msg);
}
CastUnknownPointerSub::From(span) => {
let msg = f(diag, crate::fluent_generated::hir_typeck_label_from.into());
diag.span_label(span, msg);
}
}
}
}
#[derive(Subdiagnostic)] #[derive(Subdiagnostic)]
pub enum CannotCastToBoolHelp { pub enum CannotCastToBoolHelp {
#[suggestion( #[suggestion(