Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 1be4688

Browse files
committed
Auto merge of rust-lang#120486 - reitermarkus:use-generic-nonzero, r=dtolnay
Use generic `NonZero` internally. Tracking issue: rust-lang#120257
2 parents 0f806a9 + a90cc05 commit 1be4688

File tree

144 files changed

+641
-633
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

144 files changed

+641
-633
lines changed

compiler/rustc_attr/src/builtin.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_session::parse::feature_err;
1313
use rustc_session::{RustcVersion, Session};
1414
use rustc_span::hygiene::Transparency;
1515
use rustc_span::{symbol::sym, symbol::Symbol, Span};
16-
use std::num::NonZeroU32;
16+
use std::num::NonZero;
1717

1818
use crate::session_diagnostics::{self, IncorrectReprFormatGenericCause};
1919

@@ -113,7 +113,7 @@ pub enum StabilityLevel {
113113
/// Reason for the current stability level.
114114
reason: UnstableReason,
115115
/// Relevant `rust-lang/rust` issue.
116-
issue: Option<NonZeroU32>,
116+
issue: Option<NonZero<u32>>,
117117
is_soft: bool,
118118
/// If part of a feature is stabilized and a new feature is added for the remaining parts,
119119
/// then the `implied_by` attribute is used to indicate which now-stable feature previously
@@ -442,7 +442,7 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
442442
// is a name/value pair string literal.
443443
issue_num = match issue.unwrap().as_str() {
444444
"none" => None,
445-
issue => match issue.parse::<NonZeroU32>() {
445+
issue => match issue.parse::<NonZero<u32>>() {
446446
Ok(num) => Some(num),
447447
Err(err) => {
448448
sess.dcx().emit_err(

compiler/rustc_attr/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#![allow(internal_features)]
88
#![feature(rustdoc_internals)]
99
#![doc(rust_logo)]
10+
#![feature(generic_nonzero)]
1011
#![feature(let_chains)]
1112

1213
#[macro_use]

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! to be const-safe.
66
77
use std::fmt::Write;
8-
use std::num::NonZeroUsize;
8+
use std::num::NonZero;
99

1010
use either::{Left, Right};
1111

@@ -785,7 +785,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
785785
fn visit_union(
786786
&mut self,
787787
op: &OpTy<'tcx, M::Provenance>,
788-
_fields: NonZeroUsize,
788+
_fields: NonZero<usize>,
789789
) -> InterpResult<'tcx> {
790790
// Special check for CTFE validation, preventing `UnsafeCell` inside unions in immutable memory.
791791
if self.ctfe_mode.is_some_and(|c| !c.allow_immutable_unsafe_cell()) {

compiler/rustc_const_eval/src/interpret/visitor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_middle::ty;
77
use rustc_target::abi::FieldIdx;
88
use rustc_target::abi::{FieldsShape, VariantIdx, Variants};
99

10-
use std::num::NonZeroUsize;
10+
use std::num::NonZero;
1111

1212
use super::{InterpCx, MPlaceTy, Machine, Projectable};
1313

@@ -43,7 +43,7 @@ pub trait ValueVisitor<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>>: Sized {
4343
}
4444
/// Visits the given value as a union. No automatic recursion can happen here.
4545
#[inline(always)]
46-
fn visit_union(&mut self, _v: &Self::V, _fields: NonZeroUsize) -> InterpResult<'tcx> {
46+
fn visit_union(&mut self, _v: &Self::V, _fields: NonZero<usize>) -> InterpResult<'tcx> {
4747
Ok(())
4848
}
4949
/// Visits the given value as the pointer of a `Box`. There is nothing to recurse into.

compiler/rustc_const_eval/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Rust MIR: a lowered representation of Rust.
1111
#![feature(assert_matches)]
1212
#![feature(box_patterns)]
1313
#![feature(decl_macro)]
14+
#![feature(generic_nonzero)]
1415
#![feature(let_chains)]
1516
#![feature(slice_ptr_get)]
1617
#![feature(never_type)]

compiler/rustc_data_structures/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#![feature(cfg_match)]
2121
#![feature(core_intrinsics)]
2222
#![feature(extend_one)]
23+
#![feature(generic_nonzero)]
2324
#![feature(hash_raw_entry)]
2425
#![feature(hasher_prefixfree_extras)]
2526
#![feature(lazy_cell)]

compiler/rustc_data_structures/src/stable_hasher.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::fmt;
66
use std::hash::{BuildHasher, Hash, Hasher};
77
use std::marker::PhantomData;
88
use std::mem;
9+
use std::num::NonZero;
910

1011
#[cfg(test)]
1112
mod tests;
@@ -338,14 +339,14 @@ impl<CTX, T> HashStable<CTX> for PhantomData<T> {
338339
fn hash_stable(&self, _ctx: &mut CTX, _hasher: &mut StableHasher) {}
339340
}
340341

341-
impl<CTX> HashStable<CTX> for ::std::num::NonZeroU32 {
342+
impl<CTX> HashStable<CTX> for NonZero<u32> {
342343
#[inline]
343344
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
344345
self.get().hash_stable(ctx, hasher)
345346
}
346347
}
347348

348-
impl<CTX> HashStable<CTX> for ::std::num::NonZeroUsize {
349+
impl<CTX> HashStable<CTX> for NonZero<usize> {
349350
#[inline]
350351
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
351352
self.get().hash_stable(ctx, hasher)

compiler/rustc_data_structures/src/sync/worker_local.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use parking_lot::Mutex;
22
use std::cell::Cell;
33
use std::cell::OnceCell;
4-
use std::num::NonZeroUsize;
4+
use std::num::NonZero;
55
use std::ops::Deref;
66
use std::ptr;
77
use std::sync::Arc;
@@ -31,7 +31,7 @@ impl RegistryId {
3131
}
3232

3333
struct RegistryData {
34-
thread_limit: NonZeroUsize,
34+
thread_limit: NonZero<usize>,
3535
threads: Mutex<usize>,
3636
}
3737

@@ -61,7 +61,7 @@ thread_local! {
6161

6262
impl Registry {
6363
/// Creates a registry which can hold up to `thread_limit` threads.
64-
pub fn new(thread_limit: NonZeroUsize) -> Self {
64+
pub fn new(thread_limit: NonZero<usize>) -> Self {
6565
Registry(Arc::new(RegistryData { thread_limit, threads: Mutex::new(0) }))
6666
}
6767

compiler/rustc_data_structures/src/tagged_ptr/copy.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::fmt;
44
use std::hash::{Hash, Hasher};
55
use std::marker::PhantomData;
66
use std::mem::ManuallyDrop;
7-
use std::num::NonZeroUsize;
7+
use std::num::NonZero;
88
use std::ops::{Deref, DerefMut};
99
use std::ptr::NonNull;
1010

@@ -134,7 +134,7 @@ where
134134

135135
ptr.map_addr(|addr| {
136136
// Safety:
137-
// - The pointer is `NonNull` => it's address is `NonZeroUsize`
137+
// - The pointer is `NonNull` => it's address is `NonZero<usize>`
138138
// - `P::BITS` least significant bits are always zero (`Pointer` contract)
139139
// - `T::BITS <= P::BITS` (from `Self::ASSERTION`)
140140
//
@@ -143,14 +143,14 @@ where
143143
// `{non_zero} | packed_tag` can't make the value zero.
144144

145145
let packed = (addr.get() >> T::BITS) | packed_tag;
146-
unsafe { NonZeroUsize::new_unchecked(packed) }
146+
unsafe { NonZero::new_unchecked(packed) }
147147
})
148148
}
149149

150150
/// Retrieves the original raw pointer from `self.packed`.
151151
#[inline]
152152
pub(super) fn pointer_raw(&self) -> NonNull<P::Target> {
153-
self.packed.map_addr(|addr| unsafe { NonZeroUsize::new_unchecked(addr.get() << T::BITS) })
153+
self.packed.map_addr(|addr| unsafe { NonZero::new_unchecked(addr.get() << T::BITS) })
154154
}
155155

156156
/// This provides a reference to the `P` pointer itself, rather than the

compiler/rustc_errors/src/diagnostic_impls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ into_diagnostic_arg_using_display!(
7979
ast::ParamKindOrd,
8080
std::io::Error,
8181
Box<dyn std::error::Error>,
82-
std::num::NonZeroU32,
82+
std::num::NonZero<u32>,
8383
hir::Target,
8484
Edition,
8585
Ident,

0 commit comments

Comments
 (0)