1
Fork 0

Use generic NonZero internally.

This commit is contained in:
Markus Reiter 2024-01-29 23:59:09 +01:00
parent ee9c7c940c
commit 746a58d435
No known key found for this signature in database
GPG key ID: 245293B51702655B
144 changed files with 653 additions and 604 deletions

View file

@ -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};
@ -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
@ -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(

View file

@ -7,6 +7,7 @@
#![allow(internal_features)]
#![feature(rustdoc_internals)]
#![doc(rust_logo)]
#![feature(generic_nonzero)]
#![feature(let_chains)]
#[macro_use]

View file

@ -5,7 +5,7 @@
//! to be const-safe.
use std::fmt::Write;
use std::num::NonZeroUsize;
use std::num::NonZero;
use either::{Left, Right};
@ -782,7 +782,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()) {

View file

@ -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};
@ -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.

View file

@ -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)]

View file

@ -20,6 +20,7 @@
#![feature(cfg_match)]
#![feature(core_intrinsics)]
#![feature(extend_one)]
#![feature(generic_nonzero)]
#![feature(hash_raw_entry)]
#![feature(hasher_prefixfree_extras)]
#![feature(lazy_cell)]

View file

@ -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;
@ -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)

View file

@ -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;
@ -31,7 +31,7 @@ impl RegistryId {
}
struct RegistryData {
thread_limit: NonZeroUsize,
thread_limit: NonZero<usize>,
threads: Mutex<usize>,
}
@ -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) }))
}

View file

@ -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;
@ -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`)
//
@ -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

View file

@ -79,7 +79,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,

View file

@ -16,6 +16,7 @@
#![feature(box_patterns)]
#![feature(error_reporter)]
#![feature(extract_if)]
#![feature(generic_nonzero)]
#![feature(let_chains)]
#![feature(negative_impls)]
#![feature(never_type)]
@ -77,7 +78,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::ops::DerefMut;
use std::panic;
use std::path::{Path, PathBuf};
@ -545,7 +546,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,

View file

@ -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)]
@ -25,13 +26,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)]
@ -85,7 +86,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;
@ -99,21 +100,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,

View file

@ -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;
@ -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());

View file

@ -63,6 +63,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)]

View file

@ -1,5 +1,6 @@
#![feature(decl_macro)]
#![feature(error_iter)]
#![feature(generic_nonzero)]
#![feature(lazy_cell)]
#![feature(let_chains)]
#![feature(thread_spawn_unchecked)]

View file

@ -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;
@ -827,7 +827,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);

View file

@ -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, || {

View file

@ -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)]

View file

@ -1,7 +1,6 @@
#![allow(rustc::diagnostic_outside_of_impl)]
#![allow(rustc::untranslatable_diagnostic)]
use std::num::NonZeroU32;
use std::num::NonZero;
use crate::errors::RequestedLevel;
use crate::fluent_generated as fluent;
@ -402,7 +401,7 @@ pub struct BuiltinIncompleteFeaturesHelp;
#[derive(Subdiagnostic)]
#[note(lint_note)]
pub struct BuiltinFeatureIssueNote {
pub n: NonZeroU32,
pub n: NonZero<u32>,
}
pub struct BuiltinUnpermittedTypeInit<'a> {

View file

@ -5,6 +5,7 @@
#![feature(decl_macro)]
#![feature(extract_if)]
#![feature(coroutines)]
#![feature(generic_nonzero)]
#![feature(iter_from_coroutine)]
#![feature(let_chains)]
#![feature(if_let_guard)]

View file

@ -327,7 +327,7 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
}
#[inline]
fn read_lazy_offset_then<T>(&mut self, f: impl Fn(NonZeroUsize) -> T) -> T {
fn read_lazy_offset_then<T>(&mut self, f: impl Fn(NonZero<usize>) -> T) -> T {
let distance = self.read_usize();
let position = match self.lazy_state {
LazyState::NoNode => bug!("read_lazy_with_meta: outside of a metadata node"),
@ -338,7 +338,7 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
}
LazyState::Previous(last_pos) => last_pos.get() + distance,
};
let position = NonZeroUsize::new(position).unwrap();
let position = NonZero::<usize>::new(position).unwrap();
self.lazy_state = LazyState::Previous(position);
f(position)
}
@ -685,15 +685,17 @@ impl MetadataBlob {
}
pub(crate) fn get_rustc_version(&self) -> String {
LazyValue::<String>::from_position(NonZeroUsize::new(METADATA_HEADER.len() + 8).unwrap())
.decode(self)
LazyValue::<String>::from_position(
NonZero::<usize>::new(METADATA_HEADER.len() + 8).unwrap(),
)
.decode(self)
}
fn root_pos(&self) -> NonZeroUsize {
fn root_pos(&self) -> NonZero<usize> {
let offset = METADATA_HEADER.len();
let pos_bytes = self.blob()[offset..][..8].try_into().unwrap();
let pos = u64::from_le_bytes(pos_bytes);
NonZeroUsize::new(pos as usize).unwrap()
NonZero::<usize>::new(pos as usize).unwrap()
}
pub(crate) fn get_header(&self) -> CrateHeader {

View file

@ -421,7 +421,7 @@ macro_rules! record_defaulted_array {
}
impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
fn emit_lazy_distance(&mut self, position: NonZeroUsize) {
fn emit_lazy_distance(&mut self, position: NonZero<usize>) {
let pos = position.get();
let distance = match self.lazy_state {
LazyState::NoNode => bug!("emit_lazy_distance: outside of a metadata node"),
@ -439,7 +439,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
position.get() - last_pos.get()
}
};
self.lazy_state = LazyState::Previous(NonZeroUsize::new(pos).unwrap());
self.lazy_state = LazyState::Previous(NonZero::<usize>::new(pos).unwrap());
self.emit_usize(distance);
}
@ -447,7 +447,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
where
T::Value<'tcx>: Encodable<EncodeContext<'a, 'tcx>>,
{
let pos = NonZeroUsize::new(self.position()).unwrap();
let pos = NonZero::<usize>::new(self.position()).unwrap();
assert_eq!(self.lazy_state, LazyState::NoNode);
self.lazy_state = LazyState::NodeStart(pos);
@ -466,7 +466,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
where
T::Value<'tcx>: Encodable<EncodeContext<'a, 'tcx>>,
{
let pos = NonZeroUsize::new(self.position()).unwrap();
let pos = NonZero::<usize>::new(self.position()).unwrap();
assert_eq!(self.lazy_state, LazyState::NoNode);
self.lazy_state = LazyState::NodeStart(pos);

View file

@ -37,7 +37,7 @@ use rustc_target::abi::{FieldIdx, VariantIdx};
use rustc_target::spec::{PanicStrategy, TargetTriple};
use std::marker::PhantomData;
use std::num::NonZeroUsize;
use std::num::NonZero;
use decoder::DecodeContext;
pub(crate) use decoder::{CrateMetadata, CrateNumMap, MetadataBlob};
@ -83,7 +83,7 @@ pub const METADATA_HEADER: &[u8] = &[b'r', b'u', b's', b't', 0, 0, 0, METADATA_V
/// order than they were encoded in.
#[must_use]
struct LazyValue<T> {
position: NonZeroUsize,
position: NonZero<usize>,
_marker: PhantomData<fn() -> T>,
}
@ -92,7 +92,7 @@ impl<T: ParameterizedOverTcx> ParameterizedOverTcx for LazyValue<T> {
}
impl<T> LazyValue<T> {
fn from_position(position: NonZeroUsize) -> LazyValue<T> {
fn from_position(position: NonZero<usize>) -> LazyValue<T> {
LazyValue { position, _marker: PhantomData }
}
}
@ -108,7 +108,7 @@ impl<T> LazyValue<T> {
/// the minimal distance the length of the sequence, i.e.
/// it's assumed there's no 0-byte element in the sequence.
struct LazyArray<T> {
position: NonZeroUsize,
position: NonZero<usize>,
num_elems: usize,
_marker: PhantomData<fn() -> T>,
}
@ -119,12 +119,12 @@ impl<T: ParameterizedOverTcx> ParameterizedOverTcx for LazyArray<T> {
impl<T> Default for LazyArray<T> {
fn default() -> LazyArray<T> {
LazyArray::from_position_and_num_elems(NonZeroUsize::new(1).unwrap(), 0)
LazyArray::from_position_and_num_elems(NonZero::<usize>::new(1).unwrap(), 0)
}
}
impl<T> LazyArray<T> {
fn from_position_and_num_elems(position: NonZeroUsize, num_elems: usize) -> LazyArray<T> {
fn from_position_and_num_elems(position: NonZero<usize>, num_elems: usize) -> LazyArray<T> {
LazyArray { position, num_elems, _marker: PhantomData }
}
}
@ -135,7 +135,7 @@ impl<T> LazyArray<T> {
/// `LazyArray<T>`, but without requiring encoding or decoding all the values
/// eagerly and in-order.
struct LazyTable<I, T> {
position: NonZeroUsize,
position: NonZero<usize>,
/// The encoded size of the elements of a table is selected at runtime to drop
/// trailing zeroes. This is the number of bytes used for each table element.
width: usize,
@ -150,7 +150,7 @@ impl<I: 'static, T: ParameterizedOverTcx> ParameterizedOverTcx for LazyTable<I,
impl<I, T> LazyTable<I, T> {
fn from_position_and_encoded_size(
position: NonZeroUsize,
position: NonZero<usize>,
width: usize,
len: usize,
) -> LazyTable<I, T> {
@ -187,11 +187,11 @@ enum LazyState {
/// Inside a metadata node, and before any `Lazy`s.
/// The position is that of the node itself.
NodeStart(NonZeroUsize),
NodeStart(NonZero<usize>),
/// Inside a metadata node, with a previous `Lazy`s.
/// The position is where that previous `Lazy` would start.
Previous(NonZeroUsize),
Previous(NonZero<usize>),
}
type SyntaxContextTable = LazyTable<u32, Option<LazyValue<SyntaxContextData>>>;

View file

@ -339,7 +339,7 @@ impl<T> FixedSizeEncoding for Option<LazyValue<T>> {
#[inline]
fn from_bytes(b: &[u8; 8]) -> Self {
let position = NonZeroUsize::new(u64::from_bytes(b) as usize)?;
let position = NonZero::<usize>::new(u64::from_bytes(b) as usize)?;
Some(LazyValue::from_position(position))
}
@ -366,7 +366,7 @@ impl<T> LazyArray<T> {
}
fn from_bytes_impl(position: &[u8; 8], meta: &[u8; 8]) -> Option<LazyArray<T>> {
let position = NonZeroUsize::new(u64::from_bytes(position) as usize)?;
let position = NonZero::<usize>::new(u64::from_bytes(position) as usize)?;
let len = u64::from_bytes(meta) as usize;
Some(LazyArray::from_position_and_num_elems(position, len))
}
@ -497,7 +497,7 @@ impl<I: Idx, const N: usize, T: FixedSizeEncoding<ByteArray = [u8; N]>> TableBui
}
LazyTable::from_position_and_encoded_size(
NonZeroUsize::new(pos).unwrap(),
NonZero::<usize>::new(pos).unwrap(),
width,
self.blocks.len(),
)

View file

@ -34,6 +34,7 @@
#![feature(discriminant_kind)]
#![feature(exhaustive_patterns)]
#![feature(coroutines)]
#![feature(generic_nonzero)]
#![feature(if_let_guard)]
#![feature(inline_const)]
#![feature(iter_from_coroutine)]

View file

@ -21,7 +21,7 @@ use rustc_session::parse::feature_err_issue;
use rustc_session::Session;
use rustc_span::symbol::{sym, Symbol};
use rustc_span::Span;
use std::num::NonZeroU32;
use std::num::NonZero;
#[derive(PartialEq, Clone, Copy, Debug)]
pub enum StabilityLevel {
@ -102,7 +102,7 @@ pub fn report_unstable(
sess: &Session,
feature: Symbol,
reason: Option<Symbol>,
issue: Option<NonZeroU32>,
issue: Option<NonZero<u32>>,
suggestion: Option<(Span, String, String, Applicability)>,
is_soft: bool,
span: Span,
@ -235,7 +235,7 @@ pub enum EvalResult {
Deny {
feature: Symbol,
reason: Option<Symbol>,
issue: Option<NonZeroU32>,
issue: Option<NonZero<u32>>,
suggestion: Option<(Span, String, String, Applicability)>,
is_soft: bool,
},
@ -433,7 +433,7 @@ impl<'tcx> TyCtxt<'tcx> {
// the `-Z force-unstable-if-unmarked` flag present (we're
// compiling a compiler crate), then let this missing feature
// annotation slide.
if feature == sym::rustc_private && issue == NonZeroU32::new(27812) {
if feature == sym::rustc_private && issue == NonZero::<u32>::new(27812) {
if self.sess.opts.unstable_opts.force_unstable_if_unmarked {
return EvalResult::Allow;
}

View file

@ -122,7 +122,7 @@ mod value;
use std::fmt;
use std::io;
use std::io::{Read, Write};
use std::num::{NonZeroU32, NonZeroU64};
use std::num::NonZero;
use std::sync::atomic::{AtomicU32, Ordering};
use rustc_ast::LitKind;
@ -205,7 +205,7 @@ pub enum LitToConstError {
}
#[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct AllocId(pub NonZeroU64);
pub struct AllocId(pub NonZero<u64>);
// We want the `Debug` output to be readable as it is used by `derive(Debug)` for
// all the Miri types.
@ -260,7 +260,7 @@ pub fn specialized_encode_alloc_id<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>>(
}
// Used to avoid infinite recursion when decoding cyclic allocations.
type DecodingSessionId = NonZeroU32;
type DecodingSessionId = NonZero<u32>;
#[derive(Clone)]
enum State {
@ -500,7 +500,7 @@ impl<'tcx> AllocMap<'tcx> {
AllocMap {
alloc_map: Default::default(),
dedup: Default::default(),
next_id: AllocId(NonZeroU64::new(1).unwrap()),
next_id: AllocId(NonZero::<u64>::new(1).unwrap()),
}
}
fn reserve(&mut self) -> AllocId {

View file

@ -3,7 +3,7 @@ use super::{AllocId, InterpResult};
use rustc_macros::HashStable;
use rustc_target::abi::{HasDataLayout, Size};
use std::{fmt, num::NonZeroU64};
use std::{fmt, num::NonZero};
////////////////////////////////////////////////////////////////////////////////
// Pointer arithmetic
@ -129,7 +129,7 @@ pub trait Provenance: Copy + fmt::Debug + 'static {
/// The type of provenance in the compile-time interpreter.
/// This is a packed representation of an `AllocId` and an `immutable: bool`.
#[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct CtfeProvenance(NonZeroU64);
pub struct CtfeProvenance(NonZero<u64>);
impl From<AllocId> for CtfeProvenance {
fn from(value: AllocId) -> Self {
@ -155,7 +155,7 @@ impl CtfeProvenance {
/// Returns the `AllocId` of this provenance.
#[inline(always)]
pub fn alloc_id(self) -> AllocId {
AllocId(NonZeroU64::new(self.0.get() & !IMMUTABLE_MASK).unwrap())
AllocId(NonZero::<u64>::new(self.0.get() & !IMMUTABLE_MASK).unwrap())
}
/// Returns whether this provenance is immutable.

View file

@ -4,7 +4,7 @@ use rustc_errors::{DiagnosticArgValue, IntoDiagnosticArg};
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_target::abi::Size;
use std::fmt;
use std::num::NonZeroU8;
use std::num::NonZero;
use crate::ty::TyCtxt;
@ -132,7 +132,7 @@ pub struct ScalarInt {
/// The first `size` bytes of `data` are the value.
/// Do not try to read less or more bytes than that. The remaining bytes must be 0.
data: u128,
size: NonZeroU8,
size: NonZero<u8>,
}
// Cannot derive these, as the derives take references to the fields, and we
@ -161,14 +161,14 @@ impl<D: Decoder> Decodable<D> for ScalarInt {
let mut data = [0u8; 16];
let size = d.read_u8();
data[..size as usize].copy_from_slice(d.read_raw_bytes(size as usize));
ScalarInt { data: u128::from_le_bytes(data), size: NonZeroU8::new(size).unwrap() }
ScalarInt { data: u128::from_le_bytes(data), size: NonZero::<u8>::new(size).unwrap() }
}
}
impl ScalarInt {
pub const TRUE: ScalarInt = ScalarInt { data: 1_u128, size: NonZeroU8::new(1).unwrap() };
pub const TRUE: ScalarInt = ScalarInt { data: 1_u128, size: NonZero::<u8>::new(1).unwrap() };
pub const FALSE: ScalarInt = ScalarInt { data: 0_u128, size: NonZeroU8::new(1).unwrap() };
pub const FALSE: ScalarInt = ScalarInt { data: 0_u128, size: NonZero::<u8>::new(1).unwrap() };
#[inline]
pub fn size(self) -> Size {
@ -196,7 +196,7 @@ impl ScalarInt {
#[inline]
pub fn null(size: Size) -> Self {
Self { data: 0, size: NonZeroU8::new(size.bytes() as u8).unwrap() }
Self { data: 0, size: NonZero::<u8>::new(size.bytes() as u8).unwrap() }
}
#[inline]
@ -208,7 +208,7 @@ impl ScalarInt {
pub fn try_from_uint(i: impl Into<u128>, size: Size) -> Option<Self> {
let data = i.into();
if size.truncate(data) == data {
Some(Self { data, size: NonZeroU8::new(size.bytes() as u8).unwrap() })
Some(Self { data, size: NonZero::<u8>::new(size.bytes() as u8).unwrap() })
} else {
None
}
@ -220,7 +220,7 @@ impl ScalarInt {
// `into` performed sign extension, we have to truncate
let truncated = size.truncate(i as u128);
if size.sign_extend(truncated) as i128 == i {
Some(Self { data: truncated, size: NonZeroU8::new(size.bytes() as u8).unwrap() })
Some(Self { data: truncated, size: NonZero::<u8>::new(size.bytes() as u8).unwrap() })
} else {
None
}
@ -388,7 +388,7 @@ macro_rules! from {
fn from(u: $ty) -> Self {
Self {
data: u128::from(u),
size: NonZeroU8::new(std::mem::size_of::<$ty>() as u8).unwrap(),
size: NonZero::<u8>::new(std::mem::size_of::<$ty>() as u8).unwrap(),
}
}
}
@ -427,7 +427,10 @@ impl TryFrom<ScalarInt> for bool {
impl From<char> for ScalarInt {
#[inline]
fn from(c: char) -> Self {
Self { data: c as u128, size: NonZeroU8::new(std::mem::size_of::<char>() as u8).unwrap() }
Self {
data: c as u128,
size: NonZero::<u8>::new(std::mem::size_of::<char>() as u8).unwrap(),
}
}
}
@ -454,7 +457,7 @@ impl From<Single> for ScalarInt {
#[inline]
fn from(f: Single) -> Self {
// We trust apfloat to give us properly truncated data.
Self { data: f.to_bits(), size: NonZeroU8::new((Single::BITS / 8) as u8).unwrap() }
Self { data: f.to_bits(), size: NonZero::<u8>::new((Single::BITS / 8) as u8).unwrap() }
}
}
@ -470,7 +473,7 @@ impl From<Double> for ScalarInt {
#[inline]
fn from(f: Double) -> Self {
// We trust apfloat to give us properly truncated data.
Self { data: f.to_bits(), size: NonZeroU8::new((Double::BITS / 8) as u8).unwrap() }
Self { data: f.to_bits(), size: NonZero::<u8>::new((Double::BITS / 8) as u8).unwrap() }
}
}

View file

@ -18,7 +18,7 @@ use core::intrinsics;
use std::cmp::Ordering;
use std::marker::PhantomData;
use std::mem;
use std::num::NonZeroUsize;
use std::num::NonZero;
use std::ops::{ControlFlow, Deref};
use std::ptr::NonNull;
@ -144,7 +144,7 @@ impl<'tcx> GenericArg<'tcx> {
#[inline]
pub fn unpack(self) -> GenericArgKind<'tcx> {
let ptr = unsafe {
self.ptr.map_addr(|addr| NonZeroUsize::new_unchecked(addr.get() & !TAG_MASK))
self.ptr.map_addr(|addr| NonZero::<usize>::new_unchecked(addr.get() & !TAG_MASK))
};
// SAFETY: use of `Interned::new_unchecked` here is ok because these
// pointers were originally created from `Interned` types in `pack()`,

View file

@ -20,7 +20,7 @@ use rustc_target::spec::{abi::Abi as SpecAbi, HasTargetSpec, PanicStrategy, Targ
use std::cmp;
use std::fmt;
use std::num::NonZeroUsize;
use std::num::NonZero;
use std::ops::Bound;
pub trait IntegerExt {
@ -761,7 +761,7 @@ where
};
tcx.mk_layout(LayoutS {
variants: Variants::Single { index: variant_index },
fields: match NonZeroUsize::new(fields) {
fields: match NonZero::<usize>::new(fields) {
Some(fields) => FieldsShape::Union(fields),
None => FieldsShape::Arbitrary { offsets: IndexVec::new(), memory_index: IndexVec::new() },
},

View file

@ -61,7 +61,7 @@ use std::fmt::Debug;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::mem;
use std::num::NonZeroUsize;
use std::num::NonZero;
use std::ops::ControlFlow;
use std::ptr::NonNull;
use std::{fmt, str};
@ -618,7 +618,7 @@ impl<'tcx> Term<'tcx> {
#[inline]
pub fn unpack(self) -> TermKind<'tcx> {
let ptr = unsafe {
self.ptr.map_addr(|addr| NonZeroUsize::new_unchecked(addr.get() & !TAG_MASK))
self.ptr.map_addr(|addr| NonZero::<usize>::new_unchecked(addr.get() & !TAG_MASK))
};
// SAFETY: use of `Interned::new_unchecked` here is ok because these
// pointers were originally created from `Interned` types in `pack()`,

View file

@ -8,6 +8,7 @@
#![doc(rust_logo)]
#![feature(rustdoc_internals)]
#![allow(internal_features)]
#![feature(generic_nonzero)]
#![feature(let_chains)]
#![feature(map_try_insert)]
#![feature(try_blocks)]

View file

@ -27,7 +27,7 @@ use rustc_span::Span;
use rustc_target::spec::abi::Abi;
use std::mem::replace;
use std::num::NonZeroU32;
use std::num::NonZero;
#[derive(PartialEq)]
enum AnnotationKind {
@ -645,7 +645,7 @@ fn stability_index(tcx: TyCtxt<'_>, (): ()) -> Index {
let stability = Stability {
level: attr::StabilityLevel::Unstable {
reason: UnstableReason::Default,
issue: NonZeroU32::new(27812),
issue: NonZero::<u32>::new(27812),
is_soft: false,
implied_by: None,
},

View file

@ -3,6 +3,7 @@
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
#![doc(rust_logo)]
#![feature(rustdoc_internals)]
#![feature(generic_nonzero)]
#![feature(min_specialization)]
#![feature(rustc_attrs)]
#![allow(rustc::potential_query_instability, unused_parens)]

View file

@ -30,7 +30,7 @@ use rustc_serialize::Decodable;
use rustc_serialize::Encodable;
use rustc_session::Limit;
use rustc_span::def_id::LOCAL_CRATE;
use std::num::NonZeroU64;
use std::num::NonZero;
use thin_vec::ThinVec;
#[derive(Copy, Clone)]
@ -68,7 +68,7 @@ impl QueryContext for QueryCtxt<'_> {
#[inline]
fn next_job_id(self) -> QueryJobId {
QueryJobId(
NonZeroU64::new(
NonZero::<u64>::new(
self.query_system.jobs.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
)
.unwrap(),

View file

@ -1,5 +1,6 @@
#![feature(assert_matches)]
#![feature(core_intrinsics)]
#![feature(generic_nonzero)]
#![feature(hash_raw_entry)]
#![feature(min_specialization)]
#![feature(let_chains)]

View file

@ -11,7 +11,7 @@ use rustc_span::Span;
use std::hash::Hash;
use std::io::Write;
use std::num::NonZeroU64;
use std::num::NonZero;
#[cfg(parallel_compiler)]
use {
@ -36,7 +36,7 @@ pub type QueryMap = FxHashMap<QueryJobId, QueryJobInfo>;
/// A value uniquely identifying an active query job.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct QueryJobId(pub NonZeroU64);
pub struct QueryJobId(pub NonZero<u64>);
impl QueryJobId {
fn query(self, map: &QueryMap) -> QueryStackFrame {

View file

@ -11,6 +11,7 @@
#![feature(associated_type_bounds)]
#![feature(const_option)]
#![feature(core_intrinsics)]
#![feature(generic_nonzero)]
#![feature(inline_const)]
#![feature(min_specialization)]
#![feature(never_type)]

View file

@ -6,6 +6,7 @@ use std::cell::{Cell, RefCell};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
use std::hash::{BuildHasher, Hash};
use std::marker::PhantomData;
use std::num::NonZero;
use std::path;
use std::rc::Rc;
use std::sync::Arc;
@ -216,15 +217,15 @@ impl<D: Decoder> Decodable<D> for ! {
}
}
impl<S: Encoder> Encodable<S> for ::std::num::NonZeroU32 {
impl<S: Encoder> Encodable<S> for NonZero<u32> {
fn encode(&self, s: &mut S) {
s.emit_u32(self.get());
}
}
impl<D: Decoder> Decodable<D> for ::std::num::NonZeroU32 {
impl<D: Decoder> Decodable<D> for NonZero<u32> {
fn decode(d: &mut D) -> Self {
::std::num::NonZeroU32::new(d.read_u32()).unwrap()
NonZero::<u32>::new(d.read_u32()).unwrap()
}
}

View file

@ -3226,7 +3226,7 @@ pub(crate) mod dep_tracking {
};
use std::collections::BTreeMap;
use std::hash::{DefaultHasher, Hash};
use std::num::NonZeroUsize;
use std::num::NonZero;
use std::path::PathBuf;
pub trait DepTrackingHash {
@ -3268,7 +3268,7 @@ pub(crate) mod dep_tracking {
impl_dep_tracking_hash_via_hash!(
bool,
usize,
NonZeroUsize,
NonZero<usize>,
u64,
Hash64,
String,

View file

@ -1,4 +1,4 @@
use std::num::NonZeroU32;
use std::num::NonZero;
use rustc_ast::token;
use rustc_ast::util::literal::LitError;
@ -26,7 +26,7 @@ impl<'a> IntoDiagnostic<'a> for FeatureGateError {
#[derive(Subdiagnostic)]
#[note(session_feature_diagnostic_for_issue)]
pub struct FeatureDiagnosticForIssue {
pub n: NonZeroU32,
pub n: NonZero<u32>,
}
#[derive(Subdiagnostic)]

View file

@ -1,3 +1,4 @@
#![feature(generic_nonzero)]
#![feature(let_chains)]
#![feature(lazy_cell)]
#![feature(option_get_or_insert_default)]

View file

@ -21,7 +21,7 @@ use rustc_span::SourceFileHashAlgorithm;
use std::collections::BTreeMap;
use std::hash::{DefaultHasher, Hasher};
use std::num::{IntErrorKind, NonZeroUsize};
use std::num::{IntErrorKind, NonZero};
use std::path::PathBuf;
use std::str;
@ -617,7 +617,7 @@ mod parse {
pub(crate) fn parse_threads(slot: &mut usize, v: Option<&str>) -> bool {
match v.and_then(|s| s.parse().ok()) {
Some(0) => {
*slot = std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get);
*slot = std::thread::available_parallelism().map_or(1, NonZero::<usize>::get);
true
}
Some(i) => {
@ -991,7 +991,10 @@ mod parse {
true
}
pub(crate) fn parse_treat_err_as_bug(slot: &mut Option<NonZeroUsize>, v: Option<&str>) -> bool {
pub(crate) fn parse_treat_err_as_bug(
slot: &mut Option<NonZero<usize>>,
v: Option<&str>,
) -> bool {
match v {
Some(s) => match s.parse() {
Ok(val) => {
@ -1004,7 +1007,7 @@ mod parse {
}
},
None => {
*slot = NonZeroUsize::new(1);
*slot = NonZero::<usize>::new(1);
true
}
}
@ -1950,7 +1953,7 @@ written to standard error output)"),
"translate remapped paths into local paths when possible (default: yes)"),
trap_unreachable: Option<bool> = (None, parse_opt_bool, [TRACKED],
"generate trap instructions for unreachable intrinsics (default: use target setting, usually yes)"),
treat_err_as_bug: Option<NonZeroUsize> = (None, parse_treat_err_as_bug, [TRACKED],
treat_err_as_bug: Option<NonZero<usize>> = (None, parse_treat_err_as_bug, [TRACKED],
"treat the `val`th error that occurs as bug (default if not specified: 0 - don't treat errors as bugs. \
default if specified without a value: 1 - treat the first error as bug)"),
trim_diagnostic_paths: bool = (true, parse_bool, [UNTRACKED],