Skip to content

Commit

Permalink
Replace NonZero::<_>::new with NonZero::new.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Feb 15, 2024
1 parent 746a58d commit a90cc05
Show file tree
Hide file tree
Showing 70 changed files with 175 additions and 216 deletions.
5 changes: 2 additions & 3 deletions compiler/rustc_data_structures/src/tagged_ptr/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,14 @@ where
// `{non_zero} | packed_tag` can't make the value zero.

let packed = (addr.get() >> T::BITS) | packed_tag;
unsafe { NonZero::<usize>::new_unchecked(packed) }
unsafe { NonZero::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 { NonZero::<usize>::new_unchecked(addr.get() << T::BITS) })
self.packed.map_addr(|addr| unsafe { NonZero::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_feature/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const fn to_nonzero(n: Option<u32>) -> Option<NonZero<u32>> {
// in const context. Requires https://github.com/rust-lang/rfcs/pull/2632.
match n {
None => None,
Some(n) => NonZero::<u32>::new(n),
Some(n) => NonZero::new(n),
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, NonZero::<usize>::new(1));
tracked!(treat_err_as_bug, NonZero::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::NonZero::<usize>::new(threads).unwrap());
let registry = sync::Registry::new(std::num::NonZero::new(threads).unwrap());

if !sync::is_dyn_thread_safe() {
return run_in_thread_with_globals(edition, || {
Expand Down
10 changes: 4 additions & 6 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
}
LazyState::Previous(last_pos) => last_pos.get() + distance,
};
let position = NonZero::<usize>::new(position).unwrap();
let position = NonZero::new(position).unwrap();
self.lazy_state = LazyState::Previous(position);
f(position)
}
Expand Down Expand Up @@ -685,17 +685,15 @@ impl MetadataBlob {
}

pub(crate) fn get_rustc_version(&self) -> String {
LazyValue::<String>::from_position(
NonZero::<usize>::new(METADATA_HEADER.len() + 8).unwrap(),
)
.decode(self)
LazyValue::<String>::from_position(NonZero::new(METADATA_HEADER.len() + 8).unwrap())
.decode(self)
}

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);
NonZero::<usize>::new(pos as usize).unwrap()
NonZero::new(pos as usize).unwrap()
}

pub(crate) fn get_header(&self) -> CrateHeader {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,15 +439,15 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
position.get() - last_pos.get()
}
};
self.lazy_state = LazyState::Previous(NonZero::<usize>::new(pos).unwrap());
self.lazy_state = LazyState::Previous(NonZero::new(pos).unwrap());
self.emit_usize(distance);
}

fn lazy<T: ParameterizedOverTcx, B: Borrow<T::Value<'tcx>>>(&mut self, value: B) -> LazyValue<T>
where
T::Value<'tcx>: Encodable<EncodeContext<'a, 'tcx>>,
{
let pos = NonZero::<usize>::new(self.position()).unwrap();
let pos = NonZero::new(self.position()).unwrap();

assert_eq!(self.lazy_state, LazyState::NoNode);
self.lazy_state = LazyState::NodeStart(pos);
Expand All @@ -466,7 +466,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
where
T::Value<'tcx>: Encodable<EncodeContext<'a, 'tcx>>,
{
let pos = NonZero::<usize>::new(self.position()).unwrap();
let pos = NonZero::new(self.position()).unwrap();

assert_eq!(self.lazy_state, LazyState::NoNode);
self.lazy_state = LazyState::NodeStart(pos);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl<T: ParameterizedOverTcx> ParameterizedOverTcx for LazyArray<T> {

impl<T> Default for LazyArray<T> {
fn default() -> LazyArray<T> {
LazyArray::from_position_and_num_elems(NonZero::<usize>::new(1).unwrap(), 0)
LazyArray::from_position_and_num_elems(NonZero::new(1).unwrap(), 0)
}
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_metadata/src/rmeta/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ impl<T> FixedSizeEncoding for Option<LazyValue<T>> {

#[inline]
fn from_bytes(b: &[u8; 8]) -> Self {
let position = NonZero::<usize>::new(u64::from_bytes(b) as usize)?;
let position = NonZero::new(u64::from_bytes(b) as usize)?;
Some(LazyValue::from_position(position))
}

Expand All @@ -366,7 +366,7 @@ impl<T> LazyArray<T> {
}

fn from_bytes_impl(position: &[u8; 8], meta: &[u8; 8]) -> Option<LazyArray<T>> {
let position = NonZero::<usize>::new(u64::from_bytes(position) as usize)?;
let position = NonZero::new(u64::from_bytes(position) as usize)?;
let len = u64::from_bytes(meta) as usize;
Some(LazyArray::from_position_and_num_elems(position, len))
}
Expand Down Expand Up @@ -497,7 +497,7 @@ impl<I: Idx, const N: usize, T: FixedSizeEncoding<ByteArray = [u8; N]>> TableBui
}

LazyTable::from_position_and_encoded_size(
NonZero::<usize>::new(pos).unwrap(),
NonZero::new(pos).unwrap(),
width,
self.blocks.len(),
)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 == NonZero::<u32>::new(27812) {
if feature == sym::rustc_private && issue == NonZero::new(27812) {
if self.sess.opts.unstable_opts.force_unstable_if_unmarked {
return EvalResult::Allow;
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/mir/interpret/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ impl<'tcx> AllocMap<'tcx> {
AllocMap {
alloc_map: Default::default(),
dedup: Default::default(),
next_id: AllocId(NonZero::<u64>::new(1).unwrap()),
next_id: AllocId(NonZero::new(1).unwrap()),
}
}
fn reserve(&mut self) -> AllocId {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/mir/interpret/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl CtfeProvenance {
/// Returns the `AllocId` of this provenance.
#[inline(always)]
pub fn alloc_id(self) -> AllocId {
AllocId(NonZero::<u64>::new(self.0.get() & !IMMUTABLE_MASK).unwrap())
AllocId(NonZero::new(self.0.get() & !IMMUTABLE_MASK).unwrap())
}

/// Returns whether this provenance is immutable.
Expand Down
23 changes: 10 additions & 13 deletions compiler/rustc_middle/src/ty/consts/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: NonZero::<u8>::new(size).unwrap() }
ScalarInt { data: u128::from_le_bytes(data), size: NonZero::new(size).unwrap() }
}
}

impl ScalarInt {
pub const TRUE: ScalarInt = ScalarInt { data: 1_u128, size: NonZero::<u8>::new(1).unwrap() };
pub const TRUE: ScalarInt = ScalarInt { data: 1_u128, size: NonZero::new(1).unwrap() };

pub const FALSE: ScalarInt = ScalarInt { data: 0_u128, size: NonZero::<u8>::new(1).unwrap() };
pub const FALSE: ScalarInt = ScalarInt { data: 0_u128, size: NonZero::new(1).unwrap() };

#[inline]
pub fn size(self) -> Size {
Expand Down Expand Up @@ -196,7 +196,7 @@ impl ScalarInt {

#[inline]
pub fn null(size: Size) -> Self {
Self { data: 0, size: NonZero::<u8>::new(size.bytes() as u8).unwrap() }
Self { data: 0, size: NonZero::new(size.bytes() as u8).unwrap() }
}

#[inline]
Expand All @@ -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: NonZero::<u8>::new(size.bytes() as u8).unwrap() })
Some(Self { data, size: NonZero::new(size.bytes() as u8).unwrap() })
} else {
None
}
Expand All @@ -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: NonZero::<u8>::new(size.bytes() as u8).unwrap() })
Some(Self { data: truncated, size: NonZero::new(size.bytes() as u8).unwrap() })
} else {
None
}
Expand Down Expand Up @@ -388,7 +388,7 @@ macro_rules! from {
fn from(u: $ty) -> Self {
Self {
data: u128::from(u),
size: NonZero::<u8>::new(std::mem::size_of::<$ty>() as u8).unwrap(),
size: NonZero::new(std::mem::size_of::<$ty>() as u8).unwrap(),
}
}
}
Expand Down Expand Up @@ -427,10 +427,7 @@ impl TryFrom<ScalarInt> for bool {
impl From<char> for ScalarInt {
#[inline]
fn from(c: char) -> Self {
Self {
data: c as u128,
size: NonZero::<u8>::new(std::mem::size_of::<char>() as u8).unwrap(),
}
Self { data: c as u128, size: NonZero::new(std::mem::size_of::<char>() as u8).unwrap() }
}
}

Expand All @@ -457,7 +454,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: NonZero::<u8>::new((Single::BITS / 8) as u8).unwrap() }
Self { data: f.to_bits(), size: NonZero::new((Single::BITS / 8) as u8).unwrap() }
}
}

Expand All @@ -473,7 +470,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: NonZero::<u8>::new((Double::BITS / 8) as u8).unwrap() }
Self { data: f.to_bits(), size: NonZero::new((Double::BITS / 8) as u8).unwrap() }
}
}

Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_middle/src/ty/generic_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,8 @@ impl<'tcx> From<ty::Term<'tcx>> for GenericArg<'tcx> {
impl<'tcx> GenericArg<'tcx> {
#[inline]
pub fn unpack(self) -> GenericArgKind<'tcx> {
let ptr = unsafe {
self.ptr.map_addr(|addr| NonZero::<usize>::new_unchecked(addr.get() & !TAG_MASK))
};
let ptr =
unsafe { self.ptr.map_addr(|addr| NonZero::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()`,
// and this is just going in the other direction.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ where
};
tcx.mk_layout(LayoutS {
variants: Variants::Single { index: variant_index },
fields: match NonZero::<usize>::new(fields) {
fields: match NonZero::new(fields) {
Some(fields) => FieldsShape::Union(fields),
None => FieldsShape::Arbitrary { offsets: IndexVec::new(), memory_index: IndexVec::new() },
},
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,9 +617,8 @@ impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for Term<'tcx> {
impl<'tcx> Term<'tcx> {
#[inline]
pub fn unpack(self) -> TermKind<'tcx> {
let ptr = unsafe {
self.ptr.map_addr(|addr| NonZero::<usize>::new_unchecked(addr.get() & !TAG_MASK))
};
let ptr =
unsafe { self.ptr.map_addr(|addr| NonZero::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()`,
// and this is just going in the other direction.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_passes/src/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ fn stability_index(tcx: TyCtxt<'_>, (): ()) -> Index {
let stability = Stability {
level: attr::StabilityLevel::Unstable {
reason: UnstableReason::Default,
issue: NonZero::<u32>::new(27812),
issue: NonZero::new(27812),
is_soft: false,
implied_by: None,
},
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_query_impl/src/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,8 @@ impl QueryContext for QueryCtxt<'_> {
#[inline]
fn next_job_id(self) -> QueryJobId {
QueryJobId(
NonZero::<u64>::new(
self.query_system.jobs.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
)
.unwrap(),
NonZero::new(self.query_system.jobs.fetch_add(1, std::sync::atomic::Ordering::Relaxed))
.unwrap(),
)
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_serialize/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl<S: Encoder> Encodable<S> for NonZero<u32> {

impl<D: Decoder> Decodable<D> for NonZero<u32> {
fn decode(d: &mut D) -> Self {
NonZero::<u32>::new(d.read_u32()).unwrap()
NonZero::new(d.read_u32()).unwrap()
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ mod parse {
}
},
None => {
*slot = NonZero::<usize>::new(1);
*slot = NonZero::new(1);
true
}
}
Expand Down
6 changes: 3 additions & 3 deletions library/alloc/src/collections/binary_heap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ impl<T: Ord, A: Allocator> DerefMut for PeekMut<'_, T, A> {
// the standard library as "leak amplification".
unsafe {
// SAFETY: len > 1 so len != 0.
self.original_len = Some(NonZero::<usize>::new_unchecked(len));
self.original_len = Some(NonZero::new_unchecked(len));
// SAFETY: len > 1 so all this does for now is leak elements,
// which is safe.
self.heap.data.set_len(1);
Expand Down Expand Up @@ -1576,8 +1576,8 @@ unsafe impl<T, A: Allocator> SourceIter for IntoIter<T, A> {
#[unstable(issue = "none", feature = "inplace_iteration")]
#[doc(hidden)]
unsafe impl<I, A: Allocator> InPlaceIterable for IntoIter<I, A> {
const EXPAND_BY: Option<NonZero<usize>> = NonZero::<usize>::new(1);
const MERGE_BY: Option<NonZero<usize>> = NonZero::<usize>::new(1);
const EXPAND_BY: Option<NonZero<usize>> = NonZero::new(1);
const MERGE_BY: Option<NonZero<usize>> = NonZero::new(1);
}

unsafe impl<I> AsVecIntoIter for IntoIter<I> {
Expand Down
4 changes: 2 additions & 2 deletions library/alloc/src/collections/vec_deque/into_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
self.inner.drain(..n);
0
};
NonZero::<usize>::new(rem).map_or(Ok(()), Err)
NonZero::new(rem).map_or(Ok(()), Err)
}

#[inline]
Expand Down Expand Up @@ -192,7 +192,7 @@ impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> {
self.inner.truncate(len - n);
0
};
NonZero::<usize>::new(rem).map_or(Ok(()), Err)
NonZero::new(rem).map_or(Ok(()), Err)
}

fn try_rfold<B, F, R>(&mut self, mut init: B, mut f: F) -> R
Expand Down
8 changes: 4 additions & 4 deletions library/alloc/src/vec/into_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
unsafe {
ptr::drop_in_place(to_drop);
}
NonZero::<usize>::new(n - step_size).map_or(Ok(()), Err)
NonZero::new(n - step_size).map_or(Ok(()), Err)
}

#[inline]
Expand Down Expand Up @@ -350,7 +350,7 @@ impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> {
unsafe {
ptr::drop_in_place(to_drop);
}
NonZero::<usize>::new(n - step_size).map_or(Ok(()), Err)
NonZero::new(n - step_size).map_or(Ok(()), Err)
}
}

Expand Down Expand Up @@ -457,8 +457,8 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for IntoIter<T, A> {
#[unstable(issue = "none", feature = "inplace_iteration")]
#[doc(hidden)]
unsafe impl<T, A: Allocator> InPlaceIterable for IntoIter<T, A> {
const EXPAND_BY: Option<NonZero<usize>> = NonZero::<usize>::new(1);
const MERGE_BY: Option<NonZero<usize>> = NonZero::<usize>::new(1);
const EXPAND_BY: Option<NonZero<usize>> = NonZero::new(1);
const MERGE_BY: Option<NonZero<usize>> = NonZero::new(1);
}

#[unstable(issue = "none", feature = "inplace_iteration")]
Expand Down
Loading

0 comments on commit a90cc05

Please sign in to comment.