Skip to content

Commit 229b668

Browse files
committed
Migrate 'cast enum with drop to int' diagnostic
1 parent d55485b commit 229b668

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

Diff for: compiler/rustc_hir_typeck/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ 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_enum_drop = cannot cast enum `{$expr_ty}` into integer `{$cast_ty}` because it implements `Drop`
25+
2426
hir_typeck_cast_unknown_pointer = cannot cast {$to ->
2527
[true] to
2628
*[false] from

Diff for: compiler/rustc_hir_typeck/src/cast.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use super::FnCtxt;
3333
use crate::errors;
3434
use crate::type_error_struct;
3535
use hir::ExprKind;
36-
use rustc_errors::{Applicability, DelayDm, Diagnostic, DiagnosticBuilder, ErrorGuaranteed};
36+
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed};
3737
use rustc_hir as hir;
3838
use rustc_macros::{TypeFoldable, TypeVisitable};
3939
use rustc_middle::mir::Mutability;
@@ -936,17 +936,14 @@ impl<'a, 'tcx> CastCheck<'tcx> {
936936
if let ty::Adt(d, _) = self.expr_ty.kind()
937937
&& d.has_dtor(fcx.tcx)
938938
{
939-
fcx.tcx.struct_span_lint_hir(
939+
fcx.tcx.emit_spanned_lint(
940940
lint::builtin::CENUM_IMPL_DROP_CAST,
941941
self.expr.hir_id,
942942
self.span,
943-
DelayDm(|| format!(
944-
"cannot cast enum `{}` into integer `{}` because it implements `Drop`",
945-
self.expr_ty, self.cast_ty
946-
)),
947-
|lint| {
948-
lint
949-
},
943+
errors::CastEnumDrop {
944+
expr_ty: self.expr_ty,
945+
cast_ty: self.cast_ty,
946+
}
950947
);
951948
}
952949
}

Diff for: compiler/rustc_hir_typeck/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,13 @@ pub struct CannotCastToBool<'tcx> {
528528
pub help: CannotCastToBoolHelp,
529529
}
530530

531+
#[derive(LintDiagnostic)]
532+
#[diag(hir_typeck_cast_enum_drop)]
533+
pub struct CastEnumDrop<'tcx> {
534+
pub expr_ty: Ty<'tcx>,
535+
pub cast_ty: Ty<'tcx>,
536+
}
537+
531538
#[derive(Diagnostic)]
532539
#[diag(hir_typeck_cast_unknown_pointer, code = "E0641")]
533540
pub struct CastUnknownPointer {

0 commit comments

Comments
 (0)