Skip to content

Commit 739bec2

Browse files
committed
Use generic NonZero internally.
1 parent 5518eaa commit 739bec2

File tree

150 files changed

+654
-605
lines changed

Some content is hidden

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

150 files changed

+654
-605
lines changed

compiler/rustc_abi/src/layout.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use tracing::debug;
77

88
use crate::{
99
Abi, AbiAndPrefAlign, Align, FieldsShape, IndexSlice, IndexVec, Integer, LayoutS, Niche,
10-
NonZeroUsize, Primitive, ReprOptions, Scalar, Size, StructKind, TagEncoding, TargetDataLayout,
10+
NonZero, Primitive, ReprOptions, Scalar, Size, StructKind, TagEncoding, TargetDataLayout,
1111
Variants, WrappingRange,
1212
};
1313

@@ -324,7 +324,7 @@ pub trait LayoutCalculator {
324324

325325
Some(LayoutS {
326326
variants: Variants::Single { index: VariantIdx::new(0) },
327-
fields: FieldsShape::Union(NonZeroUsize::new(only_variant.len())?),
327+
fields: FieldsShape::Union(NonZero::<usize>::new(only_variant.len())?),
328328
abi,
329329
largest_niche: None,
330330
align,

compiler/rustc_abi/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
#![cfg_attr(feature = "nightly", feature(generic_nonzero))]
12
#![cfg_attr(feature = "nightly", feature(step_trait))]
23
#![cfg_attr(feature = "nightly", allow(internal_features))]
34
#![cfg_attr(feature = "nightly", doc(rust_logo))]
45
#![cfg_attr(feature = "nightly", feature(rustdoc_internals))]
56

67
use std::fmt;
7-
use std::num::{NonZeroUsize, ParseIntError};
8+
use std::num::{NonZero, ParseIntError};
89
use std::ops::{Add, AddAssign, Mul, RangeInclusive, Sub};
910
use std::str::FromStr;
1011

@@ -1124,7 +1125,7 @@ pub enum FieldsShape<FieldIdx: Idx> {
11241125
Primitive,
11251126

11261127
/// All fields start at no offset. The `usize` is the field count.
1127-
Union(NonZeroUsize),
1128+
Union(NonZero<usize>),
11281129

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

compiler/rustc_attr/src/builtin.rs

+3-3
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

+1
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
#![deny(rustc::untranslatable_diagnostic)]
1213
#![deny(rustc::diagnostic_outside_of_impl)]

compiler/rustc_const_eval/src/interpret/validity.rs

+2-2
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

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

compiler/rustc_const_eval/src/interpret/visitor.rs

+2-2
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

+1
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

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#![feature(cfg_match)]
2323
#![feature(core_intrinsics)]
2424
#![feature(extend_one)]
25+
#![feature(generic_nonzero)]
2526
#![feature(hash_raw_entry)]
2627
#![feature(hasher_prefixfree_extras)]
2728
#![feature(lazy_cell)]

compiler/rustc_data_structures/src/stable_hasher.rs

+3-2
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

+3-3
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

+5-4
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,15 @@ 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::<usize>::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
154+
.map_addr(|addr| unsafe { NonZero::<usize>::new_unchecked(addr.get() << T::BITS) })
154155
}
155156

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

compiler/rustc_errors/src/diagnostic_impls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ into_diagnostic_arg_using_display!(
8383
ast::ParamKindOrd,
8484
std::io::Error,
8585
Box<dyn std::error::Error>,
86-
std::num::NonZeroU32,
86+
std::num::NonZero<u32>,
8787
hir::Target,
8888
Edition,
8989
Ident,

compiler/rustc_errors/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#![feature(box_patterns)]
1414
#![feature(error_reporter)]
1515
#![feature(extract_if)]
16+
#![feature(generic_nonzero)]
1617
#![feature(let_chains)]
1718
#![feature(min_specialization)]
1819
#![feature(negative_impls)]
@@ -73,7 +74,7 @@ use std::error::Report;
7374
use std::fmt;
7475
use std::hash::Hash;
7576
use std::io::Write;
76-
use std::num::NonZeroUsize;
77+
use std::num::NonZero;
7778
use std::panic;
7879
use std::path::{Path, PathBuf};
7980

@@ -536,7 +537,7 @@ pub struct DiagCtxtFlags {
536537
pub can_emit_warnings: bool,
537538
/// If Some, the Nth error-level diagnostic is upgraded to bug-level.
538539
/// (rustc: see `-Z treat-err-as-bug`)
539-
pub treat_err_as_bug: Option<NonZeroUsize>,
540+
pub treat_err_as_bug: Option<NonZero<usize>>,
540541
/// Eagerly emit delayed bugs as errors, so that the compiler debugger may
541542
/// see all of the errors being emitted at once.
542543
pub eagerly_emit_delayed_bugs: bool,

compiler/rustc_feature/src/lib.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//! symbol to the `accepted` or `removed` modules respectively.
1313
1414
#![allow(internal_features)]
15+
#![feature(generic_nonzero)]
1516
#![feature(rustdoc_internals)]
1617
#![doc(rust_logo)]
1718
#![feature(lazy_cell)]
@@ -27,13 +28,13 @@ mod unstable;
2728
mod tests;
2829

2930
use rustc_span::symbol::Symbol;
30-
use std::num::NonZeroU32;
31+
use std::num::NonZero;
3132

3233
#[derive(Debug, Clone)]
3334
pub struct Feature {
3435
pub name: Symbol,
3536
pub since: &'static str,
36-
issue: Option<NonZeroU32>,
37+
issue: Option<NonZero<u32>>,
3738
}
3839

3940
#[derive(Copy, Clone, Debug)]
@@ -87,7 +88,7 @@ impl UnstableFeatures {
8788
}
8889
}
8990

90-
fn find_lang_feature_issue(feature: Symbol) -> Option<NonZeroU32> {
91+
fn find_lang_feature_issue(feature: Symbol) -> Option<NonZero<u32>> {
9192
// Search in all the feature lists.
9293
if let Some(f) = UNSTABLE_FEATURES.iter().find(|f| f.feature.name == feature) {
9394
return f.feature.issue;
@@ -101,21 +102,21 @@ fn find_lang_feature_issue(feature: Symbol) -> Option<NonZeroU32> {
101102
panic!("feature `{feature}` is not declared anywhere");
102103
}
103104

104-
const fn to_nonzero(n: Option<u32>) -> Option<NonZeroU32> {
105-
// Can be replaced with `n.and_then(NonZeroU32::new)` if that is ever usable
105+
const fn to_nonzero(n: Option<u32>) -> Option<NonZero<u32>> {
106+
// Can be replaced with `n.and_then(NonZero::new)` if that is ever usable
106107
// in const context. Requires https://github.com/rust-lang/rfcs/pull/2632.
107108
match n {
108109
None => None,
109-
Some(n) => NonZeroU32::new(n),
110+
Some(n) => NonZero::<u32>::new(n),
110111
}
111112
}
112113

113114
pub enum GateIssue {
114115
Language,
115-
Library(Option<NonZeroU32>),
116+
Library(Option<NonZero<u32>>),
116117
}
117118

118-
pub fn find_feature_issue(feature: Symbol, issue: GateIssue) -> Option<NonZeroU32> {
119+
pub fn find_feature_issue(feature: Symbol, issue: GateIssue) -> Option<NonZero<u32>> {
119120
match issue {
120121
GateIssue::Language => find_lang_feature_issue(feature),
121122
GateIssue::Library(lib) => lib,

compiler/rustc_hir_analysis/src/check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub mod wfcheck;
7474

7575
pub use check::check_abi;
7676

77-
use std::num::NonZeroU32;
77+
use std::num::NonZero;
7878

7979
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
8080
use rustc_errors::ErrorGuaranteed;
@@ -270,7 +270,7 @@ fn default_body_is_unstable(
270270
item_did: DefId,
271271
feature: Symbol,
272272
reason: Option<Symbol>,
273-
issue: Option<NonZeroU32>,
273+
issue: Option<NonZero<u32>>,
274274
) {
275275
let missing_item_name = tcx.associated_item(item_did).name;
276276
let (mut some_note, mut none_note, mut reason_str) = (false, false, String::new());

compiler/rustc_hir_analysis/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ This API is completely unstable and subject to change.
6161
#![feature(rustdoc_internals)]
6262
#![allow(internal_features)]
6363
#![feature(control_flow_enum)]
64+
#![feature(generic_nonzero)]
6465
#![feature(if_let_guard)]
6566
#![feature(is_sorted)]
6667
#![feature(iter_intersperse)]

compiler/rustc_interface/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![feature(decl_macro)]
22
#![feature(error_iter)]
3+
#![feature(generic_nonzero)]
34
#![feature(lazy_cell)]
45
#![feature(let_chains)]
56
#![feature(thread_spawn_unchecked)]

compiler/rustc_interface/src/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_span::{FileName, SourceFileHashAlgorithm};
2020
use rustc_target::spec::{CodeModel, LinkerFlavorCli, MergeFunctions, PanicStrategy, RelocModel};
2121
use rustc_target::spec::{RelroLevel, SanitizerSet, SplitDebuginfo, StackProtector, TlsModel};
2222
use std::collections::{BTreeMap, BTreeSet};
23-
use std::num::NonZeroUsize;
23+
use std::num::NonZero;
2424
use std::path::{Path, PathBuf};
2525
use std::sync::Arc;
2626

@@ -826,7 +826,7 @@ fn test_unstable_options_tracking_hash() {
826826
tracked!(tls_model, Some(TlsModel::GeneralDynamic));
827827
tracked!(translate_remapped_path_to_local_path, false);
828828
tracked!(trap_unreachable, Some(false));
829-
tracked!(treat_err_as_bug, NonZeroUsize::new(1));
829+
tracked!(treat_err_as_bug, NonZero::<usize>::new(1));
830830
tracked!(tune_cpu, Some(String::from("abc")));
831831
tracked!(uninit_const_chunk_threshold, 123);
832832
tracked!(unleash_the_miri_inside_of_you, true);

compiler/rustc_interface/src/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
107107
use rustc_query_impl::QueryCtxt;
108108
use rustc_query_system::query::{deadlock, QueryContext};
109109

110-
let registry = sync::Registry::new(std::num::NonZeroUsize::new(threads).unwrap());
110+
let registry = sync::Registry::new(std::num::NonZero::<usize>::new(threads).unwrap());
111111

112112
if !sync::is_dyn_thread_safe() {
113113
return run_in_thread_with_globals(edition, || {

compiler/rustc_lint/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#![feature(array_windows)]
3232
#![feature(box_patterns)]
3333
#![feature(control_flow_enum)]
34+
#![feature(generic_nonzero)]
3435
#![feature(if_let_guard)]
3536
#![feature(iter_order_by)]
3637
#![feature(let_chains)]

compiler/rustc_lint/src/lints.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(rustc::untranslatable_diagnostic)]
22
#![allow(rustc::diagnostic_outside_of_impl)]
3-
use std::num::NonZeroU32;
3+
use std::num::NonZero;
44

55
use crate::errors::RequestedLevel;
66
use crate::fluent_generated as fluent;
@@ -407,7 +407,7 @@ pub struct BuiltinIncompleteFeaturesHelp;
407407
#[derive(Subdiagnostic)]
408408
#[note(lint_note)]
409409
pub struct BuiltinFeatureIssueNote {
410-
pub n: NonZeroU32,
410+
pub n: NonZero<u32>,
411411
}
412412

413413
pub struct BuiltinUnpermittedTypeInit<'a> {

0 commit comments

Comments
 (0)