From d8c81734588a62e564720675558c23f2d6dac118 Mon Sep 17 00:00:00 2001 From: JamesHinshelwood Date: Tue, 2 Oct 2018 16:12:25 +0100 Subject: [PATCH 1/3] Add const to nonzero get method --- src/libcore/num/mod.rs | 3 ++- src/librustc/hir/def_id.rs | 2 +- src/librustc/lib.rs | 2 ++ src/librustc/ty/sty.rs | 4 +-- src/librustc_data_structures/indexed_vec.rs | 27 ++++++--------------- src/librustc_data_structures/lib.rs | 1 + src/librustc_driver/test.rs | 27 +++++++++------------ src/librustc_mir/lib.rs | 1 + src/test/ui/consts/const-nonzero.rs | 11 +++++++++ 9 files changed, 39 insertions(+), 39 deletions(-) create mode 100644 src/test/ui/consts/const-nonzero.rs 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/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..89b83825d838d 100644 --- a/src/librustc_data_structures/indexed_vec.rs +++ b/src/librustc_data_structures/indexed_vec.rs @@ -101,13 +101,13 @@ macro_rules! newtype_index { #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, $($derives),*)] #[rustc_layout_scalar_valid_range_end($max)] $v struct $type { - private: u32 + private: ::std::num::NonZeroU32 } 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,13 +128,13 @@ macro_rules! newtype_index { ]; unsafe { - $type { private: value } + $type::from_u32_unchecked(value) } } #[inline] $v const unsafe fn from_u32_unchecked(value: u32) -> Self { - $type { private: value } + $type { private: ::std::num::NonZeroU32::new_unchecked(value + 1) } } /// Extract value of this index as an integer. @@ -156,8 +145,8 @@ macro_rules! newtype_index { /// Extract value of this index as a usize. #[inline] - $v fn as_u32(self) -> u32 { - self.private + $v const fn as_u32(self) -> u32 { + self.private.get() - 1 } /// Extract value of this index as a u32. @@ -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/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() { +} From 5e175e6a5db2fbfbe92719a93605425a07c30de2 Mon Sep 17 00:00:00 2001 From: JamesHinshelwood Date: Sun, 11 Nov 2018 22:15:09 +0000 Subject: [PATCH 2/3] Use normal u32 for newtype_index --- src/librustc/ty/mod.rs | 2 +- src/librustc_data_structures/indexed_vec.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) 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_data_structures/indexed_vec.rs b/src/librustc_data_structures/indexed_vec.rs index 89b83825d838d..83e8ff7e5ffc1 100644 --- a/src/librustc_data_structures/indexed_vec.rs +++ b/src/librustc_data_structures/indexed_vec.rs @@ -101,7 +101,7 @@ macro_rules! newtype_index { #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, $($derives),*)] #[rustc_layout_scalar_valid_range_end($max)] $v struct $type { - private: ::std::num::NonZeroU32 + private: u32 } impl $type { @@ -134,7 +134,7 @@ macro_rules! newtype_index { #[inline] $v const unsafe fn from_u32_unchecked(value: u32) -> Self { - $type { private: ::std::num::NonZeroU32::new_unchecked(value + 1) } + $type { private: value } } /// Extract value of this index as an integer. @@ -146,7 +146,7 @@ macro_rules! newtype_index { /// Extract value of this index as a usize. #[inline] $v const fn as_u32(self) -> u32 { - self.private.get() - 1 + self.private } /// Extract value of this index as a u32. From bb402f1d5a638ef36ed54b107ead9697e6f68740 Mon Sep 17 00:00:00 2001 From: JamesHinshelwood Date: Tue, 13 Nov 2018 16:55:52 +0000 Subject: [PATCH 3/3] Enable const_fn in newtype test --- src/librustc_target/lib.rs | 1 + src/test/run-pass-fulldeps/newtype_index.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) 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;