Skip to content

Commit 591d924

Browse files
committed
Use generic NonZero.
1 parent a5efbc0 commit 591d924

File tree

21 files changed

+80
-79
lines changed

21 files changed

+80
-79
lines changed

compiler/rustc_abi/src/layout.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::borrow::{Borrow, Cow};
22
use std::cmp;
33
use std::fmt::{self, Write};
44
use std::iter;
5+
use std::num::NonZero;
56
use std::ops::Bound;
67
use std::ops::Deref;
78

@@ -10,8 +11,8 @@ use tracing::debug;
1011

1112
use crate::{
1213
Abi, AbiAndPrefAlign, Align, FieldsShape, IndexSlice, IndexVec, Integer, LayoutS, Niche,
13-
NonZeroUsize, Primitive, ReprOptions, Scalar, Size, StructKind, TagEncoding, TargetDataLayout,
14-
Variants, WrappingRange,
14+
Primitive, ReprOptions, Scalar, Size, StructKind, TagEncoding, TargetDataLayout, Variants,
15+
WrappingRange,
1516
};
1617

1718
// A variant is absent if it's uninhabited and only has ZST fields.
@@ -327,7 +328,7 @@ pub trait LayoutCalculator {
327328

328329
Some(LayoutS {
329330
variants: Variants::Single { index: VariantIdx::new(0) },
330-
fields: FieldsShape::Union(NonZeroUsize::new(only_variant.len())?),
331+
fields: FieldsShape::Union(NonZero::new(only_variant.len())?),
331332
abi,
332333
largest_niche: None,
333334
align,

compiler/rustc_abi/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#![cfg_attr(feature = "nightly", feature(rustdoc_internals))]
55

66
use std::fmt;
7-
use std::num::{NonZeroUsize, ParseIntError};
7+
use std::num::{NonZero, ParseIntError};
88
use std::ops::{Add, AddAssign, Mul, RangeInclusive, Sub};
99
use std::str::FromStr;
1010

@@ -1147,7 +1147,7 @@ pub enum FieldsShape<FieldIdx: Idx> {
11471147
Primitive,
11481148

11491149
/// All fields start at no offset. The `usize` is the field count.
1150-
Union(NonZeroUsize),
1150+
Union(NonZero<usize>),
11511151

11521152
/// Array/vector-like placement, with all fields of identical types.
11531153
Array { stride: Size, count: u64 },

compiler/rustc_ast/src/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2164,7 +2164,7 @@ pub enum TyKind {
21642164
MacCall(P<MacCall>),
21652165
/// Placeholder for a `va_list`.
21662166
CVarArgs,
2167-
/// Pattern types like `pattern_type!(u32 is 1..=)`, which is the same as `NonZeroU32`,
2167+
/// Pattern types like `pattern_type!(u32 is 1..=)`, which is the same as `NonZero<u32>`,
21682168
/// just as part of the type system.
21692169
Pat(P<Ty>, P<Pat>),
21702170
/// Sometimes we need a dummy value when no error has occurred.

compiler/stable_mir/src/abi.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::ty::{Align, IndexedVal, Ty, VariantIdx};
66
use crate::Error;
77
use crate::Opaque;
88
use std::fmt::{self, Debug};
9-
use std::num::NonZeroUsize;
9+
use std::num::NonZero;
1010
use std::ops::RangeInclusive;
1111

1212
/// A function ABI definition.
@@ -133,7 +133,7 @@ pub enum FieldsShape {
133133
Primitive,
134134

135135
/// All fields start at no offset. The `usize` is the field count.
136-
Union(NonZeroUsize),
136+
Union(NonZero<usize>),
137137

138138
/// Array/vector-like placement, with all fields of identical types.
139139
Array { stride: Size, count: u64 },

library/core/src/iter/adapters/step_by.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
intrinsics,
33
iter::{from_fn, TrustedLen, TrustedRandomAccess},
4-
num::NonZeroUsize,
4+
num::NonZero,
55
ops::{Range, Try},
66
};
77

@@ -42,10 +42,10 @@ impl<I> StepBy<I> {
4242
/// The `step` that was originally passed to `Iterator::step_by(step)`,
4343
/// aka `self.step_minus_one + 1`.
4444
#[inline]
45-
fn original_step(&self) -> NonZeroUsize {
45+
fn original_step(&self) -> NonZero<usize> {
4646
// SAFETY: By type invariant, `step_minus_one` cannot be `MAX`, which
4747
// means the addition cannot overflow and the result cannot be zero.
48-
unsafe { NonZeroUsize::new_unchecked(intrinsics::unchecked_add(self.step_minus_one, 1)) }
48+
unsafe { NonZero::new_unchecked(intrinsics::unchecked_add(self.step_minus_one, 1)) }
4949
}
5050
}
5151

@@ -231,12 +231,12 @@ unsafe impl<I: Iterator> StepByImpl<I> for StepBy<I> {
231231
#[inline]
232232
default fn spec_size_hint(&self) -> (usize, Option<usize>) {
233233
#[inline]
234-
fn first_size(step: NonZeroUsize) -> impl Fn(usize) -> usize {
234+
fn first_size(step: NonZero<usize>) -> impl Fn(usize) -> usize {
235235
move |n| if n == 0 { 0 } else { 1 + (n - 1) / step }
236236
}
237237

238238
#[inline]
239-
fn other_size(step: NonZeroUsize) -> impl Fn(usize) -> usize {
239+
fn other_size(step: NonZero<usize>) -> impl Fn(usize) -> usize {
240240
move |n| n / step
241241
}
242242

src/tools/build-manifest/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ fn main() {
236236
let num_threads = if let Some(num) = env::var_os("BUILD_MANIFEST_NUM_THREADS") {
237237
num.to_str().unwrap().parse().expect("invalid number for BUILD_MANIFEST_NUM_THREADS")
238238
} else {
239-
std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get)
239+
std::thread::available_parallelism().map_or(1, std::num::NonZero::get)
240240
};
241241
rayon::ThreadPoolBuilder::new()
242242
.num_threads(num_threads)

src/tools/clippy/clippy_lints/src/transmute/eager_transmute.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub(super) fn check<'tcx>(
8787
&& is_normalizable(cx, cx.param_env, from_ty)
8888
&& is_normalizable(cx, cx.param_env, to_ty)
8989
// we only want to lint if the target type has a niche that is larger than the one of the source type
90-
// e.g. `u8` to `NonZeroU8` should lint, but `NonZeroU8` to `u8` should not
90+
// e.g. `u8` to `NonZero<u8>` should lint, but `NonZero<u8>` to `u8` should not
9191
&& let Ok(from_layout) = cx.tcx.layout_of(cx.param_env.and(from_ty))
9292
&& let Ok(to_layout) = cx.tcx.layout_of(cx.param_env.and(to_ty))
9393
&& match (from_layout.largest_niche, to_layout.largest_niche) {

src/tools/clippy/lintcheck/src/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clap::Parser;
2-
use std::num::NonZeroUsize;
2+
use std::num::NonZero;
33
use std::path::PathBuf;
44

55
#[derive(Clone, Debug, Parser)]
@@ -61,7 +61,7 @@ impl LintcheckConfig {
6161
config.max_jobs = if config.fix || config.recursive {
6262
1
6363
} else {
64-
std::thread::available_parallelism().map_or(1, NonZeroUsize::get)
64+
std::thread::available_parallelism().map_or(1, NonZero::get)
6565
};
6666
};
6767

src/tools/clippy/tests/ui/arithmetic_side_effects.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
extern crate proc_macro_derive;
1717

18-
use core::num::{NonZeroUsize, Saturating, Wrapping};
18+
use core::num::{NonZero, Saturating, Wrapping};
1919

2020
const ONE: i32 = 1;
2121
const ZERO: i32 = 0;
@@ -494,15 +494,15 @@ pub fn issue_11262() {
494494
}
495495

496496
pub fn issue_11392() {
497-
fn example_div(unsigned: usize, nonzero_unsigned: NonZeroUsize) -> usize {
497+
fn example_div(unsigned: usize, nonzero_unsigned: NonZero<usize>) -> usize {
498498
unsigned / nonzero_unsigned
499499
}
500500

501-
fn example_rem(unsigned: usize, nonzero_unsigned: NonZeroUsize) -> usize {
501+
fn example_rem(unsigned: usize, nonzero_unsigned: NonZero<usize>) -> usize {
502502
unsigned % nonzero_unsigned
503503
}
504504

505-
let (unsigned, nonzero_unsigned) = (0, NonZeroUsize::new(1).unwrap());
505+
let (unsigned, nonzero_unsigned) = (0, NonZero::new(1).unwrap());
506506
example_div(unsigned, nonzero_unsigned);
507507
example_rem(unsigned, nonzero_unsigned);
508508
}

src/tools/clippy/tests/ui/eager_transmute.fixed

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![warn(clippy::eager_transmute)]
33
#![allow(clippy::transmute_int_to_non_zero, clippy::missing_transmute_annotations)]
44

5-
use std::num::NonZeroU8;
5+
use std::num::NonZero;
66

77
#[repr(u8)]
88
enum Opcode {
@@ -85,21 +85,21 @@ macro_rules! impls {
8585
}
8686
impls!(NonMaxU8, NonZeroNonMaxU8);
8787

88-
fn niche_tests(v1: u8, v2: NonZeroU8, v3: NonZeroNonMaxU8) {
89-
// u8 -> NonZeroU8, do lint
90-
let _: Option<NonZeroU8> = (v1 > 0).then(|| unsafe { std::mem::transmute(v1) });
88+
fn niche_tests(v1: u8, v2: NonZero<u8>, v3: NonZeroNonMaxU8) {
89+
// u8 -> NonZero<u8>, do lint
90+
let _: Option<NonZero<u8>> = (v1 > 0).then(|| unsafe { std::mem::transmute(v1) });
9191

92-
// NonZeroU8 -> u8, don't lint, target type has no niche and therefore a higher validity range
93-
let _: Option<u8> = (v2 > NonZeroU8::new(1).unwrap()).then_some(unsafe { std::mem::transmute(v2) });
92+
// NonZero<u8> -> u8, don't lint, target type has no niche and therefore a higher validity range
93+
let _: Option<u8> = (v2 > NonZero::new(1u8).unwrap()).then_some(unsafe { std::mem::transmute(v2) });
9494

95-
// NonZeroU8 -> NonMaxU8, do lint, different niche
96-
let _: Option<NonMaxU8> = (v2 < NonZeroU8::new(255).unwrap()).then(|| unsafe { std::mem::transmute(v2) });
95+
// NonZero<u8> -> NonMaxU8, do lint, different niche
96+
let _: Option<NonMaxU8> = (v2 < NonZero::new(255u8).unwrap()).then(|| unsafe { std::mem::transmute(v2) });
9797

9898
// NonZeroNonMaxU8 -> NonMaxU8, don't lint, target type has more validity
9999
let _: Option<NonMaxU8> = (v3 < 255).then_some(unsafe { std::mem::transmute(v2) });
100100

101-
// NonZeroU8 -> NonZeroNonMaxU8, do lint, target type has less validity
102-
let _: Option<NonZeroNonMaxU8> = (v2 < NonZeroU8::new(255).unwrap()).then(|| unsafe { std::mem::transmute(v2) });
101+
// NonZero<u8> -> NonZeroNonMaxU8, do lint, target type has less validity
102+
let _: Option<NonZeroNonMaxU8> = (v2 < NonZero::new(255u8).unwrap()).then(|| unsafe { std::mem::transmute(v2) });
103103
}
104104

105105
fn main() {}

src/tools/clippy/tests/ui/eager_transmute.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![warn(clippy::eager_transmute)]
33
#![allow(clippy::transmute_int_to_non_zero, clippy::missing_transmute_annotations)]
44

5-
use std::num::NonZeroU8;
5+
use std::num::NonZero;
66

77
#[repr(u8)]
88
enum Opcode {
@@ -85,21 +85,21 @@ macro_rules! impls {
8585
}
8686
impls!(NonMaxU8, NonZeroNonMaxU8);
8787

88-
fn niche_tests(v1: u8, v2: NonZeroU8, v3: NonZeroNonMaxU8) {
89-
// u8 -> NonZeroU8, do lint
90-
let _: Option<NonZeroU8> = (v1 > 0).then_some(unsafe { std::mem::transmute(v1) });
88+
fn niche_tests(v1: u8, v2: NonZero<u8>, v3: NonZeroNonMaxU8) {
89+
// u8 -> NonZero<u8>, do lint
90+
let _: Option<NonZero<u8>> = (v1 > 0).then_some(unsafe { std::mem::transmute(v1) });
9191

92-
// NonZeroU8 -> u8, don't lint, target type has no niche and therefore a higher validity range
93-
let _: Option<u8> = (v2 > NonZeroU8::new(1).unwrap()).then_some(unsafe { std::mem::transmute(v2) });
92+
// NonZero<u8> -> u8, don't lint, target type has no niche and therefore a higher validity range
93+
let _: Option<u8> = (v2 > NonZero::new(1u8).unwrap()).then_some(unsafe { std::mem::transmute(v2) });
9494

95-
// NonZeroU8 -> NonMaxU8, do lint, different niche
96-
let _: Option<NonMaxU8> = (v2 < NonZeroU8::new(255).unwrap()).then_some(unsafe { std::mem::transmute(v2) });
95+
// NonZero<u8> -> NonMaxU8, do lint, different niche
96+
let _: Option<NonMaxU8> = (v2 < NonZero::new(255u8).unwrap()).then_some(unsafe { std::mem::transmute(v2) });
9797

9898
// NonZeroNonMaxU8 -> NonMaxU8, don't lint, target type has more validity
9999
let _: Option<NonMaxU8> = (v3 < 255).then_some(unsafe { std::mem::transmute(v2) });
100100

101-
// NonZeroU8 -> NonZeroNonMaxU8, do lint, target type has less validity
102-
let _: Option<NonZeroNonMaxU8> = (v2 < NonZeroU8::new(255).unwrap()).then_some(unsafe { std::mem::transmute(v2) });
101+
// NonZero<u8> -> NonZeroNonMaxU8, do lint, target type has less validity
102+
let _: Option<NonZeroNonMaxU8> = (v2 < NonZero::new(255u8).unwrap()).then_some(unsafe { std::mem::transmute(v2) });
103103
}
104104

105105
fn main() {}

src/tools/clippy/tests/ui/eager_transmute.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -157,34 +157,34 @@ LL | (op < 4).then(|| std::mem::transmute::<_, Opcode>(op));
157157
error: this transmute is always evaluated eagerly, even if the condition is false
158158
--> tests/ui/eager_transmute.rs:90:60
159159
|
160-
LL | let _: Option<NonZeroU8> = (v1 > 0).then_some(unsafe { std::mem::transmute(v1) });
161-
| ^^^^^^^^^^^^^^^^^^^^^^^
160+
LL | let _: Option<NonZero<u8>> = (v1 > 0).then_some(unsafe { std::mem::transmute(v1) });
161+
| ^^^^^^^^^^^^^^^^^^^^^^^
162162
|
163163
help: consider using `bool::then` to only transmute if the condition holds
164164
|
165-
LL | let _: Option<NonZeroU8> = (v1 > 0).then(|| unsafe { std::mem::transmute(v1) });
166-
| ~~~~ ++
165+
LL | let _: Option<NonZero<u8>> = (v1 > 0).then(|| unsafe { std::mem::transmute(v1) });
166+
| ~~~~ ++
167167

168168
error: this transmute is always evaluated eagerly, even if the condition is false
169169
--> tests/ui/eager_transmute.rs:96:86
170170
|
171-
LL | let _: Option<NonMaxU8> = (v2 < NonZeroU8::new(255).unwrap()).then_some(unsafe { std::mem::transmute(v2) });
171+
LL | let _: Option<NonMaxU8> = (v2 < NonZero::new(255u8).unwrap()).then_some(unsafe { std::mem::transmute(v2) });
172172
| ^^^^^^^^^^^^^^^^^^^^^^^
173173
|
174174
help: consider using `bool::then` to only transmute if the condition holds
175175
|
176-
LL | let _: Option<NonMaxU8> = (v2 < NonZeroU8::new(255).unwrap()).then(|| unsafe { std::mem::transmute(v2) });
176+
LL | let _: Option<NonMaxU8> = (v2 < NonZero::new(255u8).unwrap()).then(|| unsafe { std::mem::transmute(v2) });
177177
| ~~~~ ++
178178

179179
error: this transmute is always evaluated eagerly, even if the condition is false
180180
--> tests/ui/eager_transmute.rs:102:93
181181
|
182-
LL | let _: Option<NonZeroNonMaxU8> = (v2 < NonZeroU8::new(255).unwrap()).then_some(unsafe { std::mem::transmute(v2) });
182+
LL | let _: Option<NonZeroNonMaxU8> = (v2 < NonZero::new(255u8).unwrap()).then_some(unsafe { std::mem::transmute(v2) });
183183
| ^^^^^^^^^^^^^^^^^^^^^^^
184184
|
185185
help: consider using `bool::then` to only transmute if the condition holds
186186
|
187-
LL | let _: Option<NonZeroNonMaxU8> = (v2 < NonZeroU8::new(255).unwrap()).then(|| unsafe { std::mem::transmute(v2) });
187+
LL | let _: Option<NonZeroNonMaxU8> = (v2 < NonZero::new(255u8).unwrap()).then(|| unsafe { std::mem::transmute(v2) });
188188
| ~~~~ ++
189189

190190
error: aborting due to 17 previous errors

src/tools/miri/tests/fail/enum-set-discriminant-niche-variant-wrong.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![feature(custom_mir)]
33

44
use std::intrinsics::mir::*;
5-
use std::num::NonZeroI32;
5+
use std::num::NonZero;
66

77
// We define our own option type so that we can control the variant indices.
88
#[allow(unused)]
@@ -13,7 +13,7 @@ enum Option<T> {
1313
use Option::*;
1414

1515
#[custom_mir(dialect = "runtime", phase = "optimized")]
16-
fn set_discriminant(ptr: &mut Option<NonZeroI32>) {
16+
fn set_discriminant(ptr: &mut Option<NonZero<i32>>) {
1717
mir! {
1818
{
1919
// We set the discriminant to `Some`, which is a NOP since this is the niched variant.
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use std::num::*;
1+
use std::num::NonZero;
22

33
#[repr(C)]
4-
struct S1(NonZeroI32);
4+
struct S1(NonZero<i32>);
55

66
#[repr(C)]
77
struct S2(i32);
@@ -11,6 +11,6 @@ fn callee(_s: S2) {}
1111
fn main() {
1212
let fnptr: fn(S2) = callee;
1313
let fnptr: fn(S1) = unsafe { std::mem::transmute(fnptr) };
14-
fnptr(S1(NonZeroI32::new(1).unwrap()));
14+
fnptr(S1(NonZero::new(1).unwrap()));
1515
//~^ ERROR: calling a function with argument of type S2 passing data of type S1
1616
}

src/tools/miri/tests/fail/validity/cast_fn_ptr_invalid_callee_ret.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
#![feature(core_intrinsics, custom_mir)]
33

44
use std::intrinsics::mir::*;
5-
use std::num::NonZeroU32;
5+
use std::num::NonZero;
66
use std::ptr;
77

8-
// This function supposedly returns a NonZeroU32, but actually returns something invalid in a way that
9-
// never materializes a bad NonZeroU32 value: we take a pointer to the return place and cast the pointer
8+
// This function supposedly returns a `NonZero<u32>`, but actually returns something invalid in a way that
9+
// never materializes a bad `NonZero<u32>` value: we take a pointer to the return place and cast the pointer
1010
// type. That way we never get an "invalid value constructed" error inside the function, it can
1111
// only possibly be detected when the return value is passed to the caller.
1212
#[custom_mir(dialect = "runtime", phase = "optimized")]
13-
fn f() -> NonZeroU32 {
13+
fn f() -> NonZero<u32> {
1414
mir! {
1515
{
1616
let tmp = ptr::addr_of_mut!(RET);
@@ -22,7 +22,7 @@ fn f() -> NonZeroU32 {
2222
}
2323

2424
fn main() {
25-
let f: fn() -> u32 = unsafe { std::mem::transmute(f as fn() -> NonZeroU32) };
26-
// There's a NonZeroU32-to-u32 transmute happening here
25+
let f: fn() -> u32 = unsafe { std::mem::transmute(f as fn() -> NonZero<u32>) };
26+
// There's a `NonZero<u32>` to `u32` transmute happening here.
2727
f(); //~ERROR: expected something greater or equal to 1
2828
}

src/tools/miri/tests/fail/validity/cast_fn_ptr_invalid_caller_arg.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22
#![feature(core_intrinsics, custom_mir)]
33

44
use std::intrinsics::mir::*;
5-
use std::num::NonZeroU32;
5+
use std::num::NonZero;
66
use std::ptr;
77

88
fn f(c: u32) {
99
println!("{c}");
1010
}
1111

12-
// Call that function in a bad way, with an invalid NonZeroU32, but without
13-
// ever materializing this as a NonZeroU32 value outside the call itself.
12+
// Call that function in a bad way, with an invalid `NonZero<u32>`, but without
13+
// ever materializing this as a `NonZero<u32>` value outside the call itself.
1414
#[custom_mir(dialect = "runtime", phase = "optimized")]
15-
fn call(f: fn(NonZeroU32)) {
15+
fn call(f: fn(NonZero<u32>)) {
1616
mir! {
1717
let _res: ();
1818
{
1919
let c = 0;
2020
let tmp = ptr::addr_of!(c);
21-
let ptr = tmp as *const NonZeroU32;
22-
// The call site now is a NonZeroU32-to-u32 transmute.
21+
let ptr = tmp as *const NonZero<u32>;
22+
// The call site now is a `NonZero<u32>` to `u32` transmute.
2323
Call(_res = f(*ptr), ReturnTo(retblock), UnwindContinue()) //~ERROR: expected something greater or equal to 1
2424
}
2525
retblock = {
@@ -29,6 +29,6 @@ fn call(f: fn(NonZeroU32)) {
2929
}
3030

3131
fn main() {
32-
let f: fn(NonZeroU32) = unsafe { std::mem::transmute(f as fn(u32)) };
32+
let f: fn(NonZero<u32>) = unsafe { std::mem::transmute(f as fn(u32)) };
3333
call(f);
3434
}

0 commit comments

Comments
 (0)