Skip to content

Commit d174a49

Browse files
authored
Rollup merge of rust-lang#124587 - reitermarkus:use-generic-nonzero, r=dtolnay
Generic `NonZero` post-stabilization changes. Tracking issue: rust-lang#120257 r? `@dtolnay`
2 parents 14bdce1 + bd8e565 commit d174a49

31 files changed

+399
-440
lines changed

compiler/rustc_abi/src/layout.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::borrow::{Borrow, Cow};
22
use std::cmp;
33
use std::fmt::{self, Write};
44
use std::iter;
5+
use std::num::NonZero;
56
use std::ops::Bound;
67
use std::ops::Deref;
78

@@ -10,8 +11,8 @@ use tracing::debug;
1011

1112
use crate::{
1213
Abi, AbiAndPrefAlign, Align, FieldsShape, IndexSlice, IndexVec, Integer, LayoutS, Niche,
13-
NonZeroUsize, Primitive, ReprOptions, Scalar, Size, StructKind, TagEncoding, TargetDataLayout,
14-
Variants, WrappingRange,
14+
Primitive, ReprOptions, Scalar, Size, StructKind, TagEncoding, TargetDataLayout, Variants,
15+
WrappingRange,
1516
};
1617

1718
// A variant is absent if it's uninhabited and only has ZST fields.
@@ -327,7 +328,7 @@ pub trait LayoutCalculator {
327328

328329
Some(LayoutS {
329330
variants: Variants::Single { index: VariantIdx::new(0) },
330-
fields: FieldsShape::Union(NonZeroUsize::new(only_variant.len())?),
331+
fields: FieldsShape::Union(NonZero::new(only_variant.len())?),
331332
abi,
332333
largest_niche: None,
333334
align,

compiler/rustc_abi/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#![cfg_attr(feature = "nightly", feature(rustdoc_internals))]
55

66
use std::fmt;
7-
use std::num::{NonZeroUsize, ParseIntError};
7+
use std::num::{NonZero, ParseIntError};
88
use std::ops::{Add, AddAssign, Mul, RangeInclusive, Sub};
99
use std::str::FromStr;
1010

@@ -1149,7 +1149,7 @@ pub enum FieldsShape<FieldIdx: Idx> {
11491149
Primitive,
11501150

11511151
/// All fields start at no offset. The `usize` is the field count.
1152-
Union(NonZeroUsize),
1152+
Union(NonZero<usize>),
11531153

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

compiler/rustc_ast/src/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2164,7 +2164,7 @@ pub enum TyKind {
21642164
MacCall(P<MacCall>),
21652165
/// Placeholder for a `va_list`.
21662166
CVarArgs,
2167-
/// Pattern types like `pattern_type!(u32 is 1..=)`, which is the same as `NonZeroU32`,
2167+
/// Pattern types like `pattern_type!(u32 is 1..=)`, which is the same as `NonZero<u32>`,
21682168
/// just as part of the type system.
21692169
Pat(P<Ty>, P<Pat>),
21702170
/// Sometimes we need a dummy value when no error has occurred.

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+6-23
Original file line numberDiff line numberDiff line change
@@ -2227,15 +2227,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22272227
) -> bool {
22282228
let tcx = self.tcx;
22292229
let (adt, args, unwrap) = match expected.kind() {
2230-
// In case Option<NonZero*> is wanted, but * is provided, suggest calling new
2230+
// In case `Option<NonZero<T>>` is wanted, but `T` is provided, suggest calling `new`.
22312231
ty::Adt(adt, args) if tcx.is_diagnostic_item(sym::Option, adt.did()) => {
22322232
let nonzero_type = args.type_at(0); // Unwrap option type.
22332233
let ty::Adt(adt, args) = nonzero_type.kind() else {
22342234
return false;
22352235
};
22362236
(adt, args, "")
22372237
}
2238-
// In case `NonZero<*>` is wanted but `*` is provided, also add `.unwrap()` to satisfy types.
2238+
// In case `NonZero<T>` is wanted but `T` is provided, also add `.unwrap()` to satisfy types.
22392239
ty::Adt(adt, args) => (adt, args, ".unwrap()"),
22402240
_ => return false,
22412241
};
@@ -2244,32 +2244,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22442244
return false;
22452245
}
22462246

2247-
// FIXME: This can be simplified once `NonZero<T>` is stable.
2248-
let coercable_types = [
2249-
("NonZeroU8", tcx.types.u8),
2250-
("NonZeroU16", tcx.types.u16),
2251-
("NonZeroU32", tcx.types.u32),
2252-
("NonZeroU64", tcx.types.u64),
2253-
("NonZeroU128", tcx.types.u128),
2254-
("NonZeroI8", tcx.types.i8),
2255-
("NonZeroI16", tcx.types.i16),
2256-
("NonZeroI32", tcx.types.i32),
2257-
("NonZeroI64", tcx.types.i64),
2258-
("NonZeroI128", tcx.types.i128),
2259-
];
2260-
22612247
let int_type = args.type_at(0);
2262-
2263-
let Some(nonzero_alias) = coercable_types.iter().find_map(|(nonzero_alias, t)| {
2264-
if *t == int_type && self.can_coerce(expr_ty, *t) { Some(nonzero_alias) } else { None }
2265-
}) else {
2248+
if !self.can_coerce(expr_ty, int_type) {
22662249
return false;
2267-
};
2250+
}
22682251

22692252
err.multipart_suggestion(
2270-
format!("consider calling `{nonzero_alias}::new`"),
2253+
format!("consider calling `{}::new`", sym::NonZero),
22712254
vec![
2272-
(expr.span.shrink_to_lo(), format!("{nonzero_alias}::new(")),
2255+
(expr.span.shrink_to_lo(), format!("{}::new(", sym::NonZero)),
22732256
(expr.span.shrink_to_hi(), format!("){unwrap}")),
22742257
],
22752258
Applicability::MaybeIncorrect,

compiler/stable_mir/src/abi.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::ty::{Align, IndexedVal, Ty, VariantIdx};
66
use crate::Error;
77
use crate::Opaque;
88
use std::fmt::{self, Debug};
9-
use std::num::NonZeroUsize;
9+
use std::num::NonZero;
1010
use std::ops::RangeInclusive;
1111

1212
/// A function ABI definition.
@@ -133,7 +133,7 @@ pub enum FieldsShape {
133133
Primitive,
134134

135135
/// All fields start at no offset. The `usize` is the field count.
136-
Union(NonZeroUsize),
136+
Union(NonZero<usize>),
137137

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

library/core/src/iter/adapters/step_by.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
intrinsics,
33
iter::{from_fn, TrustedLen, TrustedRandomAccess},
4-
num::NonZeroUsize,
4+
num::NonZero,
55
ops::{Range, Try},
66
};
77

@@ -42,10 +42,10 @@ impl<I> StepBy<I> {
4242
/// The `step` that was originally passed to `Iterator::step_by(step)`,
4343
/// aka `self.step_minus_one + 1`.
4444
#[inline]
45-
fn original_step(&self) -> NonZeroUsize {
45+
fn original_step(&self) -> NonZero<usize> {
4646
// SAFETY: By type invariant, `step_minus_one` cannot be `MAX`, which
4747
// means the addition cannot overflow and the result cannot be zero.
48-
unsafe { NonZeroUsize::new_unchecked(intrinsics::unchecked_add(self.step_minus_one, 1)) }
48+
unsafe { NonZero::new_unchecked(intrinsics::unchecked_add(self.step_minus_one, 1)) }
4949
}
5050
}
5151

@@ -231,12 +231,12 @@ unsafe impl<I: Iterator> StepByImpl<I> for StepBy<I> {
231231
#[inline]
232232
default fn spec_size_hint(&self) -> (usize, Option<usize>) {
233233
#[inline]
234-
fn first_size(step: NonZeroUsize) -> impl Fn(usize) -> usize {
234+
fn first_size(step: NonZero<usize>) -> impl Fn(usize) -> usize {
235235
move |n| if n == 0 { 0 } else { 1 + (n - 1) / step }
236236
}
237237

238238
#[inline]
239-
fn other_size(step: NonZeroUsize) -> impl Fn(usize) -> usize {
239+
fn other_size(step: NonZero<usize>) -> impl Fn(usize) -> usize {
240240
move |n| n / step
241241
}
242242

0 commit comments

Comments
 (0)