Remove the private generic NonZero<T> wrapper type.

Instead, use `#[rustc_layout_scalar_valid_range_start(1)]` directly
on relevant libcore types.
This commit is contained in:
Simon Sapin 2018-12-26 18:27:30 +01:00
parent 79d8a0fcef
commit 7a09115280
7 changed files with 24 additions and 53 deletions

View File

@ -342,8 +342,7 @@ def extract_length_ptr_and_cap_from_std_vec(vec_val):
vec_ptr_val = buf.get_child_at_index(0)
capacity = buf.get_child_at_index(1).as_integer()
unique_ptr_val = vec_ptr_val.get_child_at_index(0)
data_ptr = unique_ptr_val.get_child_at_index(0)
data_ptr = vec_ptr_val.get_child_at_index(0)
assert data_ptr.type.get_dwarf_type_kind() == DWARF_TYPE_CODE_PTR
return (length, data_ptr, capacity)
@ -360,8 +359,7 @@ def extract_tail_head_ptr_and_cap_from_std_vecdeque(vec_val):
vec_ptr_val = buf.get_child_at_index(0)
capacity = buf.get_child_at_index(1).as_integer()
unique_ptr_val = vec_ptr_val.get_child_at_index(0)
data_ptr = unique_ptr_val.get_child_at_index(0)
data_ptr = vec_ptr_val.get_child_at_index(0)
assert data_ptr.type.get_dwarf_type_kind() == DWARF_TYPE_CODE_PTR
return (tail, head, data_ptr, capacity)

View File

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

View File

@ -216,7 +216,6 @@ pub mod alloc;
// note: does not need to be public
mod iter_private;
mod nonzero;
mod tuple;
mod unit;

View File

@ -1,23 +0,0 @@
//! Exposes the NonZero lang item which provides optimization hints.
use ops::{CoerceUnsized, DispatchFromDyn};
use marker::Freeze;
/// A wrapper type for raw pointers and integers that will never be
/// NULL or 0 that might allow certain optimizations.
#[rustc_layout_scalar_valid_range_start(1)]
#[derive(Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub(crate) struct NonZero<T: Freeze>(pub(crate) T);
// Do not call `T::clone` as theoretically it could turn the field into `0`
// invalidating `NonZero`'s invariant.
impl<T: Copy + Freeze> Clone for NonZero<T> {
fn clone(&self) -> Self {
unsafe { NonZero(self.0) }
}
}
impl<T: CoerceUnsized<U> + Freeze, U: Freeze> CoerceUnsized<NonZero<U>> for NonZero<T> {}
impl<T: DispatchFromDyn<U> + Freeze, U: Freeze> DispatchFromDyn<NonZero<U>> for NonZero<T> {}

View File

@ -6,7 +6,6 @@ use convert::TryFrom;
use fmt;
use intrinsics;
use mem;
use nonzero::NonZero;
use ops;
use str::FromStr;
@ -48,7 +47,8 @@ assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", st
#[stable(feature = "nonzero", since = "1.28.0")]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct $Ty(NonZero<$Int>);
#[rustc_layout_scalar_valid_range_start(1)]
pub struct $Ty($Int);
}
impl $Ty {
@ -60,7 +60,7 @@ assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", st
#[stable(feature = "nonzero", since = "1.28.0")]
#[inline]
pub const unsafe fn new_unchecked(n: $Int) -> Self {
$Ty(NonZero(n))
$Ty(n)
}
/// Create a non-zero if the given value is not zero.
@ -68,7 +68,7 @@ assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", st
#[inline]
pub fn new(n: $Int) -> Option<Self> {
if n != 0 {
Some($Ty(unsafe { NonZero(n) }))
Some(unsafe { $Ty(n) })
} else {
None
}
@ -78,7 +78,7 @@ assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", st
#[stable(feature = "nonzero", since = "1.28.0")]
#[inline]
pub fn get(self) -> $Int {
self.0 .0
self.0
}
}
@ -86,7 +86,7 @@ assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", st
#[stable(feature = "from_nonzero", since = "1.31.0")]
impl From<$Ty> for $Int {
fn from(nonzero: $Ty) -> Self {
nonzero.0 .0
nonzero.0
}
}

View File

@ -70,7 +70,6 @@ use fmt;
use hash;
use marker::{PhantomData, Unsize};
use mem::{self, MaybeUninit};
use nonzero::NonZero;
use cmp::Ordering::{self, Less, Equal, Greater};
@ -2718,8 +2717,9 @@ impl<T: ?Sized> PartialOrd for *mut T {
(if you also use #[may_dangle]), Send, and/or Sync")]
#[doc(hidden)]
#[repr(transparent)]
#[rustc_layout_scalar_valid_range_start(1)]
pub struct Unique<T: ?Sized> {
pointer: NonZero<*const T>,
pointer: *const T,
// NOTE: this marker has no consequences for variance, but is necessary
// for dropck to understand that we logically own a `T`.
//
@ -2776,13 +2776,13 @@ impl<T: ?Sized> Unique<T> {
///
/// `ptr` must be non-null.
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
Unique { pointer: NonZero(ptr as _), _marker: PhantomData }
Unique { pointer: ptr as _, _marker: PhantomData }
}
/// Creates a new `Unique` if `ptr` is non-null.
pub fn new(ptr: *mut T) -> Option<Self> {
if !ptr.is_null() {
Some(Unique { pointer: unsafe { NonZero(ptr as _) }, _marker: PhantomData })
Some(unsafe { Unique { pointer: ptr as _, _marker: PhantomData } })
} else {
None
}
@ -2790,7 +2790,7 @@ impl<T: ?Sized> Unique<T> {
/// Acquires the underlying `*mut` pointer.
pub fn as_ptr(self) -> *mut T {
self.pointer.0 as *mut T
self.pointer as *mut T
}
/// Dereferences the content.
@ -2838,21 +2838,21 @@ impl<T: ?Sized> fmt::Pointer for Unique<T> {
#[unstable(feature = "ptr_internals", issue = "0")]
impl<'a, T: ?Sized> From<&'a mut T> for Unique<T> {
fn from(reference: &'a mut T) -> Self {
Unique { pointer: unsafe { NonZero(reference as *mut T) }, _marker: PhantomData }
unsafe { Unique { pointer: reference as *mut T, _marker: PhantomData } }
}
}
#[unstable(feature = "ptr_internals", issue = "0")]
impl<'a, T: ?Sized> From<&'a T> for Unique<T> {
fn from(reference: &'a T) -> Self {
Unique { pointer: unsafe { NonZero(reference as *const T) }, _marker: PhantomData }
unsafe { Unique { pointer: reference as *const T, _marker: PhantomData } }
}
}
#[unstable(feature = "ptr_internals", issue = "0")]
impl<'a, T: ?Sized> From<NonNull<T>> for Unique<T> {
fn from(p: NonNull<T>) -> Self {
Unique { pointer: p.pointer, _marker: PhantomData }
unsafe { Unique { pointer: p.pointer, _marker: PhantomData } }
}
}
@ -2875,8 +2875,9 @@ impl<'a, T: ?Sized> From<NonNull<T>> for Unique<T> {
/// provide a public API that follows the normal shared XOR mutable rules of Rust.
#[stable(feature = "nonnull", since = "1.25.0")]
#[repr(transparent)]
#[rustc_layout_scalar_valid_range_start(1)]
pub struct NonNull<T: ?Sized> {
pointer: NonZero<*const T>,
pointer: *const T,
}
/// `NonNull` pointers are not `Send` because the data they reference may be aliased.
@ -2918,7 +2919,7 @@ impl<T: ?Sized> NonNull<T> {
#[stable(feature = "nonnull", since = "1.25.0")]
#[inline]
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
NonNull { pointer: NonZero(ptr as _) }
NonNull { pointer: ptr as _ }
}
/// Creates a new `NonNull` if `ptr` is non-null.
@ -2936,7 +2937,7 @@ impl<T: ?Sized> NonNull<T> {
#[stable(feature = "nonnull", since = "1.25.0")]
#[inline]
pub const fn as_ptr(self) -> *mut T {
self.pointer.0 as *mut T
self.pointer as *mut T
}
/// Dereferences the content.
@ -3040,7 +3041,7 @@ impl<T: ?Sized> hash::Hash for NonNull<T> {
impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
#[inline]
fn from(unique: Unique<T>) -> Self {
NonNull { pointer: unique.pointer }
unsafe { NonNull { pointer: unique.pointer } }
}
}
@ -3048,7 +3049,7 @@ impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
impl<'a, T: ?Sized> From<&'a mut T> for NonNull<T> {
#[inline]
fn from(reference: &'a mut T) -> Self {
NonNull { pointer: unsafe { NonZero(reference as *mut T) } }
unsafe { NonNull { pointer: reference as *mut T } }
}
}
@ -3056,6 +3057,6 @@ impl<'a, T: ?Sized> From<&'a mut T> for NonNull<T> {
impl<'a, T: ?Sized> From<&'a T> for NonNull<T> {
#[inline]
fn from(reference: &'a T) -> Self {
NonNull { pointer: unsafe { NonZero(reference as *const T) } }
unsafe { NonNull { pointer: reference as *const T } }
}
}

View File

@ -36,8 +36,6 @@ print-type-size type: `MyOption<std::num::NonZeroU32>`: 4 bytes, alignment: 4 by
print-type-size variant `None`: 0 bytes
print-type-size variant `Some`: 4 bytes
print-type-size field `.0`: 4 bytes
print-type-size type: `core::nonzero::NonZero<u32>`: 4 bytes, alignment: 4 bytes
print-type-size field `.0`: 4 bytes
print-type-size type: `std::num::NonZeroU32`: 4 bytes, alignment: 4 bytes
print-type-size field `.0`: 4 bytes
print-type-size type: `Enum4<(), (), (), MyOption<u8>>`: 2 bytes, alignment: 1 bytes