-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement arbitrary_enum_discriminant
- Loading branch information
Showing
18 changed files
with
328 additions
and
55 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/doc/unstable-book/src/language-features/arbitrary-enum-discriminant.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# `arbitrary_enum_discriminant` | ||
|
||
The tracking issue for this feature is: [#60553] | ||
|
||
[#60553]: https://github.com/rust-lang/rust/issues/60553 | ||
|
||
------------------------ | ||
|
||
The `arbitrary_enum_discriminant` feature permits tuple-like and | ||
struct-like enum variants with `#[repr(<int-type>)]` to have explicit discriminants. | ||
|
||
## Examples | ||
|
||
```rust | ||
#![feature(arbitrary_enum_discriminant)] | ||
|
||
#[allow(dead_code)] | ||
#[repr(u8)] | ||
enum Enum { | ||
Unit = 3, | ||
Tuple(u16) = 2, | ||
Struct { | ||
a: u8, | ||
b: u16, | ||
} = 1, | ||
} | ||
|
||
impl Enum { | ||
fn tag(&self) -> u8 { | ||
unsafe { *(self as *const Self as *const u8) } | ||
} | ||
} | ||
|
||
assert_eq!(3, Enum::Unit.tag()); | ||
assert_eq!(2, Enum::Tuple(5).tag()); | ||
assert_eq!(1, Enum::Struct{a: 7, b: 11}.tag()); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/test/ui/enum-discriminant/arbitrary_enum_discriminant-no-repr.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#![crate_type="lib"] | ||
#![feature(arbitrary_enum_discriminant)] | ||
|
||
enum Enum { | ||
//~^ ERROR `#[repr(inttype)]` must be specified | ||
Unit = 1, | ||
Tuple() = 2, | ||
Struct{} = 3, | ||
} |
14 changes: 14 additions & 0 deletions
14
src/test/ui/enum-discriminant/arbitrary_enum_discriminant-no-repr.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error[E0732]: `#[repr(inttype)]` must be specified | ||
--> $DIR/arbitrary_enum_discriminant-no-repr.rs:4:1 | ||
| | ||
LL | / enum Enum { | ||
LL | | | ||
LL | | Unit = 1, | ||
LL | | Tuple() = 2, | ||
LL | | Struct{} = 3, | ||
LL | | } | ||
| |_^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0732`. |
Oops, something went wrong.