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

Make specifying repr optional for fieldless enums #88203

Closed
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
15 changes: 9 additions & 6 deletions compiler/rustc_error_codes/src/error_codes/E0732.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ Erroneous code example:

```compile_fail,E0732
enum Enum { // error!
Unit = 1,
Tuple() = 2,
Struct{} = 3,
Unit = 3,
Tuple(u16) = 2,
Struct {
a: u8,
b: u16,
} = 1,
}
# fn main() {}
```

A `#[repr(inttype)]` must be provided on an `enum` if it has a non-unit
variant with a discriminant, or where there are both unit variants with
discriminants and non-unit variants. This restriction ensures that there
A `#[repr(inttype)]` must be provided on an `enum` if it not fieldless and
has a discriminant, or where there are both fieldless variants with
discriminants and variants that have fields. This restriction ensures that there
is a well-defined way to extract a variant's discriminant from a value;
for instance:

Expand Down
13 changes: 9 additions & 4 deletions compiler/rustc_typeck/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1384,12 +1384,17 @@ fn check_enum<'tcx>(
}

if tcx.adt_def(def_id).repr.int.is_none() {
let is_unit = |var: &hir::Variant<'_>| matches!(var.data, hir::VariantData::Unit(..));
let is_fieldless = |var: &hir::Variant<'_>| match var.data {
hir::VariantData::Unit(_) => true,
hir::VariantData::Tuple(fields, _) | hir::VariantData::Struct(fields, _) => {
fields.is_empty()
}
};

let has_disr = |var: &hir::Variant<'_>| var.disr_expr.is_some();
let has_non_units = vs.iter().any(|var| !is_unit(var));
let disr_units = vs.iter().any(|var| is_unit(&var) && has_disr(&var));
let disr_non_unit = vs.iter().any(|var| !is_unit(&var) && has_disr(&var));
let has_non_units = vs.iter().any(|var| !is_fieldless(var));
let disr_units = vs.iter().any(|var| is_fieldless(&var) && has_disr(&var));
let disr_non_unit = vs.iter().any(|var| !is_fieldless(&var) && has_disr(&var));

if disr_non_unit || (disr_units && has_non_units) {
let mut err =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#![crate_type="lib"]

#![crate_type = "lib"]
enum Enum {
//~^ ERROR `#[repr(inttype)]` must be specified
Unit = 1,
Tuple() = 2,
Struct{} = 3,
//~^ ERROR `#[repr(inttype)]` must be specified
Unit = 5,
Tuple(u8) = 3,
Struct {
foo: u16
} = 1,
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
error[E0732]: `#[repr(inttype)]` must be specified
--> $DIR/arbitrary_enum_discriminant-no-repr.rs:3:1
--> $DIR/arbitrary_enum_discriminant-no-repr.rs:2:1
|
LL | / enum Enum {
LL | |
LL | | Unit = 1,
LL | | Tuple() = 2,
LL | | Struct{} = 3,
LL | | Unit = 5,
LL | | Tuple(u8) = 3,
... |
LL | | } = 1,
LL | | }
| |_^

Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/enum-discriminant/fieldless-no-repr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// check-pass
#![crate_type="lib"]

enum Enum {
Unit = 1,
Tuple() = 2,
Struct{} = 3,
}