Skip to content
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

Disallow non-c-like but "fieldless" ADTs from being casted to integer if they use arbitrary enum discriminant #89234

Merged
merged 1 commit into from
Dec 2, 2021
Merged
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
18 changes: 17 additions & 1 deletion compiler/rustc_middle/src/ty/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_errors::ErrorReported;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def::{CtorKind, DefKind, Res};
use rustc_hir::def_id::DefId;
use rustc_index::vec::{Idx, IndexVec};
use rustc_query_system::ich::StableHashingContext;
Expand Down Expand Up @@ -314,6 +314,22 @@ impl<'tcx> AdtDef {
/// Whether the ADT lacks fields. Note that this includes uninhabited enums,
/// e.g., `enum Void {}` is considered payload free as well.
pub fn is_payloadfree(&self) -> bool {
// Treat the ADT as not payload-free if arbitrary_enum_discriminant is used (#88621).
// This would disallow the following kind of enum from being casted into integer.
// ```
// enum Enum {
// Foo() = 1,
// Bar{} = 2,
// Baz = 3,
// }
// ```
if self
.variants
.iter()
.any(|v| matches!(v.discr, VariantDiscr::Explicit(_)) && v.ctor_kind != CtorKind::Const)
{
return false;
}
self.variants.iter().all(|v| v.fields.is_empty())
}

Expand Down
13 changes: 13 additions & 0 deletions src/test/ui/cast/issue-88621.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![feature(arbitrary_enum_discriminant)]

#[repr(u8)]
enum Kind2 {
Foo() = 1,
Bar{} = 2,
Baz = 3,
}

fn main() {
let _ = Kind2::Foo() as u8;
//~^ ERROR non-primitive cast
}
9 changes: 9 additions & 0 deletions src/test/ui/cast/issue-88621.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0605]: non-primitive cast: `Kind2` as `u8`
--> $DIR/issue-88621.rs:11:13
|
LL | let _ = Kind2::Foo() as u8;
| ^^^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object

error: aborting due to previous error

For more information about this error, try `rustc --explain E0605`.
13 changes: 0 additions & 13 deletions src/test/ui/enum-discriminant/arbitrary_enum_discriminant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ impl Enum {
}
}

#[allow(dead_code)]
#[repr(u8)]
enum FieldlessEnum {
Unit = 3,
Tuple() = 2,
Struct {} = 1,
}

fn main() {
const UNIT: Enum = Enum::Unit;
const TUPLE: Enum = Enum::Tuple(5);
Expand All @@ -48,9 +40,4 @@ fn main() {
assert_eq!(3, UNIT_TAG);
assert_eq!(2, TUPLE_TAG);
assert_eq!(1, STRUCT_TAG);

// Ensure `as` conversions are correct
assert_eq!(3, FieldlessEnum::Unit as u8);
assert_eq!(2, FieldlessEnum::Tuple() as u8);
assert_eq!(1, FieldlessEnum::Struct{} as u8);
}