Add repr(no_niche) to UnsafeCell. Fix #68303.

This commit is contained in:
Felix S. Klock II 2020-01-23 14:33:55 -05:00
parent 35e3b4d1d8
commit 3e047229ef
3 changed files with 34 additions and 0 deletions

View File

@ -1475,6 +1475,7 @@ impl<T: ?Sized + fmt::Display> fmt::Display for RefMut<'_, T> {
#[lang = "unsafe_cell"]
#[stable(feature = "rust1", since = "1.0.0")]
#[repr(transparent)]
#[cfg_attr(not(bootstrap), repr(no_niche))] // rust-lang/rust#68303.
pub struct UnsafeCell<T: ?Sized> {
value: T,
}

View File

@ -138,6 +138,7 @@
#![feature(const_type_id)]
#![feature(const_caller_location)]
#![feature(assoc_int_consts)]
#![cfg_attr(not(bootstrap), feature(no_niche))] // rust-lang/rust#68303
#[prelude_import]
#[allow(unused)]

View File

@ -0,0 +1,32 @@
// For rust-lang/rust#68303: the contents of `UnsafeCell<T>` cannot
// participate in the niche-optimization for enum discriminants. This
// test checks that an `Option<UnsafeCell<NonZeroU32>>` has the same
// size in memory as an `Option<UnsafeCell<u32>>` (namely, 8 bytes).
// run-pass
#![feature(no_niche)]
use std::cell::UnsafeCell;
use std::mem::size_of;
use std::num::NonZeroU32 as N32;
struct Wrapper<T>(T);
#[repr(transparent)]
struct Transparent<T>(T);
#[repr(no_niche)]
struct NoNiche<T>(T);
fn main() {
assert_eq!(size_of::<Option<Wrapper<u32>>>(), 8);
assert_eq!(size_of::<Option<Wrapper<N32>>>(), 4);
assert_eq!(size_of::<Option<Transparent<u32>>>(), 8);
assert_eq!(size_of::<Option<Transparent<N32>>>(), 4);
assert_eq!(size_of::<Option<NoNiche<u32>>>(), 8);
assert_eq!(size_of::<Option<NoNiche<N32>>>(), 8);
assert_eq!(size_of::<Option<UnsafeCell<u32>>>(), 8);
assert_eq!(size_of::<Option<UnsafeCell<N32>>>(), 8);
}