From 0b20ce97f7cc1360190cb9c2b102cdee4f487de2 Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Sat, 8 Feb 2020 16:02:20 -0800 Subject: [PATCH 1/2] Make `num::NonZeroX::new` an unstable `const fn` --- src/libcore/num/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index ed37b48b3e855..3ae8f0f7870c1 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -69,8 +69,9 @@ assert_eq!(size_of::>(), size_of::<", s /// Creates a non-zero if the given value is not zero. #[$stability] + #[rustc_const_unstable(feature = "const_nonzero_int_methods", issue = "53718")] #[inline] - pub fn new(n: $Int) -> Option { + pub const fn new(n: $Int) -> Option { if n != 0 { // SAFETY: we just checked that there's no `0` Some(unsafe { Self(n) }) From 0755c41ae26af20c940a80b3c1f686ab5916e655 Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Sat, 8 Feb 2020 16:02:37 -0800 Subject: [PATCH 2/2] Test `NonZeroU8::new` in a const context --- src/test/ui/consts/const-nonzero.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/test/ui/consts/const-nonzero.rs b/src/test/ui/consts/const-nonzero.rs index 6db3d1b3331fa..2160bad48074d 100644 --- a/src/test/ui/consts/const-nonzero.rs +++ b/src/test/ui/consts/const-nonzero.rs @@ -1,9 +1,18 @@ -// build-pass (FIXME(62277): could be check-pass?) +// run-pass + +#![feature(const_nonzero_int_methods)] use std::num::NonZeroU8; const X: NonZeroU8 = unsafe { NonZeroU8::new_unchecked(5) }; const Y: u8 = X.get(); +const ZERO: Option = NonZeroU8::new(0); +const ONE: Option = NonZeroU8::new(1); + fn main() { + assert_eq!(Y, 5); + + assert!(ZERO.is_none()); + assert_eq!(ONE.unwrap().get(), 1); }