|
| 1 | +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +// Test that we will do various size optimizations to enum layout, but |
| 12 | +// *not* if `#[repr(u8)]` or `#[repr(C)]` is passed. See also #40029. |
| 13 | + |
| 14 | +#![allow(dead_code)] |
| 15 | + |
| 16 | +use std::mem; |
| 17 | + |
| 18 | +enum Nullable<T> { |
| 19 | + Alive(T), |
| 20 | + Dropped, |
| 21 | +} |
| 22 | + |
| 23 | +#[repr(u8)] |
| 24 | +enum NullableU8<T> { |
| 25 | + Alive(T), |
| 26 | + Dropped, |
| 27 | +} |
| 28 | + |
| 29 | +#[repr(C)] |
| 30 | +enum NullableC<T> { |
| 31 | + Alive(T), |
| 32 | + Dropped, |
| 33 | +} |
| 34 | + |
| 35 | +struct StructNewtype<T>(T); |
| 36 | + |
| 37 | +#[repr(C)] |
| 38 | +struct StructNewtypeC<T>(T); |
| 39 | + |
| 40 | +enum EnumNewtype<T> { Variant(T) } |
| 41 | + |
| 42 | +#[repr(u8)] |
| 43 | +enum EnumNewtypeU8<T> { Variant(T) } |
| 44 | + |
| 45 | +#[repr(C)] |
| 46 | +enum EnumNewtypeC<T> { Variant(T) } |
| 47 | + |
| 48 | +fn main() { |
| 49 | + assert!(mem::size_of::<Box<i32>>() == mem::size_of::<Nullable<Box<i32>>>()); |
| 50 | + assert!(mem::size_of::<Box<i32>>() < mem::size_of::<NullableU8<Box<i32>>>()); |
| 51 | + assert!(mem::size_of::<Box<i32>>() < mem::size_of::<NullableC<Box<i32>>>()); |
| 52 | + |
| 53 | + assert!(mem::size_of::<i32>() == mem::size_of::<StructNewtype<i32>>()); |
| 54 | + assert!(mem::size_of::<i32>() == mem::size_of::<StructNewtypeC<i32>>()); |
| 55 | + |
| 56 | + assert!(mem::size_of::<i32>() == mem::size_of::<EnumNewtype<i32>>()); |
| 57 | + assert!(mem::size_of::<i32>() < mem::size_of::<EnumNewtypeU8<i32>>()); |
| 58 | + assert!(mem::size_of::<i32>() < mem::size_of::<EnumNewtypeC<i32>>()); |
| 59 | +} |
0 commit comments