about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
author许杰友 Jieyou Xu (Joe) <39484203+jieyouxu@users.noreply.github.com>2025-02-05 19:09:33 +0800
committerGitHub <noreply@github.com>2025-02-05 19:09:33 +0800
commitdef44600d1882a14272f138d86aaf8002516b105 (patch)
treef423ae9d1747a7c175747189552564096f9b6fc8 /compiler
parentf75146fc1635d12f9886c5a24b62288fab93476e (diff)
parente0bbeb7a005bbf593ccae60f4b0ba86f17d3df63 (diff)
downloadrust-def44600d1882a14272f138d86aaf8002516b105.tar.gz
rust-def44600d1882a14272f138d86aaf8002516b105.zip
Rollup merge of #135964 - ehuss:cenum_impl_drop_cast, r=Nadrieril
Make cenum_impl_drop_cast a hard error

This changes the `cenum_impl_drop_cast` lint to be a hard error. This lint has been deny-by-default and warning in dependencies since https://github.com/rust-lang/rust/pull/97652 about 2.5 years ago.

Closes https://github.com/rust-lang/rust/issues/73333
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_hir_typeck/src/cast.rs11
-rw-r--r--compiler/rustc_hir_typeck/src/errors.rs4
-rw-r--r--compiler/rustc_lint/src/lib.rs5
-rw-r--r--compiler/rustc_lint_defs/src/builtin.rs53
4 files changed, 11 insertions, 62 deletions
diff --git a/compiler/rustc_hir_typeck/src/cast.rs b/compiler/rustc_hir_typeck/src/cast.rs
index 5d00ecbe918..65021a0cd11 100644
--- a/compiler/rustc_hir_typeck/src/cast.rs
+++ b/compiler/rustc_hir_typeck/src/cast.rs
@@ -831,7 +831,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
 
             // prim -> prim
             (Int(CEnum), Int(_)) => {
-                self.cenum_impl_drop_lint(fcx);
+                self.err_if_cenum_impl_drop(fcx);
                 Ok(CastKind::EnumCast)
             }
             (Int(Char) | Int(Bool), Int(_)) => Ok(CastKind::PrimIntCast),
@@ -1091,19 +1091,14 @@ impl<'a, 'tcx> CastCheck<'tcx> {
         }
     }
 
-    fn cenum_impl_drop_lint(&self, fcx: &FnCtxt<'a, 'tcx>) {
+    fn err_if_cenum_impl_drop(&self, fcx: &FnCtxt<'a, 'tcx>) {
         if let ty::Adt(d, _) = self.expr_ty.kind()
             && d.has_dtor(fcx.tcx)
         {
             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_node_span_lint(
-                lint::builtin::CENUM_IMPL_DROP_CAST,
-                self.expr.hir_id,
-                self.span,
-                errors::CastEnumDrop { expr_ty, cast_ty },
-            );
+            fcx.dcx().emit_err(errors::CastEnumDrop { span: self.span, expr_ty, cast_ty });
         }
     }
 
diff --git a/compiler/rustc_hir_typeck/src/errors.rs b/compiler/rustc_hir_typeck/src/errors.rs
index 052adaa69b2..143736072aa 100644
--- a/compiler/rustc_hir_typeck/src/errors.rs
+++ b/compiler/rustc_hir_typeck/src/errors.rs
@@ -677,9 +677,11 @@ pub(crate) struct CannotCastToBool<'tcx> {
     pub help: CannotCastToBoolHelp,
 }
 
-#[derive(LintDiagnostic)]
+#[derive(Diagnostic)]
 #[diag(hir_typeck_cast_enum_drop)]
 pub(crate) struct CastEnumDrop<'tcx> {
+    #[primary_span]
+    pub span: Span,
     pub expr_ty: Ty<'tcx>,
     pub cast_ty: Ty<'tcx>,
 }
diff --git a/compiler/rustc_lint/src/lib.rs b/compiler/rustc_lint/src/lib.rs
index 83a168c3f44..fd6c17735c5 100644
--- a/compiler/rustc_lint/src/lib.rs
+++ b/compiler/rustc_lint/src/lib.rs
@@ -595,6 +595,11 @@ fn register_builtins(store: &mut LintStore) {
          <https://github.com/rust-lang/rust/pull/125380> for more information",
     );
     store.register_removed("unsupported_calling_conventions", "converted into hard error");
+    store.register_removed(
+        "cenum_impl_drop_cast",
+        "converted into hard error, \
+         see <https://github.com/rust-lang/rust/issues/73333> for more information",
+    );
 }
 
 fn register_internals(store: &mut LintStore) {
diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs
index 5b4a1f174cb..9561b3de5f8 100644
--- a/compiler/rustc_lint_defs/src/builtin.rs
+++ b/compiler/rustc_lint_defs/src/builtin.rs
@@ -27,7 +27,6 @@ declare_lint_pass! {
         BARE_TRAIT_OBJECTS,
         BINDINGS_WITH_VARIANT_NAME,
         BREAK_WITH_LABEL_AND_LOOP,
-        CENUM_IMPL_DROP_CAST,
         COHERENCE_LEAK_CHECK,
         CONFLICTING_REPR_HINTS,
         CONST_EVALUATABLE_UNCHECKED,
@@ -2613,58 +2612,6 @@ declare_lint! {
 }
 
 declare_lint! {
-    /// The `cenum_impl_drop_cast` lint detects an `as` cast of a field-less
-    /// `enum` that implements [`Drop`].
-    ///
-    /// [`Drop`]: https://doc.rust-lang.org/std/ops/trait.Drop.html
-    ///
-    /// ### Example
-    ///
-    /// ```rust,compile_fail
-    /// # #![allow(unused)]
-    /// enum E {
-    ///     A,
-    /// }
-    ///
-    /// impl Drop for E {
-    ///     fn drop(&mut self) {
-    ///         println!("Drop");
-    ///     }
-    /// }
-    ///
-    /// fn main() {
-    ///     let e = E::A;
-    ///     let i = e as u32;
-    /// }
-    /// ```
-    ///
-    /// {{produces}}
-    ///
-    /// ### Explanation
-    ///
-    /// Casting a field-less `enum` that does not implement [`Copy`] to an
-    /// integer moves the value without calling `drop`. This can result in
-    /// surprising behavior if it was expected that `drop` should be called.
-    /// Calling `drop` automatically would be inconsistent with other move
-    /// operations. Since neither behavior is clear or consistent, it was
-    /// decided that a cast of this nature will no longer be allowed.
-    ///
-    /// This is a [future-incompatible] lint to transition this to a hard error
-    /// in the future. See [issue #73333] for more details.
-    ///
-    /// [future-incompatible]: ../index.md#future-incompatible-lints
-    /// [issue #73333]: https://github.com/rust-lang/rust/issues/73333
-    /// [`Copy`]: https://doc.rust-lang.org/std/marker/trait.Copy.html
-    pub CENUM_IMPL_DROP_CAST,
-    Deny,
-    "a C-like enum implementing Drop is cast",
-    @future_incompatible = FutureIncompatibleInfo {
-        reason: FutureIncompatibilityReason::FutureReleaseErrorReportInDeps,
-        reference: "issue #73333 <https://github.com/rust-lang/rust/issues/73333>",
-    };
-}
-
-declare_lint! {
     /// The `fuzzy_provenance_casts` lint detects an `as` cast between an integer
     /// and a pointer.
     ///