Skip to content

Commit 6e6abb3

Browse files
committed
Migrate 'cast enum with drop to int' diagnostic
1 parent 78cfc8d commit 6e6abb3

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;
@@ -939,17 +939,17 @@ impl<'a, 'tcx> CastCheck<'tcx> {
939939
if let ty::Adt(d, _) = self.expr_ty.kind()
940940
&& d.has_dtor(fcx.tcx)
941941
{
942-
fcx.tcx.struct_span_lint_hir(
942+
let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
943+
let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
944+
945+
fcx.tcx.emit_spanned_lint(
943946
lint::builtin::CENUM_IMPL_DROP_CAST,
944947
self.expr.hir_id,
945948
self.span,
946-
DelayDm(|| format!(
947-
"cannot cast enum `{}` into integer `{}` because it implements `Drop`",
948-
self.expr_ty, self.cast_ty
949-
)),
950-
|lint| {
951-
lint
952-
},
949+
errors::CastEnumDrop {
950+
expr_ty,
951+
cast_ty,
952+
}
953953
);
954954
}
955955
}

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)