Skip to content

Commit

Permalink
Use generic NonZero internally.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Jan 30, 2024
1 parent 5518eaa commit 155c7fc
Show file tree
Hide file tree
Showing 148 changed files with 657 additions and 608 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_abi/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tracing::debug;

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

Expand Down Expand Up @@ -324,7 +324,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::<usize>::new(only_variant.len())?),
abi,
largest_niche: None,
align,
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_abi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#![cfg_attr(feature = "nightly", feature(generic_nonzero))]
#![cfg_attr(feature = "nightly", feature(step_trait))]
#![cfg_attr(feature = "nightly", allow(internal_features))]
#![cfg_attr(feature = "nightly", doc(rust_logo))]
#![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 @@ -1124,7 +1125,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
6 changes: 3 additions & 3 deletions compiler/rustc_attr/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustc_session::parse::feature_err;
use rustc_session::{RustcVersion, Session};
use rustc_span::hygiene::Transparency;
use rustc_span::{symbol::sym, symbol::Symbol, Span};
use std::num::NonZeroU32;
use std::num::NonZero;

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

Expand Down Expand Up @@ -113,7 +113,7 @@ pub enum StabilityLevel {
/// Reason for the current stability level.
reason: UnstableReason,
/// Relevant `rust-lang/rust` issue.
issue: Option<NonZeroU32>,
issue: Option<NonZero<u32>>,
is_soft: bool,
/// If part of a feature is stabilized and a new feature is added for the remaining parts,
/// then the `implied_by` attribute is used to indicate which now-stable feature previously
Expand Down Expand Up @@ -442,7 +442,7 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
// is a name/value pair string literal.
issue_num = match issue.unwrap().as_str() {
"none" => None,
issue => match issue.parse::<NonZeroU32>() {
issue => match issue.parse::<NonZero<u32>>() {
Ok(num) => Some(num),
Err(err) => {
sess.dcx().emit_err(
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_attr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#![allow(internal_features)]
#![feature(rustdoc_internals)]
#![doc(rust_logo)]
#![feature(generic_nonzero)]
#![feature(let_chains)]
#![deny(rustc::untranslatable_diagnostic)]
#![deny(rustc::diagnostic_outside_of_impl)]
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_const_eval/src/interpret/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! to be const-safe.

use std::fmt::Write;
use std::num::NonZeroUsize;
use std::num::NonZero;

use either::{Left, Right};

Expand Down Expand Up @@ -780,7 +780,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
fn visit_union(
&mut self,
op: &OpTy<'tcx, M::Provenance>,
_fields: NonZeroUsize,
_fields: NonZero<usize>,
) -> InterpResult<'tcx> {
// Special check for CTFE validation, preventing `UnsafeCell` inside unions in immutable memory.
if self.ctfe_mode.is_some_and(|c| !c.allow_immutable_unsafe_cell()) {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_const_eval/src/interpret/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustc_middle::ty;
use rustc_target::abi::FieldIdx;
use rustc_target::abi::{FieldsShape, VariantIdx, Variants};

use std::num::NonZeroUsize;
use std::num::NonZero;

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

Expand Down Expand Up @@ -43,7 +43,7 @@ pub trait ValueVisitor<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>>: Sized {
}
/// Visits the given value as a union. No automatic recursion can happen here.
#[inline(always)]
fn visit_union(&mut self, _v: &Self::V, _fields: NonZeroUsize) -> InterpResult<'tcx> {
fn visit_union(&mut self, _v: &Self::V, _fields: NonZero<usize>) -> InterpResult<'tcx> {
Ok(())
}
/// Visits the given value as the pointer of a `Box`. There is nothing to recurse into.
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_const_eval/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Rust MIR: a lowered representation of Rust.
#![feature(assert_matches)]
#![feature(box_patterns)]
#![feature(decl_macro)]
#![feature(generic_nonzero)]
#![feature(let_chains)]
#![feature(slice_ptr_get)]
#![feature(never_type)]
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_data_structures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#![feature(cfg_match)]
#![feature(core_intrinsics)]
#![feature(extend_one)]
#![feature(generic_nonzero)]
#![feature(hash_raw_entry)]
#![feature(hasher_prefixfree_extras)]
#![feature(lazy_cell)]
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_data_structures/src/stable_hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::fmt;
use std::hash::{BuildHasher, Hash, Hasher};
use std::marker::PhantomData;
use std::mem;
use std::num::NonZero;

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

impl<CTX> HashStable<CTX> for ::std::num::NonZeroU32 {
impl<CTX> HashStable<CTX> for NonZero<u32> {
#[inline]
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
self.get().hash_stable(ctx, hasher)
}
}

impl<CTX> HashStable<CTX> for ::std::num::NonZeroUsize {
impl<CTX> HashStable<CTX> for NonZero<usize> {
#[inline]
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
self.get().hash_stable(ctx, hasher)
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_data_structures/src/sync/worker_local.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use parking_lot::Mutex;
use std::cell::Cell;
use std::cell::OnceCell;
use std::num::NonZeroUsize;
use std::num::NonZero;
use std::ops::Deref;
use std::ptr;
use std::sync::Arc;
Expand Down Expand Up @@ -31,7 +31,7 @@ impl RegistryId {
}

struct RegistryData {
thread_limit: NonZeroUsize,
thread_limit: NonZero<usize>,
threads: Mutex<usize>,
}

Expand Down Expand Up @@ -61,7 +61,7 @@ thread_local! {

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

Expand Down
9 changes: 5 additions & 4 deletions compiler/rustc_data_structures/src/tagged_ptr/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::fmt;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::mem::ManuallyDrop;
use std::num::NonZeroUsize;
use std::num::NonZero;
use std::ops::{Deref, DerefMut};
use std::ptr::NonNull;

Expand Down Expand Up @@ -134,7 +134,7 @@ where

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

let packed = (addr.get() >> T::BITS) | packed_tag;
unsafe { NonZeroUsize::new_unchecked(packed) }
unsafe { NonZero::<usize>::new_unchecked(packed) }
})
}

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

/// This provides a reference to the `P` pointer itself, rather than the
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_errors/src/diagnostic_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ into_diagnostic_arg_using_display!(
ast::ParamKindOrd,
std::io::Error,
Box<dyn std::error::Error>,
std::num::NonZeroU32,
std::num::NonZero<u32>,
hir::Target,
Edition,
Ident,
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#![feature(box_patterns)]
#![feature(error_reporter)]
#![feature(extract_if)]
#![feature(generic_nonzero)]
#![feature(let_chains)]
#![feature(min_specialization)]
#![feature(negative_impls)]
Expand Down Expand Up @@ -73,7 +74,7 @@ use std::error::Report;
use std::fmt;
use std::hash::Hash;
use std::io::Write;
use std::num::NonZeroUsize;
use std::num::NonZero;
use std::panic;
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -536,7 +537,7 @@ pub struct DiagCtxtFlags {
pub can_emit_warnings: bool,
/// If Some, the Nth error-level diagnostic is upgraded to bug-level.
/// (rustc: see `-Z treat-err-as-bug`)
pub treat_err_as_bug: Option<NonZeroUsize>,
pub treat_err_as_bug: Option<NonZero<usize>>,
/// Eagerly emit delayed bugs as errors, so that the compiler debugger may
/// see all of the errors being emitted at once.
pub eagerly_emit_delayed_bugs: bool,
Expand Down
17 changes: 9 additions & 8 deletions compiler/rustc_feature/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//! symbol to the `accepted` or `removed` modules respectively.

#![allow(internal_features)]
#![feature(generic_nonzero)]
#![feature(rustdoc_internals)]
#![doc(rust_logo)]
#![feature(lazy_cell)]
Expand All @@ -27,13 +28,13 @@ mod unstable;
mod tests;

use rustc_span::symbol::Symbol;
use std::num::NonZeroU32;
use std::num::NonZero;

#[derive(Debug, Clone)]
pub struct Feature {
pub name: Symbol,
pub since: &'static str,
issue: Option<NonZeroU32>,
issue: Option<NonZero<u32>>,
}

#[derive(Copy, Clone, Debug)]
Expand Down Expand Up @@ -87,7 +88,7 @@ impl UnstableFeatures {
}
}

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

const fn to_nonzero(n: Option<u32>) -> Option<NonZeroU32> {
// Can be replaced with `n.and_then(NonZeroU32::new)` if that is ever usable
const fn to_nonzero(n: Option<u32>) -> Option<NonZero<u32>> {
// Can be replaced with `n.and_then(NonZero::new)` if that is ever usable
// in const context. Requires https://github.com/rust-lang/rfcs/pull/2632.
match n {
None => None,
Some(n) => NonZeroU32::new(n),
Some(n) => NonZero::<u32>::new(n),
}
}

pub enum GateIssue {
Language,
Library(Option<NonZeroU32>),
Library(Option<NonZero<u32>>),
}

pub fn find_feature_issue(feature: Symbol, issue: GateIssue) -> Option<NonZeroU32> {
pub fn find_feature_issue(feature: Symbol, issue: GateIssue) -> Option<NonZero<u32>> {
match issue {
GateIssue::Language => find_lang_feature_issue(feature),
GateIssue::Library(lib) => lib,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_analysis/src/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub mod wfcheck;

pub use check::check_abi;

use std::num::NonZeroU32;
use std::num::NonZero;

use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::ErrorGuaranteed;
Expand Down Expand Up @@ -270,7 +270,7 @@ fn default_body_is_unstable(
item_did: DefId,
feature: Symbol,
reason: Option<Symbol>,
issue: Option<NonZeroU32>,
issue: Option<NonZero<u32>>,
) {
let missing_item_name = tcx.associated_item(item_did).name;
let (mut some_note, mut none_note, mut reason_str) = (false, false, String::new());
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ This API is completely unstable and subject to change.
#![feature(rustdoc_internals)]
#![allow(internal_features)]
#![feature(control_flow_enum)]
#![feature(generic_nonzero)]
#![feature(if_let_guard)]
#![feature(is_sorted)]
#![feature(iter_intersperse)]
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_interface/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![feature(decl_macro)]
#![feature(error_iter)]
#![feature(generic_nonzero)]
#![feature(lazy_cell)]
#![feature(let_chains)]
#![feature(thread_spawn_unchecked)]
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use rustc_span::{FileName, SourceFileHashAlgorithm};
use rustc_target::spec::{CodeModel, LinkerFlavorCli, MergeFunctions, PanicStrategy, RelocModel};
use rustc_target::spec::{RelroLevel, SanitizerSet, SplitDebuginfo, StackProtector, TlsModel};
use std::collections::{BTreeMap, BTreeSet};
use std::num::NonZeroUsize;
use std::num::NonZero;
use std::path::{Path, PathBuf};
use std::sync::Arc;

Expand Down Expand Up @@ -826,7 +826,7 @@ fn test_unstable_options_tracking_hash() {
tracked!(tls_model, Some(TlsModel::GeneralDynamic));
tracked!(translate_remapped_path_to_local_path, false);
tracked!(trap_unreachable, Some(false));
tracked!(treat_err_as_bug, NonZeroUsize::new(1));
tracked!(treat_err_as_bug, NonZero::<usize>::new(1));
tracked!(tune_cpu, Some(String::from("abc")));
tracked!(uninit_const_chunk_threshold, 123);
tracked!(unleash_the_miri_inside_of_you, true);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_interface/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
use rustc_query_impl::QueryCtxt;
use rustc_query_system::query::{deadlock, QueryContext};

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

if !sync::is_dyn_thread_safe() {
return run_in_thread_with_globals(edition, || {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#![feature(array_windows)]
#![feature(box_patterns)]
#![feature(control_flow_enum)]
#![feature(generic_nonzero)]
#![feature(if_let_guard)]
#![feature(iter_order_by)]
#![feature(let_chains)]
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(rustc::untranslatable_diagnostic)]
#![allow(rustc::diagnostic_outside_of_impl)]
use std::num::NonZeroU32;
use std::num::NonZero;

use crate::errors::RequestedLevel;
use crate::fluent_generated as fluent;
Expand Down Expand Up @@ -407,7 +407,7 @@ pub struct BuiltinIncompleteFeaturesHelp;
#[derive(Subdiagnostic)]
#[note(lint_note)]
pub struct BuiltinFeatureIssueNote {
pub n: NonZeroU32,
pub n: NonZero<u32>,
}

pub struct BuiltinUnpermittedTypeInit<'a> {
Expand Down
Loading

0 comments on commit 155c7fc

Please sign in to comment.