Skip to content

Commit 67f46ce

Browse files
committed
Use num::NonZero* instead of NonZero<_> in rustc and tests
1 parent 2d13ddb commit 67f46ce

File tree

8 files changed

+38
-46
lines changed

8 files changed

+38
-46
lines changed

src/libcore/tests/nonzero.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,36 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use core::nonzero::NonZero;
11+
use core::num::NonZeroU32;
1212
use core::option::Option;
1313
use core::option::Option::{Some, None};
1414
use std::mem::size_of;
1515

1616
#[test]
1717
fn test_create_nonzero_instance() {
1818
let _a = unsafe {
19-
NonZero::new_unchecked(21)
19+
NonZeroU32::new_unchecked(21)
2020
};
2121
}
2222

2323
#[test]
2424
fn test_size_nonzero_in_option() {
25-
assert_eq!(size_of::<NonZero<u32>>(), size_of::<Option<NonZero<u32>>>());
25+
assert_eq!(size_of::<NonZeroU32>(), size_of::<Option<NonZeroU32>>());
2626
}
2727

2828
#[test]
2929
fn test_match_on_nonzero_option() {
3030
let a = Some(unsafe {
31-
NonZero::new_unchecked(42)
31+
NonZeroU32::new_unchecked(42)
3232
});
3333
match a {
3434
Some(val) => assert_eq!(val.get(), 42),
35-
None => panic!("unexpected None while matching on Some(NonZero(_))")
35+
None => panic!("unexpected None while matching on Some(NonZeroU32(_))")
3636
}
3737

38-
match unsafe { Some(NonZero::new_unchecked(43)) } {
38+
match unsafe { Some(NonZeroU32::new_unchecked(43)) } {
3939
Some(val) => assert_eq!(val.get(), 43),
40-
None => panic!("unexpected None while matching on Some(NonZero(_))")
40+
None => panic!("unexpected None while matching on Some(NonZeroU32(_))")
4141
}
4242
}
4343

src/librustc/ty/subst.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ use syntax_pos::{Span, DUMMY_SP};
1919
use rustc_data_structures::accumulate_vec::AccumulateVec;
2020

2121
use core::intrinsics;
22-
use core::nonzero::NonZero;
2322
use std::fmt;
2423
use std::iter;
2524
use std::marker::PhantomData;
2625
use std::mem;
26+
use std::num::NonZeroUsize;
2727

2828
/// An entity in the Rust typesystem, which can be one of
2929
/// several kinds (only types and lifetimes for now).
@@ -32,7 +32,7 @@ use std::mem;
3232
/// indicate the type (`Ty` or `Region`) it points to.
3333
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
3434
pub struct Kind<'tcx> {
35-
ptr: NonZero<usize>,
35+
ptr: NonZeroUsize,
3636
marker: PhantomData<(Ty<'tcx>, ty::Region<'tcx>)>
3737
}
3838

@@ -63,7 +63,7 @@ impl<'tcx> UnpackedKind<'tcx> {
6363

6464
Kind {
6565
ptr: unsafe {
66-
NonZero::new_unchecked(ptr | tag)
66+
NonZeroUsize::new_unchecked(ptr | tag)
6767
},
6868
marker: PhantomData
6969
}

src/librustc_data_structures/obligation_forest/node_index.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use core::nonzero::NonZero;
11+
use std::num::NonZeroU32;
1212
use std::u32;
1313

1414
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1515
pub struct NodeIndex {
16-
index: NonZero<u32>,
16+
index: NonZeroU32,
1717
}
1818

1919
impl NodeIndex {
2020
pub fn new(value: usize) -> NodeIndex {
2121
assert!(value < (u32::MAX as usize));
22-
NodeIndex { index: NonZero::new((value as u32) + 1).unwrap() }
22+
NodeIndex { index: NonZeroU32::new((value as u32) + 1).unwrap() }
2323
}
2424

2525
pub fn get(self) -> usize {

src/librustc_mir/dataflow/move_paths/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ mod abs_domain;
2929
// (which is likely to yield a subtle off-by-one error).
3030
pub(crate) mod indexes {
3131
use std::fmt;
32-
use core::nonzero::NonZero;
32+
use std::num::NonZeroUsize;
3333
use rustc_data_structures::indexed_vec::Idx;
3434

3535
macro_rules! new_index {
3636
($Index:ident, $debug_name:expr) => {
3737
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
38-
pub struct $Index(NonZero<usize>);
38+
pub struct $Index(NonZeroUsize);
3939

4040
impl Idx for $Index {
4141
fn new(idx: usize) -> Self {
42-
$Index(NonZero::new(idx + 1).unwrap())
42+
$Index(NonZeroUsize::new(idx + 1).unwrap())
4343
}
4444
fn index(self) -> usize {
4545
self.0.get() - 1

src/test/run-pass/enum-null-pointer-opt.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010

1111
#![feature(nonzero, core)]
1212

13-
extern crate core;
14-
15-
use core::nonzero::NonZero;
1613
use std::mem::size_of;
14+
use std::num::NonZeroUsize;
15+
use std::ptr::NonNull;
1716
use std::rc::Rc;
1817
use std::sync::Arc;
1918

@@ -59,8 +58,8 @@ fn main() {
5958
assert_eq!(size_of::<[Box<isize>; 1]>(), size_of::<Option<[Box<isize>; 1]>>());
6059

6160
// Should apply to NonZero
62-
assert_eq!(size_of::<NonZero<usize>>(), size_of::<Option<NonZero<usize>>>());
63-
assert_eq!(size_of::<NonZero<*mut i8>>(), size_of::<Option<NonZero<*mut i8>>>());
61+
assert_eq!(size_of::<NonZeroUsize>(), size_of::<Option<NonZeroUsize>>());
62+
assert_eq!(size_of::<NonNull<i8>>(), size_of::<Option<NonNull<i8>>>());
6463

6564
// Should apply to types that use NonZero internally
6665
assert_eq!(size_of::<Vec<isize>>(), size_of::<Option<Vec<isize>>>());

src/test/run-pass/issue-23433.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// Don't fail if we encounter a NonZero<*T> where T is an unsized type
11+
// Don't fail if we encounter a NonNull<T> where T is an unsized type
1212

1313
use std::ptr::NonNull;
1414

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

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// This file illustrates how niche-filling enums are handled,
1515
// modelled after cases like `Option<&u32>`, `Option<bool>` and such.
1616
//
17-
// It uses NonZero directly, rather than `&_` or `Unique<_>`, because
17+
// It uses NonZeroU32 rather than `&_` or `Unique<_>`, because
1818
// the test is not set up to deal with target-dependent pointer width.
1919
//
2020
// It avoids using u64/i64 because on some targets that is only 4-byte
@@ -25,8 +25,7 @@
2525
#![feature(nonzero)]
2626
#![allow(dead_code)]
2727

28-
extern crate core;
29-
use core::nonzero::{NonZero, Zeroable};
28+
use std::num::NonZeroU32;
3029

3130
pub enum MyOption<T> { None, Some(T) }
3231

@@ -36,40 +35,32 @@ impl<T> Default for MyOption<T> {
3635

3736
pub enum EmbeddedDiscr {
3837
None,
39-
Record { pre: u8, val: NonZero<u32>, post: u16 },
38+
Record { pre: u8, val: NonZeroU32, post: u16 },
4039
}
4140

4241
impl Default for EmbeddedDiscr {
4342
fn default() -> Self { EmbeddedDiscr::None }
4443
}
4544

4645
#[derive(Default)]
47-
pub struct IndirectNonZero<T: Zeroable + One> {
46+
pub struct IndirectNonZero {
4847
pre: u8,
49-
nested: NestedNonZero<T>,
48+
nested: NestedNonZero,
5049
post: u16,
5150
}
5251

53-
pub struct NestedNonZero<T: Zeroable> {
52+
pub struct NestedNonZero {
5453
pre: u8,
55-
val: NonZero<T>,
54+
val: NonZeroU32,
5655
post: u16,
5756
}
5857

59-
impl<T: Zeroable+One> Default for NestedNonZero<T> {
58+
impl Default for NestedNonZero {
6059
fn default() -> Self {
61-
NestedNonZero { pre: 0, val: NonZero::new(T::one()).unwrap(), post: 0 }
60+
NestedNonZero { pre: 0, val: NonZeroU32::new(1).unwrap(), post: 0 }
6261
}
6362
}
6463

65-
pub trait One {
66-
fn one() -> Self;
67-
}
68-
69-
impl One for u32 {
70-
fn one() -> Self { 1 }
71-
}
72-
7364
pub enum Enum4<A, B, C, D> {
7465
One(A),
7566
Two(B),
@@ -79,9 +70,9 @@ pub enum Enum4<A, B, C, D> {
7970

8071
#[start]
8172
fn start(_: isize, _: *const *const u8) -> isize {
82-
let _x: MyOption<NonZero<u32>> = Default::default();
73+
let _x: MyOption<NonZeroU32> = Default::default();
8374
let _y: EmbeddedDiscr = Default::default();
84-
let _z: MyOption<IndirectNonZero<u32>> = Default::default();
75+
let _z: MyOption<IndirectNonZero> = Default::default();
8576
let _a: MyOption<bool> = Default::default();
8677
let _b: MyOption<char> = Default::default();
8778
let _c: MyOption<std::cmp::Ordering> = Default::default();

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
print-type-size type: `IndirectNonZero<u32>`: 12 bytes, alignment: 4 bytes
1+
print-type-size type: `IndirectNonZero`: 12 bytes, alignment: 4 bytes
22
print-type-size field `.nested`: 8 bytes
33
print-type-size field `.post`: 2 bytes
44
print-type-size field `.pre`: 1 bytes
55
print-type-size end padding: 1 bytes
6-
print-type-size type: `MyOption<IndirectNonZero<u32>>`: 12 bytes, alignment: 4 bytes
6+
print-type-size type: `MyOption<IndirectNonZero>`: 12 bytes, alignment: 4 bytes
77
print-type-size variant `None`: 0 bytes
88
print-type-size variant `Some`: 12 bytes
99
print-type-size field `.0`: 12 bytes
@@ -14,7 +14,7 @@ print-type-size field `.val`: 4 bytes
1414
print-type-size field `.post`: 2 bytes
1515
print-type-size field `.pre`: 1 bytes
1616
print-type-size end padding: 1 bytes
17-
print-type-size type: `NestedNonZero<u32>`: 8 bytes, alignment: 4 bytes
17+
print-type-size type: `NestedNonZero`: 8 bytes, alignment: 4 bytes
1818
print-type-size field `.val`: 4 bytes
1919
print-type-size field `.post`: 2 bytes
2020
print-type-size field `.pre`: 1 bytes
@@ -32,12 +32,14 @@ print-type-size type: `MyOption<char>`: 4 bytes, alignment: 4 bytes
3232
print-type-size variant `None`: 0 bytes
3333
print-type-size variant `Some`: 4 bytes
3434
print-type-size field `.0`: 4 bytes
35-
print-type-size type: `MyOption<core::nonzero::NonZero<u32>>`: 4 bytes, alignment: 4 bytes
35+
print-type-size type: `MyOption<std::num::NonZeroU32>`: 4 bytes, alignment: 4 bytes
3636
print-type-size variant `None`: 0 bytes
3737
print-type-size variant `Some`: 4 bytes
3838
print-type-size field `.0`: 4 bytes
3939
print-type-size type: `core::nonzero::NonZero<u32>`: 4 bytes, alignment: 4 bytes
4040
print-type-size field `.0`: 4 bytes
41+
print-type-size type: `std::num::NonZeroU32`: 4 bytes, alignment: 4 bytes
42+
print-type-size field `.0`: 4 bytes
4143
print-type-size type: `Enum4<(), (), (), MyOption<u8>>`: 2 bytes, alignment: 1 bytes
4244
print-type-size variant `One`: 0 bytes
4345
print-type-size field `.0`: 0 bytes

0 commit comments

Comments
 (0)