Skip to content

Commit

Permalink
Rollup merge of #124587 - reitermarkus:use-generic-nonzero, r=dtolnay
Browse files Browse the repository at this point in the history
Generic `NonZero` post-stabilization changes.

Tracking issue: #120257

r? ``@dtolnay``
  • Loading branch information
matthiaskrgr authored May 8, 2024
2 parents d30af5e + bd8e565 commit d8a3a69
Show file tree
Hide file tree
Showing 31 changed files with 399 additions and 440 deletions.
7 changes: 4 additions & 3 deletions compiler/rustc_abi/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::borrow::{Borrow, Cow};
use std::cmp;
use std::fmt::{self, Write};
use std::iter;
use std::num::NonZero;
use std::ops::Bound;
use std::ops::Deref;

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

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

// A variant is absent if it's uninhabited and only has ZST fields.
Expand Down Expand Up @@ -327,7 +328,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::new(only_variant.len())?),
abi,
largest_niche: None,
align,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![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 @@ -1149,7 +1149,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
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2164,7 +2164,7 @@ pub enum TyKind {
MacCall(P<MacCall>),
/// Placeholder for a `va_list`.
CVarArgs,
/// Pattern types like `pattern_type!(u32 is 1..=)`, which is the same as `NonZeroU32`,
/// Pattern types like `pattern_type!(u32 is 1..=)`, which is the same as `NonZero<u32>`,
/// just as part of the type system.
Pat(P<Ty>, P<Pat>),
/// Sometimes we need a dummy value when no error has occurred.
Expand Down
29 changes: 6 additions & 23 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2227,15 +2227,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
) -> bool {
let tcx = self.tcx;
let (adt, args, unwrap) = match expected.kind() {
// In case Option<NonZero*> is wanted, but * is provided, suggest calling new
// In case `Option<NonZero<T>>` is wanted, but `T` is provided, suggest calling `new`.
ty::Adt(adt, args) if tcx.is_diagnostic_item(sym::Option, adt.did()) => {
let nonzero_type = args.type_at(0); // Unwrap option type.
let ty::Adt(adt, args) = nonzero_type.kind() else {
return false;
};
(adt, args, "")
}
// In case `NonZero<*>` is wanted but `*` is provided, also add `.unwrap()` to satisfy types.
// In case `NonZero<T>` is wanted but `T` is provided, also add `.unwrap()` to satisfy types.
ty::Adt(adt, args) => (adt, args, ".unwrap()"),
_ => return false,
};
Expand All @@ -2244,32 +2244,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return false;
}

// FIXME: This can be simplified once `NonZero<T>` is stable.
let coercable_types = [
("NonZeroU8", tcx.types.u8),
("NonZeroU16", tcx.types.u16),
("NonZeroU32", tcx.types.u32),
("NonZeroU64", tcx.types.u64),
("NonZeroU128", tcx.types.u128),
("NonZeroI8", tcx.types.i8),
("NonZeroI16", tcx.types.i16),
("NonZeroI32", tcx.types.i32),
("NonZeroI64", tcx.types.i64),
("NonZeroI128", tcx.types.i128),
];

let int_type = args.type_at(0);

let Some(nonzero_alias) = coercable_types.iter().find_map(|(nonzero_alias, t)| {
if *t == int_type && self.can_coerce(expr_ty, *t) { Some(nonzero_alias) } else { None }
}) else {
if !self.can_coerce(expr_ty, int_type) {
return false;
};
}

err.multipart_suggestion(
format!("consider calling `{nonzero_alias}::new`"),
format!("consider calling `{}::new`", sym::NonZero),
vec![
(expr.span.shrink_to_lo(), format!("{nonzero_alias}::new(")),
(expr.span.shrink_to_lo(), format!("{}::new(", sym::NonZero)),
(expr.span.shrink_to_hi(), format!("){unwrap}")),
],
Applicability::MaybeIncorrect,
Expand Down
4 changes: 2 additions & 2 deletions compiler/stable_mir/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::ty::{Align, IndexedVal, Ty, VariantIdx};
use crate::Error;
use crate::Opaque;
use std::fmt::{self, Debug};
use std::num::NonZeroUsize;
use std::num::NonZero;
use std::ops::RangeInclusive;

/// A function ABI definition.
Expand Down Expand Up @@ -133,7 +133,7 @@ pub enum FieldsShape {
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
10 changes: 5 additions & 5 deletions library/core/src/iter/adapters/step_by.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
intrinsics,
iter::{from_fn, TrustedLen, TrustedRandomAccess},
num::NonZeroUsize,
num::NonZero,
ops::{Range, Try},
};

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

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

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

Expand Down
Loading

0 comments on commit d8a3a69

Please sign in to comment.