Skip to content

Warn when casting an enum that is fieldless but not C-like #92700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3067,6 +3067,7 @@ declare_lint_pass! {
DEREF_INTO_DYN_SUPERTRAIT,
DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
DUPLICATE_MACRO_ATTRIBUTES,
NOT_CENUM_CAST,
]
}

Expand Down Expand Up @@ -3633,3 +3634,43 @@ declare_lint! {
Warn,
"duplicated attribute"
}

declare_lint! {
/// The `not_cenum_cast` lint detects an `as` cast of a field-less
/// `enum` that is not C-like.
///
/// ### Example
///
/// ```rust
/// # #![allow(unused)]
/// enum E {
/// A(),
/// B{},
/// C,
/// }
///
/// fn main() {
/// let i = E::A() as u32;
/// }
/// ```
///
/// {{produces}}
///
/// ### Explanation
///
/// Historically we permit casting of enums if none of the variants have
/// fields. The intended behaviour is that only C-like (all variants are
/// unit variants) enums can be casted this way.
///
/// This is a [future-incompatible] lint to transition this to a hard error
/// in the future. See [issue #88621] for more details.
///
/// [future-incompatible]: ../index.md#future-incompatible-lints
/// [issue #88621]: https://github.com/rust-lang/rust/issues/88621
pub NOT_CENUM_CAST,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: CENUM looks a little off to me. Maybe C_ENUM would be better?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an existing lint "CENUM_IMPL_DROP_CAST" and I just followed that.

Warn,
"an enum that is not C-like is cast",
@future_incompatible = FutureIncompatibleInfo {
reference: "issue #88611 <https://github.com/rust-lang/rust/issues/88621>",
};
}
14 changes: 14 additions & 0 deletions compiler/rustc_middle/src/ty/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,20 @@ impl<'tcx> AdtDef {
self.variants.iter().all(|v| v.fields.is_empty())
}

/// Whether all variants have only constant constructors
/// (i.e. there are no tuple or struct variants).
/// This is distinct from `is_payloadfree` specifically for the case of
/// empty tuple constructors, e.g. for:
/// ```
/// enum Number {
/// Zero(),
/// }
/// ```
/// this function returns false, where `is_payloadfree` returns true.
pub fn is_c_like_enum(&self) -> bool {
self.is_enum() && self.variants.iter().all(|v| v.ctor_kind == CtorKind::Const)
}

/// Return a `VariantDef` given a variant id.
pub fn variant_with_id(&self, vid: DefId) -> &VariantDef {
self.variants.iter().find(|v| v.def_id == vid).expect("variant_with_id: unknown variant")
Expand Down
20 changes: 20 additions & 0 deletions compiler/rustc_typeck/src/check/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
// prim -> prim
(Int(CEnum), Int(_)) => {
self.cenum_impl_drop_lint(fcx);
self.not_cenum_lint(fcx);
Ok(CastKind::EnumCast)
}
(Int(Char) | Int(Bool), Int(_)) => Ok(CastKind::PrimIntCast),
Expand Down Expand Up @@ -919,6 +920,25 @@ impl<'a, 'tcx> CastCheck<'tcx> {
}
}
}

fn not_cenum_lint(&self, fcx: &FnCtxt<'a, 'tcx>) {
if let ty::Adt(d, _) = self.expr_ty.kind() {
if !d.is_c_like_enum() {
fcx.tcx.struct_span_lint_hir(
lint::builtin::NOT_CENUM_CAST,
self.expr.hir_id,
self.span,
|err| {
err.build(&format!(
"cannot cast enum `{}` into integer `{}` because it is not C-like",
self.expr_ty, self.cast_ty
))
.emit();
},
);
}
}
}
}

impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Expand Down
13 changes: 13 additions & 0 deletions src/test/ui/cast/not_cenum_cast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![deny(not_cenum_cast)]

enum E {
A(),
B{},
C,
}

fn main() {
let i = E::A() as u32;
//~^ ERROR cannot cast enum `E` into integer `u32` because it is not C-like
//~| WARN this was previously accepted
}
16 changes: 16 additions & 0 deletions src/test/ui/cast/not_cenum_cast.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error: cannot cast enum `E` into integer `u32` because it is not C-like
--> $DIR/not_cenum_cast.rs:10:13
|
LL | let i = E::A() as u32;
| ^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/not_cenum_cast.rs:1:9
|
LL | #![deny(not_cenum_cast)]
| ^^^^^^^^^^^^^^
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #88611 <https://github.com/rust-lang/rust/issues/88621>

error: aborting due to previous error