Skip to content

Commit d174173

Browse files
committed
Auto merge of #57133 - SimonSapin:zero, r=oli-obk
Remove the private generic NonZero<T> wrapper type Instead, use `#[rustc_layout_scalar_valid_range_start(1)]` directly on relevant libcore types.
2 parents d298697 + 7a09115 commit d174173

File tree

7 files changed

+24
-53
lines changed

7 files changed

+24
-53
lines changed

src/etc/debugger_pretty_printers_common.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,7 @@ def extract_length_ptr_and_cap_from_std_vec(vec_val):
342342

343343
vec_ptr_val = buf.get_child_at_index(0)
344344
capacity = buf.get_child_at_index(1).as_integer()
345-
unique_ptr_val = vec_ptr_val.get_child_at_index(0)
346-
data_ptr = unique_ptr_val.get_child_at_index(0)
345+
data_ptr = vec_ptr_val.get_child_at_index(0)
347346
assert data_ptr.type.get_dwarf_type_kind() == DWARF_TYPE_CODE_PTR
348347
return (length, data_ptr, capacity)
349348

@@ -360,8 +359,7 @@ def extract_tail_head_ptr_and_cap_from_std_vecdeque(vec_val):
360359

361360
vec_ptr_val = buf.get_child_at_index(0)
362361
capacity = buf.get_child_at_index(1).as_integer()
363-
unique_ptr_val = vec_ptr_val.get_child_at_index(0)
364-
data_ptr = unique_ptr_val.get_child_at_index(0)
362+
data_ptr = vec_ptr_val.get_child_at_index(0)
365363
assert data_ptr.type.get_dwarf_type_kind() == DWARF_TYPE_CODE_PTR
366364
return (tail, head, data_ptr, capacity)
367365

src/etc/gdb_rust_pretty_printing.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,7 @@ def children(self):
322322

323323
# Yield each key (and optionally value) from a BoxedNode.
324324
def children_of_node(boxed_node, height, want_values):
325-
ptr = boxed_node['ptr']['pointer']
326-
# This is written oddly because we don't want to rely on the field name being `__0`.
327-
node_ptr = ptr[ptr.type.fields()[0]]
325+
node_ptr = boxed_node['ptr']['pointer']
328326
if height > 0:
329327
type_name = str(node_ptr.type.target()).replace('LeafNode', 'InternalNode')
330328
node_type = gdb.lookup_type(type_name)

src/libcore/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ pub mod alloc;
216216

217217
// note: does not need to be public
218218
mod iter_private;
219-
mod nonzero;
220219
mod tuple;
221220
mod unit;
222221

src/libcore/nonzero.rs

-23
This file was deleted.

src/libcore/num/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use convert::TryFrom;
66
use fmt;
77
use intrinsics;
88
use mem;
9-
use nonzero::NonZero;
109
use ops;
1110
use str::FromStr;
1211

@@ -48,7 +47,8 @@ assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", st
4847
#[stable(feature = "nonzero", since = "1.28.0")]
4948
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
5049
#[repr(transparent)]
51-
pub struct $Ty(NonZero<$Int>);
50+
#[rustc_layout_scalar_valid_range_start(1)]
51+
pub struct $Ty($Int);
5252
}
5353

5454
impl $Ty {
@@ -60,15 +60,15 @@ assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", st
6060
#[stable(feature = "nonzero", since = "1.28.0")]
6161
#[inline]
6262
pub const unsafe fn new_unchecked(n: $Int) -> Self {
63-
$Ty(NonZero(n))
63+
$Ty(n)
6464
}
6565

6666
/// Create a non-zero if the given value is not zero.
6767
#[stable(feature = "nonzero", since = "1.28.0")]
6868
#[inline]
6969
pub fn new(n: $Int) -> Option<Self> {
7070
if n != 0 {
71-
Some($Ty(unsafe { NonZero(n) }))
71+
Some(unsafe { $Ty(n) })
7272
} else {
7373
None
7474
}
@@ -78,15 +78,15 @@ assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", st
7878
#[stable(feature = "nonzero", since = "1.28.0")]
7979
#[inline]
8080
pub fn get(self) -> $Int {
81-
self.0 .0
81+
self.0
8282
}
8383

8484
}
8585

8686
#[stable(feature = "from_nonzero", since = "1.31.0")]
8787
impl From<$Ty> for $Int {
8888
fn from(nonzero: $Ty) -> Self {
89-
nonzero.0 .0
89+
nonzero.0
9090
}
9191
}
9292

src/libcore/ptr.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ use fmt;
7070
use hash;
7171
use marker::{PhantomData, Unsize};
7272
use mem::{self, MaybeUninit};
73-
use nonzero::NonZero;
7473

7574
use cmp::Ordering::{self, Less, Equal, Greater};
7675

@@ -2718,8 +2717,9 @@ impl<T: ?Sized> PartialOrd for *mut T {
27182717
(if you also use #[may_dangle]), Send, and/or Sync")]
27192718
#[doc(hidden)]
27202719
#[repr(transparent)]
2720+
#[rustc_layout_scalar_valid_range_start(1)]
27212721
pub struct Unique<T: ?Sized> {
2722-
pointer: NonZero<*const T>,
2722+
pointer: *const T,
27232723
// NOTE: this marker has no consequences for variance, but is necessary
27242724
// for dropck to understand that we logically own a `T`.
27252725
//
@@ -2776,21 +2776,21 @@ impl<T: ?Sized> Unique<T> {
27762776
///
27772777
/// `ptr` must be non-null.
27782778
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
2779-
Unique { pointer: NonZero(ptr as _), _marker: PhantomData }
2779+
Unique { pointer: ptr as _, _marker: PhantomData }
27802780
}
27812781

27822782
/// Creates a new `Unique` if `ptr` is non-null.
27832783
pub fn new(ptr: *mut T) -> Option<Self> {
27842784
if !ptr.is_null() {
2785-
Some(Unique { pointer: unsafe { NonZero(ptr as _) }, _marker: PhantomData })
2785+
Some(unsafe { Unique { pointer: ptr as _, _marker: PhantomData } })
27862786
} else {
27872787
None
27882788
}
27892789
}
27902790

27912791
/// Acquires the underlying `*mut` pointer.
27922792
pub fn as_ptr(self) -> *mut T {
2793-
self.pointer.0 as *mut T
2793+
self.pointer as *mut T
27942794
}
27952795

27962796
/// Dereferences the content.
@@ -2838,21 +2838,21 @@ impl<T: ?Sized> fmt::Pointer for Unique<T> {
28382838
#[unstable(feature = "ptr_internals", issue = "0")]
28392839
impl<'a, T: ?Sized> From<&'a mut T> for Unique<T> {
28402840
fn from(reference: &'a mut T) -> Self {
2841-
Unique { pointer: unsafe { NonZero(reference as *mut T) }, _marker: PhantomData }
2841+
unsafe { Unique { pointer: reference as *mut T, _marker: PhantomData } }
28422842
}
28432843
}
28442844

28452845
#[unstable(feature = "ptr_internals", issue = "0")]
28462846
impl<'a, T: ?Sized> From<&'a T> for Unique<T> {
28472847
fn from(reference: &'a T) -> Self {
2848-
Unique { pointer: unsafe { NonZero(reference as *const T) }, _marker: PhantomData }
2848+
unsafe { Unique { pointer: reference as *const T, _marker: PhantomData } }
28492849
}
28502850
}
28512851

28522852
#[unstable(feature = "ptr_internals", issue = "0")]
28532853
impl<'a, T: ?Sized> From<NonNull<T>> for Unique<T> {
28542854
fn from(p: NonNull<T>) -> Self {
2855-
Unique { pointer: p.pointer, _marker: PhantomData }
2855+
unsafe { Unique { pointer: p.pointer, _marker: PhantomData } }
28562856
}
28572857
}
28582858

@@ -2875,8 +2875,9 @@ impl<'a, T: ?Sized> From<NonNull<T>> for Unique<T> {
28752875
/// provide a public API that follows the normal shared XOR mutable rules of Rust.
28762876
#[stable(feature = "nonnull", since = "1.25.0")]
28772877
#[repr(transparent)]
2878+
#[rustc_layout_scalar_valid_range_start(1)]
28782879
pub struct NonNull<T: ?Sized> {
2879-
pointer: NonZero<*const T>,
2880+
pointer: *const T,
28802881
}
28812882

28822883
/// `NonNull` pointers are not `Send` because the data they reference may be aliased.
@@ -2918,7 +2919,7 @@ impl<T: ?Sized> NonNull<T> {
29182919
#[stable(feature = "nonnull", since = "1.25.0")]
29192920
#[inline]
29202921
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
2921-
NonNull { pointer: NonZero(ptr as _) }
2922+
NonNull { pointer: ptr as _ }
29222923
}
29232924

29242925
/// Creates a new `NonNull` if `ptr` is non-null.
@@ -2936,7 +2937,7 @@ impl<T: ?Sized> NonNull<T> {
29362937
#[stable(feature = "nonnull", since = "1.25.0")]
29372938
#[inline]
29382939
pub const fn as_ptr(self) -> *mut T {
2939-
self.pointer.0 as *mut T
2940+
self.pointer as *mut T
29402941
}
29412942

29422943
/// Dereferences the content.
@@ -3040,22 +3041,22 @@ impl<T: ?Sized> hash::Hash for NonNull<T> {
30403041
impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
30413042
#[inline]
30423043
fn from(unique: Unique<T>) -> Self {
3043-
NonNull { pointer: unique.pointer }
3044+
unsafe { NonNull { pointer: unique.pointer } }
30443045
}
30453046
}
30463047

30473048
#[stable(feature = "nonnull", since = "1.25.0")]
30483049
impl<'a, T: ?Sized> From<&'a mut T> for NonNull<T> {
30493050
#[inline]
30503051
fn from(reference: &'a mut T) -> Self {
3051-
NonNull { pointer: unsafe { NonZero(reference as *mut T) } }
3052+
unsafe { NonNull { pointer: reference as *mut T } }
30523053
}
30533054
}
30543055

30553056
#[stable(feature = "nonnull", since = "1.25.0")]
30563057
impl<'a, T: ?Sized> From<&'a T> for NonNull<T> {
30573058
#[inline]
30583059
fn from(reference: &'a T) -> Self {
3059-
NonNull { pointer: unsafe { NonZero(reference as *const T) } }
3060+
unsafe { NonNull { pointer: reference as *const T } }
30603061
}
30613062
}

src/test/ui/print_type_sizes/niche-filling.stdout

-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ print-type-size type: `MyOption<std::num::NonZeroU32>`: 4 bytes, alignment: 4 by
3636
print-type-size variant `None`: 0 bytes
3737
print-type-size variant `Some`: 4 bytes
3838
print-type-size field `.0`: 4 bytes
39-
print-type-size type: `core::nonzero::NonZero<u32>`: 4 bytes, alignment: 4 bytes
40-
print-type-size field `.0`: 4 bytes
4139
print-type-size type: `std::num::NonZeroU32`: 4 bytes, alignment: 4 bytes
4240
print-type-size field `.0`: 4 bytes
4341
print-type-size type: `Enum4<(), (), (), MyOption<u8>>`: 2 bytes, alignment: 1 bytes

0 commit comments

Comments
 (0)