From 4809a13172ed4fd079cf181a3274b0b8417e22b8 Mon Sep 17 00:00:00 2001 From: Piotr Czarnecki Date: Sun, 28 Sep 2014 21:55:42 +0100 Subject: [PATCH 1/3] Proposal for C-like nested enums and their representation --- active/0000-repr-nested-enums.md | 69 ++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 active/0000-repr-nested-enums.md diff --git a/active/0000-repr-nested-enums.md b/active/0000-repr-nested-enums.md new file mode 100644 index 00000000000..6f27e7b31dd --- /dev/null +++ b/active/0000-repr-nested-enums.md @@ -0,0 +1,69 @@ +- Start Date: (fill me in with today's date, YYYY-MM-DD) +- RFC PR: (leave this empty) +- Rust Issue: (leave this empty) + +# Summary + +Allow enums that cannot contain values other than discriminants and C-like +enums to have an integer representation. Currently one can cast variants of +only simple enums to integers. + +# Motivation + +With this feature implemented, we can have better control over C-like enums: + +```rust +// exception.rs +#[repr(u8)] +pub enum Fault { + DivideError = 0, + NMI = 2, + Breakpoint = 3, + // ... +} + +// interrupt.rs +#[repr(u8)] // < this is allowed +pub enum Interrupt { + Fault(Fault), + IRQ0 = 32, + IRQ1 = 33, + IRQ2 = 34, + Syscall = 0x80 +} +``` + +Moreover, teepee's status module could be refactored by removing the +`StatusClass` enum. + +# Detailed design + +An enum is C-like if all its variants belong to two groups: + +* variants that have empty bodies +* variants that contain exactly one value that is a C-like enum + +Remove the restriction that variants of such nested enums can't have +explicitly assigned discriminants. + +Use the error message `discriminant value already exists [E0081]` to disallow +conflicting values. The set of values of a C-like enum type is the union of +the set of all values it contains, and the set of its empty-bodied values. + +# Drawbacks + +* Adds some trans code that is not trivial. +* Yet another feature that might not be future proof. + +# Alternatives + +* This could be approached as a general enum discriminant optimization. + However, further improvements shouldn't be nearly as much of a language + change as this one and are deemed to be an implementation detail out of + scope of this RFC. +* Some kind of subtyping and variant inheritance is possible. That is, all + possible values could be accessible under one namespace. + +# Unresolved questions + +Should these enums be C-like by default, even without `repr`? From 4ac4af4204c3db0dc75238f61a7103860f730ea3 Mon Sep 17 00:00:00 2001 From: Piotr Czarnecki Date: Mon, 29 Sep 2014 15:19:04 +0100 Subject: [PATCH 2/3] Clarification --- active/0000-repr-nested-enums.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/active/0000-repr-nested-enums.md b/active/0000-repr-nested-enums.md index 6f27e7b31dd..c74dd7024fb 100644 --- a/active/0000-repr-nested-enums.md +++ b/active/0000-repr-nested-enums.md @@ -41,13 +41,14 @@ Moreover, teepee's status module could be refactored by removing the An enum is C-like if all its variants belong to two groups: * variants that have empty bodies -* variants that contain exactly one value that is a C-like enum +* variants that contain exactly one member which is a C-like enum -Remove the restriction that variants of such nested enums can't have -explicitly assigned discriminants. +Allow the use non-empty variant bodies and explicitly assigned discriminants +in a single C-like enum. Use the error message `discriminant value already exists [E0081]` to disallow -conflicting values. The set of values of a C-like enum type is the union of +conflicting values in enums that have an integer representation mandated by +the `repr` attribute. The set of values of a C-like enum type is the union of the set of all values it contains, and the set of its empty-bodied values. # Drawbacks From e8a651059e5f5b5b1d9791ee8269dfceeee367e0 Mon Sep 17 00:00:00 2001 From: Piotr Czarnecki Date: Mon, 29 Sep 2014 15:21:16 +0100 Subject: [PATCH 3/3] Added an alternative --- active/0000-repr-nested-enums.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/active/0000-repr-nested-enums.md b/active/0000-repr-nested-enums.md index c74dd7024fb..27b3ce324c3 100644 --- a/active/0000-repr-nested-enums.md +++ b/active/0000-repr-nested-enums.md @@ -58,6 +58,8 @@ the set of all values it contains, and the set of its empty-bodied values. # Alternatives +* Allow C-like enum variants that contain integers smaller than the + representation of the enum. (Perhaps with only one such member per enum.) * This could be approached as a general enum discriminant optimization. However, further improvements shouldn't be nearly as much of a language change as this one and are deemed to be an implementation detail out of