Skip to content

Commit 2a899dc

Browse files
committed
UnsafeCell now has no niches, ever.
1 parent e78e747 commit 2a899dc

File tree

22 files changed

+71
-510
lines changed

22 files changed

+71
-510
lines changed

compiler/rustc_attr/src/builtin.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,6 @@ pub enum ReprAttr {
856856
ReprSimd,
857857
ReprTransparent,
858858
ReprAlign(u32),
859-
ReprNoNiche,
860859
}
861860

862861
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
@@ -904,7 +903,6 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
904903
sym::packed => Some(ReprPacked(1)),
905904
sym::simd => Some(ReprSimd),
906905
sym::transparent => Some(ReprTransparent),
907-
sym::no_niche => Some(ReprNoNiche),
908906
sym::align => {
909907
let mut err = struct_span_err!(
910908
diagnostic,
@@ -943,7 +941,7 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
943941
Ok(literal) => acc.push(ReprPacked(literal)),
944942
Err(message) => literal_error = Some(message),
945943
};
946-
} else if matches!(name, sym::C | sym::simd | sym::transparent | sym::no_niche)
944+
} else if matches!(name, sym::C | sym::simd | sym::transparent)
947945
|| int_type_of_word(name).is_some()
948946
{
949947
recognised = true;
@@ -1001,7 +999,7 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
1001999
} else {
10021000
if matches!(
10031001
meta_item.name_or_empty(),
1004-
sym::C | sym::simd | sym::transparent | sym::no_niche
1002+
sym::C | sym::simd | sym::transparent
10051003
) || int_type_of_word(meta_item.name_or_empty()).is_some()
10061004
{
10071005
recognised = true;
@@ -1039,7 +1037,7 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
10391037
.emit();
10401038
} else if matches!(
10411039
meta_item.name_or_empty(),
1042-
sym::C | sym::simd | sym::transparent | sym::no_niche
1040+
sym::C | sym::simd | sym::transparent
10431041
) || int_type_of_word(meta_item.name_or_empty()).is_some()
10441042
{
10451043
recognised = true;

compiler/rustc_const_eval/src/interpret/intern.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: CompileTimeMachine<'mir, 'tcx, const_eval::Memory
217217
}
218218

219219
if let Some(def) = mplace.layout.ty.ty_adt_def() {
220-
if Some(def.did()) == self.ecx.tcx.lang_items().unsafe_cell_type() {
220+
if def.is_unsafe_cell() {
221221
// We are crossing over an `UnsafeCell`, we can mutate again. This means that
222222
// References we encounter inside here are interned as pointing to mutable
223223
// allocations.

compiler/rustc_const_eval/src/interpret/validity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
821821
// Special check preventing `UnsafeCell` in the inner part of constants
822822
if let Some(def) = op.layout.ty.ty_adt_def() {
823823
if matches!(self.ctfe_mode, Some(CtfeValidationMode::Const { inner: true, .. }))
824-
&& Some(def.did()) == self.ecx.tcx.lang_items().unsafe_cell_type()
824+
&& def.is_unsafe_cell()
825825
{
826826
throw_validation_failure!(self.path, { "`UnsafeCell` in a `const`" });
827827
}

compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ impl Qualif for HasMutInterior {
9696
}
9797

9898
fn in_adt_inherently<'tcx>(
99-
cx: &ConstCx<'_, 'tcx>,
99+
_cx: &ConstCx<'_, 'tcx>,
100100
adt: AdtDef<'tcx>,
101101
_: SubstsRef<'tcx>,
102102
) -> bool {
103103
// Exactly one type, `UnsafeCell`, has the `HasMutInterior` qualif inherently.
104104
// It arises structurally for all other types.
105-
Some(adt.did()) == cx.tcx.lang_items().unsafe_cell_type()
105+
adt.is_unsafe_cell()
106106
}
107107
}
108108

compiler/rustc_feature/src/active.rs

-3
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,6 @@ declare_features! (
156156
(active, intrinsics, "1.0.0", None, None),
157157
/// Allows using `#[lang = ".."]` attribute for linking items to special compiler logic.
158158
(active, lang_items, "1.0.0", None, None),
159-
/// Allows `#[repr(no_niche)]` (an implementation detail of `rustc`,
160-
/// it is not on path for eventual stabilization).
161-
(active, no_niche, "1.42.0", None, None),
162159
/// Allows using `#[omit_gdb_pretty_printer_section]`.
163160
(active, omit_gdb_pretty_printer_section, "1.5.0", None, None),
164161
/// Allows using `#[prelude_import]` on glob `use` items.

compiler/rustc_lint/src/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ fn ty_is_known_nonnull<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, mode: CItemKi
705705

706706
// Types with a `#[repr(no_niche)]` attribute have their niche hidden.
707707
// The attribute is used by the UnsafeCell for example (the only use so far).
708-
if def.repr().hide_niche() {
708+
if def.is_unsafe_cell() {
709709
return false;
710710
}
711711

compiler/rustc_middle/src/ty/adt.rs

+11
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ bitflags! {
5252
/// Indicates whether the variant list of this ADT is `#[non_exhaustive]`.
5353
/// (i.e., this flag is never set unless this ADT is an enum).
5454
const IS_VARIANT_LIST_NON_EXHAUSTIVE = 1 << 8;
55+
/// Indicates whether the type is `UnsafeCell`.
56+
const IS_UNSAFE_CELL = 1 << 9;
5557
}
5658
}
5759

@@ -242,6 +244,9 @@ impl AdtDefData {
242244
if Some(did) == tcx.lang_items().manually_drop() {
243245
flags |= AdtFlags::IS_MANUALLY_DROP;
244246
}
247+
if Some(did) == tcx.lang_items().unsafe_cell_type() {
248+
flags |= AdtFlags::IS_UNSAFE_CELL;
249+
}
245250

246251
AdtDefData { did, variants, flags, repr }
247252
}
@@ -328,6 +333,12 @@ impl<'tcx> AdtDef<'tcx> {
328333
self.flags().contains(AdtFlags::IS_BOX)
329334
}
330335

336+
/// Returns `true` if this is UnsafeCell<T>.
337+
#[inline]
338+
pub fn is_unsafe_cell(self) -> bool {
339+
self.flags().contains(AdtFlags::IS_UNSAFE_CELL)
340+
}
341+
331342
/// Returns `true` if this is `ManuallyDrop<T>`.
332343
#[inline]
333344
pub fn is_manually_drop(self) -> bool {

compiler/rustc_middle/src/ty/layout.rs

+26-22
Original file line numberDiff line numberDiff line change
@@ -542,14 +542,12 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
542542
debug!("univariant offset: {:?} field: {:#?}", offset, field);
543543
offsets[i as usize] = offset;
544544

545-
if !repr.hide_niche() {
546-
if let Some(mut niche) = field.largest_niche {
547-
let available = niche.available(dl);
548-
if available > largest_niche_available {
549-
largest_niche_available = available;
550-
niche.offset += offset;
551-
largest_niche = Some(niche);
552-
}
545+
if let Some(mut niche) = field.largest_niche {
546+
let available = niche.available(dl);
547+
if available > largest_niche_available {
548+
largest_niche_available = available;
549+
niche.offset += offset;
550+
largest_niche = Some(niche);
553551
}
554552
}
555553

@@ -1104,23 +1102,29 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
11041102
assert!(valid_range.end >= end);
11051103
valid_range.end = end;
11061104
}
1107-
1108-
// Update `largest_niche` if we have introduced a larger niche.
1109-
let niche = if def.repr().hide_niche() {
1110-
None
1105+
if def.is_unsafe_cell() {
1106+
match scalar {
1107+
Scalar::Initialized { value, valid_range } => {
1108+
*valid_range = WrappingRange::full(value.size(dl))
1109+
}
1110+
// Already doesn't have any niches
1111+
Scalar::Union { .. } => {}
1112+
}
1113+
st.largest_niche = None;
11111114
} else {
1112-
Niche::from_scalar(dl, Size::ZERO, *scalar)
1113-
};
1114-
if let Some(niche) = niche {
1115-
match st.largest_niche {
1116-
Some(largest_niche) => {
1117-
// Replace the existing niche even if they're equal,
1118-
// because this one is at a lower offset.
1119-
if largest_niche.available(dl) <= niche.available(dl) {
1120-
st.largest_niche = Some(niche);
1115+
// Update `largest_niche` if we have introduced a larger niche.
1116+
let niche = Niche::from_scalar(dl, Size::ZERO, *scalar);
1117+
if let Some(niche) = niche {
1118+
match st.largest_niche {
1119+
Some(largest_niche) => {
1120+
// Replace the existing niche even if they're equal,
1121+
// because this one is at a lower offset.
1122+
if largest_niche.available(dl) <= niche.available(dl) {
1123+
st.largest_niche = Some(niche);
1124+
}
11211125
}
1126+
None => st.largest_niche = Some(niche),
11221127
}
1123-
None => st.largest_niche = Some(niche),
11241128
}
11251129
}
11261130
}

compiler/rustc_middle/src/ty/mod.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -1719,11 +1719,9 @@ bitflags! {
17191719
const IS_TRANSPARENT = 1 << 2;
17201720
// Internal only for now. If true, don't reorder fields.
17211721
const IS_LINEAR = 1 << 3;
1722-
// If true, don't expose any niche to type's context.
1723-
const HIDE_NICHE = 1 << 4;
17241722
// If true, the type's layout can be randomized using
17251723
// the seed stored in `ReprOptions.layout_seed`
1726-
const RANDOMIZE_LAYOUT = 1 << 5;
1724+
const RANDOMIZE_LAYOUT = 1 << 4;
17271725
// Any of these flags being set prevent field reordering optimisation.
17281726
const IS_UNOPTIMISABLE = ReprFlags::IS_C.bits
17291727
| ReprFlags::IS_SIMD.bits
@@ -1780,7 +1778,6 @@ impl ReprOptions {
17801778
ReprFlags::empty()
17811779
}
17821780
attr::ReprTransparent => ReprFlags::IS_TRANSPARENT,
1783-
attr::ReprNoNiche => ReprFlags::HIDE_NICHE,
17841781
attr::ReprSimd => ReprFlags::IS_SIMD,
17851782
attr::ReprInt(i) => {
17861783
size = Some(i);
@@ -1833,11 +1830,6 @@ impl ReprOptions {
18331830
self.flags.contains(ReprFlags::IS_LINEAR)
18341831
}
18351832

1836-
#[inline]
1837-
pub fn hide_niche(&self) -> bool {
1838-
self.flags.contains(ReprFlags::HIDE_NICHE)
1839-
}
1840-
18411833
/// Returns the discriminant type, given these `repr` options.
18421834
/// This must only be called on enums!
18431835
pub fn discr_type(&self) -> attr::IntType {

compiler/rustc_passes/src/check_attr.rs

+2-19
Original file line numberDiff line numberDiff line change
@@ -1808,21 +1808,6 @@ impl CheckAttrVisitor<'_> {
18081808
_ => ("a", "struct, enum, or union"),
18091809
}
18101810
}
1811-
sym::no_niche => {
1812-
if !self.tcx.features().enabled(sym::no_niche) {
1813-
feature_err(
1814-
&self.tcx.sess.parse_sess,
1815-
sym::no_niche,
1816-
hint.span(),
1817-
"the attribute `repr(no_niche)` is currently unstable",
1818-
)
1819-
.emit();
1820-
}
1821-
match target {
1822-
Target::Struct | Target::Enum => continue,
1823-
_ => ("a", "struct or enum"),
1824-
}
1825-
}
18261811
sym::i8
18271812
| sym::u8
18281813
| sym::i16
@@ -1870,10 +1855,8 @@ impl CheckAttrVisitor<'_> {
18701855
// This is not ideal, but tracking precisely which ones are at fault is a huge hassle.
18711856
let hint_spans = hints.iter().map(|hint| hint.span());
18721857

1873-
// Error on repr(transparent, <anything else apart from no_niche>).
1874-
let non_no_niche = |hint: &&NestedMetaItem| hint.name_or_empty() != sym::no_niche;
1875-
let non_no_niche_count = hints.iter().filter(non_no_niche).count();
1876-
if is_transparent && non_no_niche_count > 1 {
1858+
// Error on repr(transparent, <anything else>).
1859+
if is_transparent && hints.len() > 1 {
18771860
let hint_spans: Vec<_> = hint_spans.clone().collect();
18781861
struct_span_err!(
18791862
self.tcx.sess,

compiler/rustc_span/src/symbol.rs

-2
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,6 @@ symbols! {
979979
no_link,
980980
no_main,
981981
no_mangle,
982-
no_niche,
983982
no_sanitize,
984983
no_stack_check,
985984
no_start,
@@ -1152,7 +1151,6 @@ symbols! {
11521151
repr128,
11531152
repr_align,
11541153
repr_align_enum,
1155-
repr_no_niche,
11561154
repr_packed,
11571155
repr_simd,
11581156
repr_transparent,

library/core/src/cell.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1856,7 +1856,6 @@ impl<T: ?Sized + fmt::Display> fmt::Display for RefMut<'_, T> {
18561856
#[lang = "unsafe_cell"]
18571857
#[stable(feature = "rust1", since = "1.0.0")]
18581858
#[repr(transparent)]
1859-
#[repr(no_niche)] // rust-lang/rust#68303.
18601859
pub struct UnsafeCell<T: ?Sized> {
18611860
value: T,
18621861
}

library/core/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@
191191
#![feature(never_type)]
192192
#![feature(no_core)]
193193
#![feature(no_coverage)] // rust-lang/rust#84605
194-
#![feature(no_niche)] // rust-lang/rust#68303
195194
#![feature(platform_intrinsics)]
196195
#![feature(prelude_import)]
197196
#![feature(repr_simd)]

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
// run-pass
77

8-
#![feature(no_niche)]
9-
108
use std::cell::UnsafeCell;
119
use std::mem::size_of;
1210
use std::num::NonZeroU32 as N32;
@@ -16,8 +14,7 @@ struct Wrapper<T>(T);
1614
#[repr(transparent)]
1715
struct Transparent<T>(T);
1816

19-
#[repr(no_niche)]
20-
struct NoNiche<T>(T);
17+
struct NoNiche<T>(UnsafeCell<T>);
2118

2219
fn main() {
2320
assert_eq!(size_of::<Option<Wrapper<u32>>>(), 8);

src/test/ui/lint/clashing-extern-fn.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// check-pass
22
// aux-build:external_extern_fn.rs
33
#![crate_type = "lib"]
4-
#![feature(no_niche)]
54
#![warn(clashing_extern_declarations)]
65

76
mod redeclared_different_signature {
@@ -400,9 +399,8 @@ mod hidden_niche {
400399
#[repr(transparent)]
401400
struct Transparent { x: NonZeroUsize }
402401

403-
#[repr(no_niche)]
404402
#[repr(transparent)]
405-
struct TransparentNoNiche { y: NonZeroUsize }
403+
struct TransparentNoNiche { y: UnsafeCell<NonZeroUsize> }
406404

407405
extern "C" {
408406
fn hidden_niche_transparent() -> Option<Transparent>;

0 commit comments

Comments
 (0)