Skip to content

Commit e3315f6

Browse files
authored
Rollup merge of rust-lang#68976 - ecstatic-morse:const-non-zero, r=dtolnay
Make `num::NonZeroX::new` an unstable `const fn` cc rust-lang#53718 These require `#[feature(const_if_match)]`, meaning they must remain unstable for the time being.
2 parents b3cfb97 + 0755c41 commit e3315f6

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/libcore/num/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ assert_eq!(size_of::<Option<core::num::", stringify!($Ty), ">>(), size_of::<", s
6969

7070
/// Creates a non-zero if the given value is not zero.
7171
#[$stability]
72+
#[rustc_const_unstable(feature = "const_nonzero_int_methods", issue = "53718")]
7273
#[inline]
73-
pub fn new(n: $Int) -> Option<Self> {
74+
pub const fn new(n: $Int) -> Option<Self> {
7475
if n != 0 {
7576
// SAFETY: we just checked that there's no `0`
7677
Some(unsafe { Self(n) })

src/test/ui/consts/const-nonzero.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
// build-pass (FIXME(62277): could be check-pass?)
1+
// run-pass
2+
3+
#![feature(const_nonzero_int_methods)]
24

35
use std::num::NonZeroU8;
46

57
const X: NonZeroU8 = unsafe { NonZeroU8::new_unchecked(5) };
68
const Y: u8 = X.get();
79

10+
const ZERO: Option<NonZeroU8> = NonZeroU8::new(0);
11+
const ONE: Option<NonZeroU8> = NonZeroU8::new(1);
12+
813
fn main() {
14+
assert_eq!(Y, 5);
15+
16+
assert!(ZERO.is_none());
17+
assert_eq!(ONE.unwrap().get(), 1);
918
}

0 commit comments

Comments
 (0)