Add repr(no_niche)
to UnsafeCell
. Fix #68303.
This commit is contained in:
parent
35e3b4d1d8
commit
3e047229ef
@ -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,
|
||||
}
|
||||
|
@ -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)]
|
||||
|
32
src/test/ui/layout/unsafe-cell-hides-niche.rs
Normal file
32
src/test/ui/layout/unsafe-cell-hides-niche.rs
Normal 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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user