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

Make the NonZero* methods const fn #54799

Closed
wants to merge 3 commits 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
3 changes: 2 additions & 1 deletion src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), 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
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/hir/def_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down
21 changes: 5 additions & 16 deletions src/librustc_data_structures/indexed_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -139,7 +128,7 @@ macro_rules! newtype_index {
];

unsafe {
$type { private: value }
$type::from_u32_unchecked(value)
}
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions src/librustc_data_structures/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down
27 changes: 11 additions & 16 deletions src/librustc_driver/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,8 @@ fn test_env_with_pool<F>(
);
}

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> {
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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())
};

Expand Down Expand Up @@ -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()))
};

Expand All @@ -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);
Expand All @@ -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 = {
Expand All @@ -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())
};

Expand Down
1 change: 1 addition & 0 deletions src/librustc_mir/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
1 change: 1 addition & 0 deletions src/librustc_target/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#![feature(box_syntax)]
#![feature(nll)]
#![feature(slice_patterns)]
#![feature(const_fn)]

#[macro_use]
extern crate bitflags;
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass-fulldeps/newtype_index.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/consts/const-nonzero.rs
Original file line number Diff line number Diff line change
@@ -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() {
}