Skip to content

Commit 9c5de75

Browse files
committed
Migrate 'cast enum with drop to int' diagnostic
1 parent 80a9699 commit 9c5de75

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

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

compiler/rustc_hir_typeck/src/cast.rs

+9-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;
@@ -935,17 +935,17 @@ impl<'a, 'tcx> CastCheck<'tcx> {
935935
if let ty::Adt(d, _) = self.expr_ty.kind()
936936
&& d.has_dtor(fcx.tcx)
937937
{
938-
fcx.tcx.struct_span_lint_hir(
938+
let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
939+
let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
940+
941+
fcx.tcx.emit_spanned_lint(
939942
lint::builtin::CENUM_IMPL_DROP_CAST,
940943
self.expr.hir_id,
941944
self.span,
942-
DelayDm(|| format!(
943-
"cannot cast enum `{}` into integer `{}` because it implements `Drop`",
944-
self.expr_ty, self.cast_ty
945-
)),
946-
|lint| {
947-
lint
948-
},
945+
errors::CastEnumDrop {
946+
expr_ty,
947+
cast_ty,
948+
}
949949
);
950950
}
951951
}

compiler/rustc_hir_typeck/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,13 @@ pub struct CannotCastToBool<'tcx> {
574574
pub help: CannotCastToBoolHelp,
575575
}
576576

577+
#[derive(LintDiagnostic)]
578+
#[diag(hir_typeck_cast_enum_drop)]
579+
pub struct CastEnumDrop<'tcx> {
580+
pub expr_ty: Ty<'tcx>,
581+
pub cast_ty: Ty<'tcx>,
582+
}
583+
577584
#[derive(Diagnostic)]
578585
#[diag(hir_typeck_cast_unknown_pointer, code = "E0641")]
579586
pub struct CastUnknownPointer {

0 commit comments

Comments
 (0)