Use num::NonZero* instead of NonZero<_> in rustc and tests

This commit is contained in:
Simon Sapin 2018-02-16 11:02:06 +01:00
parent 2d13ddb6e1
commit 67f46ce112
8 changed files with 38 additions and 46 deletions

View File

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use core::nonzero::NonZero;
use core::num::NonZeroU32;
use core::option::Option;
use core::option::Option::{Some, None};
use std::mem::size_of;
@ -16,28 +16,28 @@ use std::mem::size_of;
#[test]
fn test_create_nonzero_instance() {
let _a = unsafe {
NonZero::new_unchecked(21)
NonZeroU32::new_unchecked(21)
};
}
#[test]
fn test_size_nonzero_in_option() {
assert_eq!(size_of::<NonZero<u32>>(), size_of::<Option<NonZero<u32>>>());
assert_eq!(size_of::<NonZeroU32>(), size_of::<Option<NonZeroU32>>());
}
#[test]
fn test_match_on_nonzero_option() {
let a = Some(unsafe {
NonZero::new_unchecked(42)
NonZeroU32::new_unchecked(42)
});
match a {
Some(val) => assert_eq!(val.get(), 42),
None => panic!("unexpected None while matching on Some(NonZero(_))")
None => panic!("unexpected None while matching on Some(NonZeroU32(_))")
}
match unsafe { Some(NonZero::new_unchecked(43)) } {
match unsafe { Some(NonZeroU32::new_unchecked(43)) } {
Some(val) => assert_eq!(val.get(), 43),
None => panic!("unexpected None while matching on Some(NonZero(_))")
None => panic!("unexpected None while matching on Some(NonZeroU32(_))")
}
}

View File

@ -19,11 +19,11 @@ use syntax_pos::{Span, DUMMY_SP};
use rustc_data_structures::accumulate_vec::AccumulateVec;
use core::intrinsics;
use core::nonzero::NonZero;
use std::fmt;
use std::iter;
use std::marker::PhantomData;
use std::mem;
use std::num::NonZeroUsize;
/// An entity in the Rust typesystem, which can be one of
/// several kinds (only types and lifetimes for now).
@ -32,7 +32,7 @@ use std::mem;
/// indicate the type (`Ty` or `Region`) it points to.
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct Kind<'tcx> {
ptr: NonZero<usize>,
ptr: NonZeroUsize,
marker: PhantomData<(Ty<'tcx>, ty::Region<'tcx>)>
}
@ -63,7 +63,7 @@ impl<'tcx> UnpackedKind<'tcx> {
Kind {
ptr: unsafe {
NonZero::new_unchecked(ptr | tag)
NonZeroUsize::new_unchecked(ptr | tag)
},
marker: PhantomData
}

View File

@ -8,18 +8,18 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use core::nonzero::NonZero;
use std::num::NonZeroU32;
use std::u32;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct NodeIndex {
index: NonZero<u32>,
index: NonZeroU32,
}
impl NodeIndex {
pub fn new(value: usize) -> NodeIndex {
assert!(value < (u32::MAX as usize));
NodeIndex { index: NonZero::new((value as u32) + 1).unwrap() }
NodeIndex { index: NonZeroU32::new((value as u32) + 1).unwrap() }
}
pub fn get(self) -> usize {

View File

@ -29,17 +29,17 @@ mod abs_domain;
// (which is likely to yield a subtle off-by-one error).
pub(crate) mod indexes {
use std::fmt;
use core::nonzero::NonZero;
use std::num::NonZeroUsize;
use rustc_data_structures::indexed_vec::Idx;
macro_rules! new_index {
($Index:ident, $debug_name:expr) => {
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct $Index(NonZero<usize>);
pub struct $Index(NonZeroUsize);
impl Idx for $Index {
fn new(idx: usize) -> Self {
$Index(NonZero::new(idx + 1).unwrap())
$Index(NonZeroUsize::new(idx + 1).unwrap())
}
fn index(self) -> usize {
self.0.get() - 1

View File

@ -10,10 +10,9 @@
#![feature(nonzero, core)]
extern crate core;
use core::nonzero::NonZero;
use std::mem::size_of;
use std::num::NonZeroUsize;
use std::ptr::NonNull;
use std::rc::Rc;
use std::sync::Arc;
@ -59,8 +58,8 @@ fn main() {
assert_eq!(size_of::<[Box<isize>; 1]>(), size_of::<Option<[Box<isize>; 1]>>());
// Should apply to NonZero
assert_eq!(size_of::<NonZero<usize>>(), size_of::<Option<NonZero<usize>>>());
assert_eq!(size_of::<NonZero<*mut i8>>(), size_of::<Option<NonZero<*mut i8>>>());
assert_eq!(size_of::<NonZeroUsize>(), size_of::<Option<NonZeroUsize>>());
assert_eq!(size_of::<NonNull<i8>>(), size_of::<Option<NonNull<i8>>>());
// Should apply to types that use NonZero internally
assert_eq!(size_of::<Vec<isize>>(), size_of::<Option<Vec<isize>>>());

View File

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Don't fail if we encounter a NonZero<*T> where T is an unsized type
// Don't fail if we encounter a NonNull<T> where T is an unsized type
use std::ptr::NonNull;

View File

@ -14,7 +14,7 @@
// This file illustrates how niche-filling enums are handled,
// modelled after cases like `Option<&u32>`, `Option<bool>` and such.
//
// It uses NonZero directly, rather than `&_` or `Unique<_>`, because
// It uses NonZeroU32 rather than `&_` or `Unique<_>`, because
// the test is not set up to deal with target-dependent pointer width.
//
// It avoids using u64/i64 because on some targets that is only 4-byte
@ -25,8 +25,7 @@
#![feature(nonzero)]
#![allow(dead_code)]
extern crate core;
use core::nonzero::{NonZero, Zeroable};
use std::num::NonZeroU32;
pub enum MyOption<T> { None, Some(T) }
@ -36,7 +35,7 @@ impl<T> Default for MyOption<T> {
pub enum EmbeddedDiscr {
None,
Record { pre: u8, val: NonZero<u32>, post: u16 },
Record { pre: u8, val: NonZeroU32, post: u16 },
}
impl Default for EmbeddedDiscr {
@ -44,32 +43,24 @@ impl Default for EmbeddedDiscr {
}
#[derive(Default)]
pub struct IndirectNonZero<T: Zeroable + One> {
pub struct IndirectNonZero {
pre: u8,
nested: NestedNonZero<T>,
nested: NestedNonZero,
post: u16,
}
pub struct NestedNonZero<T: Zeroable> {
pub struct NestedNonZero {
pre: u8,
val: NonZero<T>,
val: NonZeroU32,
post: u16,
}
impl<T: Zeroable+One> Default for NestedNonZero<T> {
impl Default for NestedNonZero {
fn default() -> Self {
NestedNonZero { pre: 0, val: NonZero::new(T::one()).unwrap(), post: 0 }
NestedNonZero { pre: 0, val: NonZeroU32::new(1).unwrap(), post: 0 }
}
}
pub trait One {
fn one() -> Self;
}
impl One for u32 {
fn one() -> Self { 1 }
}
pub enum Enum4<A, B, C, D> {
One(A),
Two(B),
@ -79,9 +70,9 @@ pub enum Enum4<A, B, C, D> {
#[start]
fn start(_: isize, _: *const *const u8) -> isize {
let _x: MyOption<NonZero<u32>> = Default::default();
let _x: MyOption<NonZeroU32> = Default::default();
let _y: EmbeddedDiscr = Default::default();
let _z: MyOption<IndirectNonZero<u32>> = Default::default();
let _z: MyOption<IndirectNonZero> = Default::default();
let _a: MyOption<bool> = Default::default();
let _b: MyOption<char> = Default::default();
let _c: MyOption<std::cmp::Ordering> = Default::default();

View File

@ -1,9 +1,9 @@
print-type-size type: `IndirectNonZero<u32>`: 12 bytes, alignment: 4 bytes
print-type-size type: `IndirectNonZero`: 12 bytes, alignment: 4 bytes
print-type-size field `.nested`: 8 bytes
print-type-size field `.post`: 2 bytes
print-type-size field `.pre`: 1 bytes
print-type-size end padding: 1 bytes
print-type-size type: `MyOption<IndirectNonZero<u32>>`: 12 bytes, alignment: 4 bytes
print-type-size type: `MyOption<IndirectNonZero>`: 12 bytes, alignment: 4 bytes
print-type-size variant `None`: 0 bytes
print-type-size variant `Some`: 12 bytes
print-type-size field `.0`: 12 bytes
@ -14,7 +14,7 @@ print-type-size field `.val`: 4 bytes
print-type-size field `.post`: 2 bytes
print-type-size field `.pre`: 1 bytes
print-type-size end padding: 1 bytes
print-type-size type: `NestedNonZero<u32>`: 8 bytes, alignment: 4 bytes
print-type-size type: `NestedNonZero`: 8 bytes, alignment: 4 bytes
print-type-size field `.val`: 4 bytes
print-type-size field `.post`: 2 bytes
print-type-size field `.pre`: 1 bytes
@ -32,12 +32,14 @@ print-type-size type: `MyOption<char>`: 4 bytes, alignment: 4 bytes
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: `MyOption<core::nonzero::NonZero<u32>>`: 4 bytes, alignment: 4 bytes
print-type-size type: `MyOption<std::num::NonZeroU32>`: 4 bytes, alignment: 4 bytes
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
print-type-size variant `One`: 0 bytes
print-type-size field `.0`: 0 bytes