Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert the value returned by NonZero*::get being non-zero #119490

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion library/core/src/num/nonzero.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Definitions of integer that is known not to equal zero.

use crate::fmt;
use crate::hint;
use crate::ops::{BitOr, BitOrAssign, Div, Neg, Rem};
use crate::str::FromStr;

Expand Down Expand Up @@ -104,6 +105,14 @@ macro_rules! nonzero_integers {
#[inline]
#[rustc_const_stable(feature = "const_nonzero_get", since = "1.34.0")]
pub const fn get(self) -> $Int {
// SAFETY: nonzero types do not contain zero values.
unsafe {
// We can’t use `intrinsics::assume(self.0 != 0)` here because it is not const-stable.
if self.0 == 0 {
hint::unreachable_unchecked();
}
}

self.0
}

Expand All @@ -114,7 +123,8 @@ macro_rules! nonzero_integers {
#[doc = concat!("Converts a `", stringify!($Ty), "` into an `", stringify!($Int), "`")]
#[inline]
fn from(nonzero: $Ty) -> Self {
nonzero.0
// Utilize the non-zero assertion from the `get` function.
nonzero.get()
}
}

Expand Down
106 changes: 106 additions & 0 deletions tests/codegen/nonzero.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// compile-flags: -C opt-level=1 -Z merge-functions=disabled

#![crate_type = "lib"]

use core::num::{
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
};

extern crate core;

// CHECK-LABEL: void @non_zero_i8_is_not_zero(i8
#[no_mangle]
fn non_zero_i8_is_not_zero(x: NonZeroI8) {
// CHECK-NOT: br
assert_ne!(x.get(), 0);
assert_ne!(i8::from(x), 0);
}

// CHECK-LABEL: void @non_zero_i16_is_not_zero(i16
#[no_mangle]
fn non_zero_i16_is_not_zero(x: NonZeroI16) {
// CHECK-NOT: br
assert_ne!(x.get(), 0);
assert_ne!(i16::from(x), 0);
}

// CHECK-LABEL: void @non_zero_i32_is_not_zero(i32
#[no_mangle]
fn non_zero_i32_is_not_zero(x: NonZeroI32) {
// CHECK-NOT: br
assert_ne!(x.get(), 0);
assert_ne!(i32::from(x), 0);
}

// CHECK-LABEL: void @non_zero_i64_is_not_zero(i64
#[no_mangle]
fn non_zero_i64_is_not_zero(x: NonZeroI64) {
// CHECK-NOT: br
assert_ne!(x.get(), 0);
assert_ne!(i64::from(x), 0);
}

// CHECK-LABEL: void @non_zero_i128_is_not_zero(i128
#[no_mangle]
fn non_zero_i128_is_not_zero(x: NonZeroI128) {
// CHECK-NOT: br
assert_ne!(x.get(), 0);
assert_ne!(i128::from(x), 0);
}

// CHECK-LABEL: void @non_zero_isize_is_not_zero(i
#[no_mangle]
fn non_zero_isize_is_not_zero(x: NonZeroIsize) {
// CHECK-NOT: br
assert_ne!(x.get(), 0);
assert_ne!(isize::from(x), 0);
}

// CHECK-LABEL: void @non_zero_u8_is_not_zero(i8
#[no_mangle]
fn non_zero_u8_is_not_zero(x: NonZeroU8) {
// CHECK-NOT: br
assert_ne!(x.get(), 0);
assert_ne!(u8::from(x), 0);
}

// CHECK-LABEL: void @non_zero_u16_is_not_zero(i16
#[no_mangle]
fn non_zero_u16_is_not_zero(x: NonZeroU16) {
// CHECK-NOT: br
assert_ne!(x.get(), 0);
assert_ne!(u16::from(x), 0);
}

// CHECK-LABEL: void @non_zero_u32_is_not_zero(i32
#[no_mangle]
fn non_zero_u32_is_not_zero(x: NonZeroU32) {
// CHECK-NOT: br
assert_ne!(x.get(), 0);
assert_ne!(u32::from(x), 0);
}

// CHECK-LABEL: void @non_zero_u64_is_not_zero(i64
#[no_mangle]
fn non_zero_u64_is_not_zero(x: NonZeroU64) {
// CHECK-NOT: br
assert_ne!(x.get(), 0);
assert_ne!(u64::from(x), 0);
}

// CHECK-LABEL: void @non_zero_u128_is_not_zero(i128
#[no_mangle]
fn non_zero_u128_is_not_zero(x: NonZeroU128) {
// CHECK-NOT: br
assert_ne!(x.get(), 0);
assert_ne!(u128::from(x), 0);
}

// CHECK-LABEL: void @non_zero_usize_is_not_zero(i
#[no_mangle]
fn non_zero_usize_is_not_zero(x: NonZeroUsize) {
// CHECK-NOT: br
assert_ne!(x.get(), 0);
assert_ne!(usize::from(x), 0);
}
Loading