diff options
| author | Andreas Liljeqvist <bonega@gmail.com> | 2021-08-08 13:34:46 +0200 |
|---|---|---|
| committer | Andreas Liljeqvist <bonega@gmail.com> | 2021-09-13 21:55:14 +0200 |
| commit | 4d66fbc4b9b95a652636e3723937c3accec85d65 (patch) | |
| tree | 04562fd8256a218651d97a705f09f79ae2cae75a /src | |
| parent | 641e02f388acc6b1d316a59c605a32d1711a8758 (diff) | |
enum niche allocation grows toward zero if possible
Diffstat (limited to 'src')
| -rw-r--r-- | src/test/assembly/niche-prefer-zero.rs | 25 | ||||
| -rw-r--r-- | src/test/ui/enum-discriminant/niche-prefer-zero.rs | 14 |
2 files changed, 39 insertions, 0 deletions
diff --git a/src/test/assembly/niche-prefer-zero.rs b/src/test/assembly/niche-prefer-zero.rs new file mode 100644 index 00000000000..0ab37a618da --- /dev/null +++ b/src/test/assembly/niche-prefer-zero.rs @@ -0,0 +1,25 @@ +// Check that niche selection prefers zero and that jumps are optimized away. +// See https://github.com/rust-lang/rust/pull/87794 +// assembly-output: emit-asm +// only-x86 +// compile-flags: -Copt-level=3 + +#![crate_type = "lib"] + +#[repr(u8)] +pub enum Size { + One = 1, + Two = 2, + Three = 3, +} + +#[no_mangle] +pub fn handle(x: Option<Size>) -> u8 { + match x { + None => 0, + Some(size) => size as u8, + } +} + +// There should be no jumps in output +// CHECK-NOT: j diff --git a/src/test/ui/enum-discriminant/niche-prefer-zero.rs b/src/test/ui/enum-discriminant/niche-prefer-zero.rs new file mode 100644 index 00000000000..f20607a8903 --- /dev/null +++ b/src/test/ui/enum-discriminant/niche-prefer-zero.rs @@ -0,0 +1,14 @@ +// Check that niche selection prefers zero. +// See https://github.com/rust-lang/rust/pull/87794 +// run-pass +#[repr(u8)] +pub enum Size { + One = 1, + Two = 2, + Three = 3, +} + +fn main() { + // check that `None` is zero + assert_eq!(0, unsafe { std::mem::transmute::<Option<Size>, u8>(None) }); +} |
