Skip to content

Commit 3e04722

Browse files
committed
Add repr(no_niche) to UnsafeCell. Fix #68303.
1 parent 35e3b4d commit 3e04722

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

Diff for: src/libcore/cell.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,7 @@ impl<T: ?Sized + fmt::Display> fmt::Display for RefMut<'_, T> {
14751475
#[lang = "unsafe_cell"]
14761476
#[stable(feature = "rust1", since = "1.0.0")]
14771477
#[repr(transparent)]
1478+
#[cfg_attr(not(bootstrap), repr(no_niche))] // rust-lang/rust#68303.
14781479
pub struct UnsafeCell<T: ?Sized> {
14791480
value: T,
14801481
}

Diff for: src/libcore/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
#![feature(const_type_id)]
139139
#![feature(const_caller_location)]
140140
#![feature(assoc_int_consts)]
141+
#![cfg_attr(not(bootstrap), feature(no_niche))] // rust-lang/rust#68303
141142

142143
#[prelude_import]
143144
#[allow(unused)]

Diff for: src/test/ui/layout/unsafe-cell-hides-niche.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// For rust-lang/rust#68303: the contents of `UnsafeCell<T>` cannot
2+
// participate in the niche-optimization for enum discriminants. This
3+
// test checks that an `Option<UnsafeCell<NonZeroU32>>` has the same
4+
// size in memory as an `Option<UnsafeCell<u32>>` (namely, 8 bytes).
5+
6+
// run-pass
7+
8+
#![feature(no_niche)]
9+
10+
use std::cell::UnsafeCell;
11+
use std::mem::size_of;
12+
use std::num::NonZeroU32 as N32;
13+
14+
struct Wrapper<T>(T);
15+
16+
#[repr(transparent)]
17+
struct Transparent<T>(T);
18+
19+
#[repr(no_niche)]
20+
struct NoNiche<T>(T);
21+
22+
fn main() {
23+
assert_eq!(size_of::<Option<Wrapper<u32>>>(), 8);
24+
assert_eq!(size_of::<Option<Wrapper<N32>>>(), 4);
25+
assert_eq!(size_of::<Option<Transparent<u32>>>(), 8);
26+
assert_eq!(size_of::<Option<Transparent<N32>>>(), 4);
27+
assert_eq!(size_of::<Option<NoNiche<u32>>>(), 8);
28+
assert_eq!(size_of::<Option<NoNiche<N32>>>(), 8);
29+
30+
assert_eq!(size_of::<Option<UnsafeCell<u32>>>(), 8);
31+
assert_eq!(size_of::<Option<UnsafeCell<N32>>>(), 8);
32+
}

0 commit comments

Comments
 (0)