Skip to content

Commit

Permalink
Use generic NonZero.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed May 5, 2024
1 parent f5fdd55 commit 0f851d2
Show file tree
Hide file tree
Showing 22 changed files with 83 additions and 82 deletions.
7 changes: 4 additions & 3 deletions compiler/rustc_abi/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::borrow::{Borrow, Cow};
use std::cmp;
use std::fmt::{self, Write};
use std::iter;
use std::num::NonZero;
use std::ops::Bound;
use std::ops::Deref;

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

use crate::{
Abi, AbiAndPrefAlign, Align, FieldsShape, IndexSlice, IndexVec, Integer, LayoutS, Niche,
NonZeroUsize, Primitive, ReprOptions, Scalar, Size, StructKind, TagEncoding, TargetDataLayout,
Variants, WrappingRange,
Primitive, ReprOptions, Scalar, Size, StructKind, TagEncoding, TargetDataLayout, Variants,
WrappingRange,
};

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

Some(LayoutS {
variants: Variants::Single { index: VariantIdx::new(0) },
fields: FieldsShape::Union(NonZeroUsize::new(only_variant.len())?),
fields: FieldsShape::Union(NonZero::new(only_variant.len())?),
abi,
largest_niche: None,
align,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![cfg_attr(feature = "nightly", feature(rustdoc_internals))]

use std::fmt;
use std::num::{NonZeroUsize, ParseIntError};
use std::num::{NonZero, ParseIntError};
use std::ops::{Add, AddAssign, Mul, RangeInclusive, Sub};
use std::str::FromStr;

Expand Down Expand Up @@ -1149,7 +1149,7 @@ pub enum FieldsShape<FieldIdx: Idx> {
Primitive,

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

/// Array/vector-like placement, with all fields of identical types.
Array { stride: Size, count: u64 },
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2164,7 +2164,7 @@ pub enum TyKind {
MacCall(P<MacCall>),
/// Placeholder for a `va_list`.
CVarArgs,
/// Pattern types like `pattern_type!(u32 is 1..=)`, which is the same as `NonZeroU32`,
/// Pattern types like `pattern_type!(u32 is 1..=)`, which is the same as `NonZero<u32>`,
/// just as part of the type system.
Pat(P<Ty>, P<Pat>),
/// Sometimes we need a dummy value when no error has occurred.
Expand Down
4 changes: 2 additions & 2 deletions compiler/stable_mir/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::ty::{Align, IndexedVal, Ty, VariantIdx};
use crate::Error;
use crate::Opaque;
use std::fmt::{self, Debug};
use std::num::NonZeroUsize;
use std::num::NonZero;
use std::ops::RangeInclusive;

/// A function ABI definition.
Expand Down Expand Up @@ -133,7 +133,7 @@ pub enum FieldsShape {
Primitive,

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

/// Array/vector-like placement, with all fields of identical types.
Array { stride: Size, count: u64 },
Expand Down
10 changes: 5 additions & 5 deletions library/core/src/iter/adapters/step_by.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
intrinsics,
iter::{from_fn, TrustedLen, TrustedRandomAccess},
num::NonZeroUsize,
num::NonZero,
ops::{Range, Try},
};

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/tools/build-manifest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ fn main() {
let num_threads = if let Some(num) = env::var_os("BUILD_MANIFEST_NUM_THREADS") {
num.to_str().unwrap().parse().expect("invalid number for BUILD_MANIFEST_NUM_THREADS")
} else {
std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get)
std::thread::available_parallelism().map_or(1, std::num::NonZero::get)
};
rayon::ThreadPoolBuilder::new()
.num_threads(num_threads)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub(super) fn check<'tcx>(
&& is_normalizable(cx, cx.param_env, from_ty)
&& is_normalizable(cx, cx.param_env, to_ty)
// we only want to lint if the target type has a niche that is larger than the one of the source type
// e.g. `u8` to `NonZeroU8` should lint, but `NonZeroU8` to `u8` should not
// e.g. `u8` to `NonZero<u8>` should lint, but `NonZero<u8>` to `u8` should not
&& let Ok(from_layout) = cx.tcx.layout_of(cx.param_env.and(from_ty))
&& let Ok(to_layout) = cx.tcx.layout_of(cx.param_env.and(to_ty))
&& match (from_layout.largest_niche, to_layout.largest_niche) {
Expand Down
4 changes: 2 additions & 2 deletions src/tools/clippy/lintcheck/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Parser;
use std::num::NonZeroUsize;
use std::num::NonZero;
use std::path::PathBuf;

#[derive(Clone, Debug, Parser)]
Expand Down Expand Up @@ -61,7 +61,7 @@ impl LintcheckConfig {
config.max_jobs = if config.fix || config.recursive {
1
} else {
std::thread::available_parallelism().map_or(1, NonZeroUsize::get)
std::thread::available_parallelism().map_or(1, NonZero::get)
};
};

Expand Down
8 changes: 4 additions & 4 deletions src/tools/clippy/tests/ui/arithmetic_side_effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

extern crate proc_macro_derive;

use core::num::{NonZeroUsize, Saturating, Wrapping};
use core::num::{NonZero, Saturating, Wrapping};

const ONE: i32 = 1;
const ZERO: i32 = 0;
Expand Down Expand Up @@ -494,15 +494,15 @@ pub fn issue_11262() {
}

pub fn issue_11392() {
fn example_div(unsigned: usize, nonzero_unsigned: NonZeroUsize) -> usize {
fn example_div(unsigned: usize, nonzero_unsigned: NonZero<usize>) -> usize {
unsigned / nonzero_unsigned
}

fn example_rem(unsigned: usize, nonzero_unsigned: NonZeroUsize) -> usize {
fn example_rem(unsigned: usize, nonzero_unsigned: NonZero<usize>) -> usize {
unsigned % nonzero_unsigned
}

let (unsigned, nonzero_unsigned) = (0, NonZeroUsize::new(1).unwrap());
let (unsigned, nonzero_unsigned) = (0, NonZero::new(1).unwrap());
example_div(unsigned, nonzero_unsigned);
example_rem(unsigned, nonzero_unsigned);
}
Expand Down
20 changes: 10 additions & 10 deletions src/tools/clippy/tests/ui/eager_transmute.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![warn(clippy::eager_transmute)]
#![allow(clippy::transmute_int_to_non_zero, clippy::missing_transmute_annotations)]

use std::num::NonZeroU8;
use std::num::NonZero;

#[repr(u8)]
enum Opcode {
Expand Down Expand Up @@ -85,21 +85,21 @@ macro_rules! impls {
}
impls!(NonMaxU8, NonZeroNonMaxU8);

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

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

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

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

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

fn main() {}
20 changes: 10 additions & 10 deletions src/tools/clippy/tests/ui/eager_transmute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![warn(clippy::eager_transmute)]
#![allow(clippy::transmute_int_to_non_zero, clippy::missing_transmute_annotations)]

use std::num::NonZeroU8;
use std::num::NonZero;

#[repr(u8)]
enum Opcode {
Expand Down Expand Up @@ -85,21 +85,21 @@ macro_rules! impls {
}
impls!(NonMaxU8, NonZeroNonMaxU8);

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

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

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

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

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

fn main() {}
18 changes: 9 additions & 9 deletions src/tools/clippy/tests/ui/eager_transmute.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -155,36 +155,36 @@ LL | (op < 4).then(|| std::mem::transmute::<_, Opcode>(op));
| ~~~~ ++

error: this transmute is always evaluated eagerly, even if the condition is false
--> tests/ui/eager_transmute.rs:90:60
--> tests/ui/eager_transmute.rs:90:62
|
LL | let _: Option<NonZeroU8> = (v1 > 0).then_some(unsafe { std::mem::transmute(v1) });
| ^^^^^^^^^^^^^^^^^^^^^^^
LL | let _: Option<NonZero<u8>> = (v1 > 0).then_some(unsafe { std::mem::transmute(v1) });
| ^^^^^^^^^^^^^^^^^^^^^^^
|
help: consider using `bool::then` to only transmute if the condition holds
|
LL | let _: Option<NonZeroU8> = (v1 > 0).then(|| unsafe { std::mem::transmute(v1) });
| ~~~~ ++
LL | let _: Option<NonZero<u8>> = (v1 > 0).then(|| unsafe { std::mem::transmute(v1) });
| ~~~~ ++

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

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

error: aborting due to 17 previous errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![feature(custom_mir)]

use std::intrinsics::mir::*;
use std::num::NonZeroI32;
use std::num::NonZero;

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

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

#[repr(C)]
struct S1(NonZeroI32);
struct S1(NonZero<i32>);

#[repr(C)]
struct S2(i32);
Expand All @@ -11,6 +11,6 @@ fn callee(_s: S2) {}
fn main() {
let fnptr: fn(S2) = callee;
let fnptr: fn(S1) = unsafe { std::mem::transmute(fnptr) };
fnptr(S1(NonZeroI32::new(1).unwrap()));
fnptr(S1(NonZero::new(1).unwrap()));
//~^ ERROR: calling a function with argument of type S2 passing data of type S1
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: Undefined Behavior: calling a function with argument of type S2 passing data of type S1
--> $DIR/abi_mismatch_repr_C.rs:LL:CC
|
LL | fnptr(S1(NonZeroI32::new(1).unwrap()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ calling a function with argument of type S2 passing data of type S1
LL | fnptr(S1(NonZero::new(1).unwrap()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ calling a function with argument of type S2 passing data of type S1
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
#![feature(core_intrinsics, custom_mir)]

use std::intrinsics::mir::*;
use std::num::NonZeroU32;
use std::num::NonZero;
use std::ptr;

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

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

0 comments on commit 0f851d2

Please sign in to comment.