diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 30b7b45468412..9650ecfb969d1 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -86,8 +86,9 @@ assert_eq!(size_of::>(), size_of::<", st /// Returns the value as a primitive type. #[stable(feature = "nonzero", since = "1.28.0")] + #[rustc_const_unstable(feature = "const_nonzero_methods")] #[inline] - pub fn get(self) -> $Int { + pub const fn get(self) -> $Int { self.0 .0 } diff --git a/src/librustc/hir/def_id.rs b/src/librustc/hir/def_id.rs index e378e1b8be0e9..2abfd8b225a4d 100644 --- a/src/librustc/hir/def_id.rs +++ b/src/librustc/hir/def_id.rs @@ -48,7 +48,7 @@ impl ::std::fmt::Debug for CrateNum { /// Item definitions in the currently-compiled crate would have the CrateNum /// LOCAL_CRATE in their DefId. -pub const LOCAL_CRATE: CrateNum = CrateNum::Index(CrateId::from_u32_const(0)); +pub const LOCAL_CRATE: CrateNum = CrateNum::Index(CrateId::from_u32(0)); impl Idx for CrateNum { diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs index 0aa964a44fd2c..e9a909444660b 100644 --- a/src/librustc/lib.rs +++ b/src/librustc/lib.rs @@ -70,6 +70,8 @@ #![feature(macro_at_most_once_rep)] #![feature(crate_visibility_modifier)] #![feature(transpose_result)] +#![feature(const_fn)] +#![feature(const_nonzero_methods)] #![recursion_limit="512"] diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index ef9b3e3efab27..bbc26dde41b56 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -1498,7 +1498,7 @@ newtype_index! { impl_stable_hash_for!(struct UniverseIndex { private }); impl UniverseIndex { - pub const ROOT: UniverseIndex = UniverseIndex::from_u32_const(0); + pub const ROOT: UniverseIndex = UniverseIndex::from_u32(0); /// Returns the "next" universe index in order -- this new index /// is considered to extend all previous universes. This diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs index a4130bf15cb82..cdccab7e0c4ae 100644 --- a/src/librustc/ty/sty.rs +++ b/src/librustc/ty/sty.rs @@ -1325,7 +1325,7 @@ impl DebruijnIndex { /// /// you would need to shift the index for `'a` into 1 new binder. #[must_use] - pub fn shifted_in(self, amount: u32) -> DebruijnIndex { + pub const fn shifted_in(self, amount: u32) -> DebruijnIndex { DebruijnIndex::from_u32(self.as_u32() + amount) } @@ -1338,7 +1338,7 @@ impl DebruijnIndex { /// Returns the resulting index when this value is moved out from /// `amount` number of new binders. #[must_use] - pub fn shifted_out(self, amount: u32) -> DebruijnIndex { + pub const fn shifted_out(self, amount: u32) -> DebruijnIndex { DebruijnIndex::from_u32(self.as_u32() - amount) } diff --git a/src/librustc_data_structures/indexed_vec.rs b/src/librustc_data_structures/indexed_vec.rs index a59bf9d530c4d..83e8ff7e5ffc1 100644 --- a/src/librustc_data_structures/indexed_vec.rs +++ b/src/librustc_data_structures/indexed_vec.rs @@ -107,7 +107,7 @@ macro_rules! newtype_index { impl $type { $v const MAX_AS_U32: u32 = $max; - $v const MAX: $type = $type::from_u32_const($max); + $v const MAX: $type = $type::from_u32($max); #[inline] $v fn from_usize(value: usize) -> Self { @@ -118,18 +118,7 @@ macro_rules! newtype_index { } #[inline] - $v fn from_u32(value: u32) -> Self { - assert!(value <= $max); - unsafe { - $type::from_u32_unchecked(value) - } - } - - /// Hacky variant of `from_u32` for use in constants. - /// This version checks the "max" constraint by using an - /// invalid array dereference. - #[inline] - $v const fn from_u32_const(value: u32) -> Self { + $v const fn from_u32(value: u32) -> Self { // This will fail at const eval time unless `value <= // max` is true (in which case we get the index 0). // It will also fail at runtime, of course, but in a @@ -139,7 +128,7 @@ macro_rules! newtype_index { ]; unsafe { - $type { private: value } + $type::from_u32_unchecked(value) } } @@ -156,7 +145,7 @@ macro_rules! newtype_index { /// Extract value of this index as a usize. #[inline] - $v fn as_u32(self) -> u32 { + $v const fn as_u32(self) -> u32 { self.private } @@ -445,7 +434,7 @@ macro_rules! newtype_index { const $name:ident = $constant:expr, $($tokens:tt)*) => ( $(#[doc = $doc])* - pub const $name: $type = $type::from_u32_const($constant); + pub const $name: $type = $type::from_u32($constant); newtype_index!( @derives [$($derives,)*] @type [$type] diff --git a/src/librustc_data_structures/lib.rs b/src/librustc_data_structures/lib.rs index 07e5548216f3c..12e937744e26b 100644 --- a/src/librustc_data_structures/lib.rs +++ b/src/librustc_data_structures/lib.rs @@ -29,6 +29,7 @@ #![feature(nll)] #![feature(allow_internal_unstable)] #![feature(vec_resize_with)] +#![feature(const_nonzero_methods)] #![cfg_attr(unix, feature(libc))] #![cfg_attr(test, feature(test))] diff --git a/src/librustc_driver/test.rs b/src/librustc_driver/test.rs index 28b7c610a91c0..82214de957f75 100644 --- a/src/librustc_driver/test.rs +++ b/src/librustc_driver/test.rs @@ -199,13 +199,8 @@ fn test_env_with_pool( ); } -fn d1() -> ty::DebruijnIndex { - ty::INNERMOST -} - -fn d2() -> ty::DebruijnIndex { - d1().shifted_in(1) -} +const D1: ty::DebruijnIndex = ty::INNERMOST; +const D2: ty::DebruijnIndex = D1.shifted_in(1); impl<'a, 'gcx, 'tcx> Env<'a, 'gcx, 'tcx> { pub fn tcx(&self) -> TyCtxt<'a, 'gcx, 'tcx> { @@ -385,7 +380,7 @@ impl<'a, 'gcx, 'tcx> Env<'a, 'gcx, 'tcx> { } pub fn t_rptr_late_bound(&self, id: u32) -> Ty<'tcx> { - let r = self.re_late_bound_with_debruijn(id, d1()); + let r = self.re_late_bound_with_debruijn(id, D1); self.infcx.tcx.mk_imm_ref(r, self.tcx().types.isize) } @@ -559,7 +554,7 @@ fn subst_ty_renumber_bound() { // t_expected = fn(&'a isize) let t_expected = { - let t_ptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, d2()); + let t_ptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, D2); env.t_fn(&[t_ptr_bound2], env.t_nil()) }; @@ -595,7 +590,7 @@ fn subst_ty_renumber_some_bounds() { // // but not that the Debruijn index is different in the different cases. let t_expected = { - let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, d2()); + let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, D2); env.t_pair(t_rptr_bound1, env.t_fn(&[t_rptr_bound2], env.t_nil())) }; @@ -621,11 +616,11 @@ fn escaping() { let t_rptr_free1 = env.t_rptr_free(1); assert!(!t_rptr_free1.has_escaping_bound_vars()); - let t_rptr_bound1 = env.t_rptr_late_bound_with_debruijn(1, d1()); - assert!(t_rptr_bound1.has_escaping_bound_vars()); + let t_rptr_bound1 = env.t_rptr_late_bound_with_debruijn(1, D1); + assert!(t_rptr_bound1.has_escaping_regions()); - let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, d2()); - assert!(t_rptr_bound2.has_escaping_bound_vars()); + let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, D2); + assert!(t_rptr_bound2.has_escaping_regions()); // t_fn = fn(A) let t_param = env.t_param(0); @@ -640,7 +635,7 @@ fn escaping() { #[test] fn subst_region_renumber_region() { test_env(EMPTY_SOURCE_STR, errors(&[]), |env| { - let re_bound1 = env.re_late_bound_with_debruijn(1, d1()); + let re_bound1 = env.re_late_bound_with_debruijn(1, D1); // type t_source<'a> = fn(&'a isize) let t_source = { @@ -655,7 +650,7 @@ fn subst_region_renumber_region() { // // but not that the Debruijn index is different in the different cases. let t_expected = { - let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, d2()); + let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, D2); env.t_fn(&[t_rptr_bound2], env.t_nil()) }; diff --git a/src/librustc_mir/lib.rs b/src/librustc_mir/lib.rs index 1a35f4da20bf1..0019124f1204e 100644 --- a/src/librustc_mir/lib.rs +++ b/src/librustc_mir/lib.rs @@ -38,6 +38,7 @@ Rust MIR: a lowered representation of Rust. Also: an experiment! #![feature(try_from)] #![feature(reverse_bits)] #![feature(underscore_imports)] +#![feature(const_nonzero_methods)] #![recursion_limit="256"] diff --git a/src/librustc_target/lib.rs b/src/librustc_target/lib.rs index e60c9922d467a..59e64c98edc7b 100644 --- a/src/librustc_target/lib.rs +++ b/src/librustc_target/lib.rs @@ -24,6 +24,7 @@ #![feature(box_syntax)] #![feature(nll)] #![feature(slice_patterns)] +#![feature(const_fn)] #[macro_use] extern crate bitflags; diff --git a/src/test/run-pass-fulldeps/newtype_index.rs b/src/test/run-pass-fulldeps/newtype_index.rs index 3cd622a33b173..42e4c5b0533f9 100644 --- a/src/test/run-pass-fulldeps/newtype_index.rs +++ b/src/test/run-pass-fulldeps/newtype_index.rs @@ -1,4 +1,4 @@ -#![feature(rustc_attrs, rustc_private, step_trait)] +#![feature(rustc_attrs, rustc_private, step_trait, const_fn)] #[macro_use] extern crate rustc_data_structures; extern crate rustc_serialize; diff --git a/src/test/ui/consts/const-nonzero.rs b/src/test/ui/consts/const-nonzero.rs new file mode 100644 index 0000000000000..4c14f3b47856b --- /dev/null +++ b/src/test/ui/consts/const-nonzero.rs @@ -0,0 +1,11 @@ +// compile-pass + +#![feature(const_nonzero_methods)] + +use std::num::NonZeroU8; + +const X: NonZeroU8 = unsafe { NonZeroU8::new_unchecked(5) }; +const Y: u8 = X.get(); + +fn main() { +}