Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6cc1998

Browse files
authoredFeb 6, 2025
Unrolled build for rust-lang#135964
Rollup merge of rust-lang#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 rust-lang#97652 about 2.5 years ago. Closes rust-lang#73333
2 parents a9730c3 + e0bbeb7 commit 6cc1998

File tree

8 files changed

+12
-181
lines changed

8 files changed

+12
-181
lines changed
 

‎compiler/rustc_hir_typeck/src/cast.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
831831

832832
// prim -> prim
833833
(Int(CEnum), Int(_)) => {
834-
self.cenum_impl_drop_lint(fcx);
834+
self.err_if_cenum_impl_drop(fcx);
835835
Ok(CastKind::EnumCast)
836836
}
837837
(Int(Char) | Int(Bool), Int(_)) => Ok(CastKind::PrimIntCast),
@@ -1091,19 +1091,14 @@ impl<'a, 'tcx> CastCheck<'tcx> {
10911091
}
10921092
}
10931093

1094-
fn cenum_impl_drop_lint(&self, fcx: &FnCtxt<'a, 'tcx>) {
1094+
fn err_if_cenum_impl_drop(&self, fcx: &FnCtxt<'a, 'tcx>) {
10951095
if let ty::Adt(d, _) = self.expr_ty.kind()
10961096
&& d.has_dtor(fcx.tcx)
10971097
{
10981098
let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
10991099
let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
11001100

1101-
fcx.tcx.emit_node_span_lint(
1102-
lint::builtin::CENUM_IMPL_DROP_CAST,
1103-
self.expr.hir_id,
1104-
self.span,
1105-
errors::CastEnumDrop { expr_ty, cast_ty },
1106-
);
1101+
fcx.dcx().emit_err(errors::CastEnumDrop { span: self.span, expr_ty, cast_ty });
11071102
}
11081103
}
11091104

‎compiler/rustc_hir_typeck/src/errors.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -677,9 +677,11 @@ pub(crate) struct CannotCastToBool<'tcx> {
677677
pub help: CannotCastToBoolHelp,
678678
}
679679

680-
#[derive(LintDiagnostic)]
680+
#[derive(Diagnostic)]
681681
#[diag(hir_typeck_cast_enum_drop)]
682682
pub(crate) struct CastEnumDrop<'tcx> {
683+
#[primary_span]
684+
pub span: Span,
683685
pub expr_ty: Ty<'tcx>,
684686
pub cast_ty: Ty<'tcx>,
685687
}

‎compiler/rustc_lint/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,11 @@ fn register_builtins(store: &mut LintStore) {
595595
<https://github.com/rust-lang/rust/pull/125380> for more information",
596596
);
597597
store.register_removed("unsupported_calling_conventions", "converted into hard error");
598+
store.register_removed(
599+
"cenum_impl_drop_cast",
600+
"converted into hard error, \
601+
see <https://github.com/rust-lang/rust/issues/73333> for more information",
602+
);
598603
}
599604

600605
fn register_internals(store: &mut LintStore) {

‎compiler/rustc_lint_defs/src/builtin.rs

-53
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ declare_lint_pass! {
2727
BARE_TRAIT_OBJECTS,
2828
BINDINGS_WITH_VARIANT_NAME,
2929
BREAK_WITH_LABEL_AND_LOOP,
30-
CENUM_IMPL_DROP_CAST,
3130
COHERENCE_LEAK_CHECK,
3231
CONFLICTING_REPR_HINTS,
3332
CONST_EVALUATABLE_UNCHECKED,
@@ -2612,58 +2611,6 @@ declare_lint! {
26122611
@edition Edition2024 => Warn;
26132612
}
26142613

2615-
declare_lint! {
2616-
/// The `cenum_impl_drop_cast` lint detects an `as` cast of a field-less
2617-
/// `enum` that implements [`Drop`].
2618-
///
2619-
/// [`Drop`]: https://doc.rust-lang.org/std/ops/trait.Drop.html
2620-
///
2621-
/// ### Example
2622-
///
2623-
/// ```rust,compile_fail
2624-
/// # #![allow(unused)]
2625-
/// enum E {
2626-
/// A,
2627-
/// }
2628-
///
2629-
/// impl Drop for E {
2630-
/// fn drop(&mut self) {
2631-
/// println!("Drop");
2632-
/// }
2633-
/// }
2634-
///
2635-
/// fn main() {
2636-
/// let e = E::A;
2637-
/// let i = e as u32;
2638-
/// }
2639-
/// ```
2640-
///
2641-
/// {{produces}}
2642-
///
2643-
/// ### Explanation
2644-
///
2645-
/// Casting a field-less `enum` that does not implement [`Copy`] to an
2646-
/// integer moves the value without calling `drop`. This can result in
2647-
/// surprising behavior if it was expected that `drop` should be called.
2648-
/// Calling `drop` automatically would be inconsistent with other move
2649-
/// operations. Since neither behavior is clear or consistent, it was
2650-
/// decided that a cast of this nature will no longer be allowed.
2651-
///
2652-
/// This is a [future-incompatible] lint to transition this to a hard error
2653-
/// in the future. See [issue #73333] for more details.
2654-
///
2655-
/// [future-incompatible]: ../index.md#future-incompatible-lints
2656-
/// [issue #73333]: https://github.com/rust-lang/rust/issues/73333
2657-
/// [`Copy`]: https://doc.rust-lang.org/std/marker/trait.Copy.html
2658-
pub CENUM_IMPL_DROP_CAST,
2659-
Deny,
2660-
"a C-like enum implementing Drop is cast",
2661-
@future_incompatible = FutureIncompatibleInfo {
2662-
reason: FutureIncompatibilityReason::FutureReleaseErrorReportInDeps,
2663-
reference: "issue #73333 <https://github.com/rust-lang/rust/issues/73333>",
2664-
};
2665-
}
2666-
26672614
declare_lint! {
26682615
/// The `fuzzy_provenance_casts` lint detects an `as` cast between an integer
26692616
/// and a pointer.

‎tests/mir-opt/building/enum_cast.droppy.built.after.mir

-71
This file was deleted.

‎tests/mir-opt/building/enum_cast.rs

-21
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,6 @@ fn far(far: Far) -> isize {
4141
far as isize
4242
}
4343

44-
// EMIT_MIR enum_cast.droppy.built.after.mir
45-
enum Droppy {
46-
A,
47-
B,
48-
C,
49-
}
50-
51-
impl Drop for Droppy {
52-
fn drop(&mut self) {}
53-
}
54-
55-
fn droppy() {
56-
{
57-
let x = Droppy::C;
58-
// remove this entire test once `cenum_impl_drop_cast` becomes a hard error
59-
#[allow(cenum_impl_drop_cast)]
60-
let y = x as usize;
61-
}
62-
let z = Droppy::B;
63-
}
64-
6544
#[repr(i16)]
6645
enum SignedAroundZero {
6746
A = -2,

‎tests/ui/cenum_impl_drop_cast.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![deny(cenum_impl_drop_cast)]
2-
31
enum E {
42
A = 0,
53
}
@@ -14,5 +12,4 @@ fn main() {
1412
let e = E::A;
1513
let i = e as u32;
1614
//~^ ERROR cannot cast enum `E` into integer `u32` because it implements `Drop`
17-
//~| WARN this was previously accepted
1815
}

‎tests/ui/cenum_impl_drop_cast.stderr

+1-24
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,8 @@
11
error: cannot cast enum `E` into integer `u32` because it implements `Drop`
2-
--> $DIR/cenum_impl_drop_cast.rs:15:13
2+
--> $DIR/cenum_impl_drop_cast.rs:13:13
33
|
44
LL | let i = e as u32;
55
| ^^^^^^^^
6-
|
7-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
8-
= note: for more information, see issue #73333 <https://github.com/rust-lang/rust/issues/73333>
9-
note: the lint level is defined here
10-
--> $DIR/cenum_impl_drop_cast.rs:1:9
11-
|
12-
LL | #![deny(cenum_impl_drop_cast)]
13-
| ^^^^^^^^^^^^^^^^^^^^
146

157
error: aborting due to 1 previous error
168

17-
Future incompatibility report: Future breakage diagnostic:
18-
error: cannot cast enum `E` into integer `u32` because it implements `Drop`
19-
--> $DIR/cenum_impl_drop_cast.rs:15:13
20-
|
21-
LL | let i = e as u32;
22-
| ^^^^^^^^
23-
|
24-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
25-
= note: for more information, see issue #73333 <https://github.com/rust-lang/rust/issues/73333>
26-
note: the lint level is defined here
27-
--> $DIR/cenum_impl_drop_cast.rs:1:9
28-
|
29-
LL | #![deny(cenum_impl_drop_cast)]
30-
| ^^^^^^^^^^^^^^^^^^^^
31-

0 commit comments

Comments
 (0)
Failed to load comments.