Migrate 'cast enum with drop to int' diagnostic

This commit is contained in:
clubby789 2023-09-15 16:48:25 +00:00
parent 80a9699117
commit 9c5de75ce1
3 changed files with 18 additions and 9 deletions

View File

@ -21,6 +21,8 @@ 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_enum_drop = cannot cast enum `{$expr_ty}` into integer `{$cast_ty}` because it implements `Drop`
hir_typeck_cast_unknown_pointer = cannot cast {$to -> hir_typeck_cast_unknown_pointer = cannot cast {$to ->
[true] to [true] to
*[false] from *[false] from

View File

@ -33,7 +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::{Applicability, DelayDm, Diagnostic, DiagnosticBuilder, ErrorGuaranteed}; use rustc_errors::{Applicability, 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;
@ -935,17 +935,17 @@ fn cenum_impl_drop_lint(&self, fcx: &FnCtxt<'a, 'tcx>) {
if let ty::Adt(d, _) = self.expr_ty.kind() if let ty::Adt(d, _) = self.expr_ty.kind()
&& d.has_dtor(fcx.tcx) && d.has_dtor(fcx.tcx)
{ {
fcx.tcx.struct_span_lint_hir( let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
fcx.tcx.emit_spanned_lint(
lint::builtin::CENUM_IMPL_DROP_CAST, lint::builtin::CENUM_IMPL_DROP_CAST,
self.expr.hir_id, self.expr.hir_id,
self.span, self.span,
DelayDm(|| format!( errors::CastEnumDrop {
"cannot cast enum `{}` into integer `{}` because it implements `Drop`", expr_ty,
self.expr_ty, self.cast_ty cast_ty,
)), }
|lint| {
lint
},
); );
} }
} }

View File

@ -574,6 +574,13 @@ pub struct CannotCastToBool<'tcx> {
pub help: CannotCastToBoolHelp, pub help: CannotCastToBoolHelp,
} }
#[derive(LintDiagnostic)]
#[diag(hir_typeck_cast_enum_drop)]
pub struct CastEnumDrop<'tcx> {
pub expr_ty: Ty<'tcx>,
pub cast_ty: Ty<'tcx>,
}
#[derive(Diagnostic)] #[derive(Diagnostic)]
#[diag(hir_typeck_cast_unknown_pointer, code = "E0641")] #[diag(hir_typeck_cast_unknown_pointer, code = "E0641")]
pub struct CastUnknownPointer { pub struct CastUnknownPointer {