Skip to content

Commit d8c8173

Browse files
Add const to nonzero get method
1 parent 5a2ca1a commit d8c8173

File tree

9 files changed

+39
-39
lines changed

9 files changed

+39
-39
lines changed

src/libcore/num/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", st
8686

8787
/// Returns the value as a primitive type.
8888
#[stable(feature = "nonzero", since = "1.28.0")]
89+
#[rustc_const_unstable(feature = "const_nonzero_methods")]
8990
#[inline]
90-
pub fn get(self) -> $Int {
91+
pub const fn get(self) -> $Int {
9192
self.0 .0
9293
}
9394

src/librustc/hir/def_id.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl ::std::fmt::Debug for CrateNum {
4848

4949
/// Item definitions in the currently-compiled crate would have the CrateNum
5050
/// LOCAL_CRATE in their DefId.
51-
pub const LOCAL_CRATE: CrateNum = CrateNum::Index(CrateId::from_u32_const(0));
51+
pub const LOCAL_CRATE: CrateNum = CrateNum::Index(CrateId::from_u32(0));
5252

5353

5454
impl Idx for CrateNum {

src/librustc/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
#![feature(macro_at_most_once_rep)]
7171
#![feature(crate_visibility_modifier)]
7272
#![feature(transpose_result)]
73+
#![feature(const_fn)]
74+
#![feature(const_nonzero_methods)]
7375

7476
#![recursion_limit="512"]
7577

src/librustc/ty/sty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1325,7 +1325,7 @@ impl DebruijnIndex {
13251325
///
13261326
/// you would need to shift the index for `'a` into 1 new binder.
13271327
#[must_use]
1328-
pub fn shifted_in(self, amount: u32) -> DebruijnIndex {
1328+
pub const fn shifted_in(self, amount: u32) -> DebruijnIndex {
13291329
DebruijnIndex::from_u32(self.as_u32() + amount)
13301330
}
13311331

@@ -1338,7 +1338,7 @@ impl DebruijnIndex {
13381338
/// Returns the resulting index when this value is moved out from
13391339
/// `amount` number of new binders.
13401340
#[must_use]
1341-
pub fn shifted_out(self, amount: u32) -> DebruijnIndex {
1341+
pub const fn shifted_out(self, amount: u32) -> DebruijnIndex {
13421342
DebruijnIndex::from_u32(self.as_u32() - amount)
13431343
}
13441344

src/librustc_data_structures/indexed_vec.rs

+8-19
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ macro_rules! newtype_index {
101101
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, $($derives),*)]
102102
#[rustc_layout_scalar_valid_range_end($max)]
103103
$v struct $type {
104-
private: u32
104+
private: ::std::num::NonZeroU32
105105
}
106106

107107
impl $type {
108108
$v const MAX_AS_U32: u32 = $max;
109109

110-
$v const MAX: $type = $type::from_u32_const($max);
110+
$v const MAX: $type = $type::from_u32($max);
111111

112112
#[inline]
113113
$v fn from_usize(value: usize) -> Self {
@@ -118,18 +118,7 @@ macro_rules! newtype_index {
118118
}
119119

120120
#[inline]
121-
$v fn from_u32(value: u32) -> Self {
122-
assert!(value <= $max);
123-
unsafe {
124-
$type::from_u32_unchecked(value)
125-
}
126-
}
127-
128-
/// Hacky variant of `from_u32` for use in constants.
129-
/// This version checks the "max" constraint by using an
130-
/// invalid array dereference.
131-
#[inline]
132-
$v const fn from_u32_const(value: u32) -> Self {
121+
$v const fn from_u32(value: u32) -> Self {
133122
// This will fail at const eval time unless `value <=
134123
// max` is true (in which case we get the index 0).
135124
// It will also fail at runtime, of course, but in a
@@ -139,13 +128,13 @@ macro_rules! newtype_index {
139128
];
140129

141130
unsafe {
142-
$type { private: value }
131+
$type::from_u32_unchecked(value)
143132
}
144133
}
145134

146135
#[inline]
147136
$v const unsafe fn from_u32_unchecked(value: u32) -> Self {
148-
$type { private: value }
137+
$type { private: ::std::num::NonZeroU32::new_unchecked(value + 1) }
149138
}
150139

151140
/// Extract value of this index as an integer.
@@ -156,8 +145,8 @@ macro_rules! newtype_index {
156145

157146
/// Extract value of this index as a usize.
158147
#[inline]
159-
$v fn as_u32(self) -> u32 {
160-
self.private
148+
$v const fn as_u32(self) -> u32 {
149+
self.private.get() - 1
161150
}
162151

163152
/// Extract value of this index as a u32.
@@ -445,7 +434,7 @@ macro_rules! newtype_index {
445434
const $name:ident = $constant:expr,
446435
$($tokens:tt)*) => (
447436
$(#[doc = $doc])*
448-
pub const $name: $type = $type::from_u32_const($constant);
437+
pub const $name: $type = $type::from_u32($constant);
449438
newtype_index!(
450439
@derives [$($derives,)*]
451440
@type [$type]

src/librustc_data_structures/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#![feature(nll)]
3030
#![feature(allow_internal_unstable)]
3131
#![feature(vec_resize_with)]
32+
#![feature(const_nonzero_methods)]
3233

3334
#![cfg_attr(unix, feature(libc))]
3435
#![cfg_attr(test, feature(test))]

src/librustc_driver/test.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,8 @@ fn test_env_with_pool<F>(
199199
);
200200
}
201201

202-
fn d1() -> ty::DebruijnIndex {
203-
ty::INNERMOST
204-
}
205-
206-
fn d2() -> ty::DebruijnIndex {
207-
d1().shifted_in(1)
208-
}
202+
const D1: ty::DebruijnIndex = ty::INNERMOST;
203+
const D2: ty::DebruijnIndex = D1.shifted_in(1);
209204

210205
impl<'a, 'gcx, 'tcx> Env<'a, 'gcx, 'tcx> {
211206
pub fn tcx(&self) -> TyCtxt<'a, 'gcx, 'tcx> {
@@ -385,7 +380,7 @@ impl<'a, 'gcx, 'tcx> Env<'a, 'gcx, 'tcx> {
385380
}
386381

387382
pub fn t_rptr_late_bound(&self, id: u32) -> Ty<'tcx> {
388-
let r = self.re_late_bound_with_debruijn(id, d1());
383+
let r = self.re_late_bound_with_debruijn(id, D1);
389384
self.infcx.tcx.mk_imm_ref(r, self.tcx().types.isize)
390385
}
391386

@@ -559,7 +554,7 @@ fn subst_ty_renumber_bound() {
559554

560555
// t_expected = fn(&'a isize)
561556
let t_expected = {
562-
let t_ptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, d2());
557+
let t_ptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, D2);
563558
env.t_fn(&[t_ptr_bound2], env.t_nil())
564559
};
565560

@@ -595,7 +590,7 @@ fn subst_ty_renumber_some_bounds() {
595590
//
596591
// but not that the Debruijn index is different in the different cases.
597592
let t_expected = {
598-
let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, d2());
593+
let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, D2);
599594
env.t_pair(t_rptr_bound1, env.t_fn(&[t_rptr_bound2], env.t_nil()))
600595
};
601596

@@ -621,11 +616,11 @@ fn escaping() {
621616
let t_rptr_free1 = env.t_rptr_free(1);
622617
assert!(!t_rptr_free1.has_escaping_bound_vars());
623618

624-
let t_rptr_bound1 = env.t_rptr_late_bound_with_debruijn(1, d1());
625-
assert!(t_rptr_bound1.has_escaping_bound_vars());
619+
let t_rptr_bound1 = env.t_rptr_late_bound_with_debruijn(1, D1);
620+
assert!(t_rptr_bound1.has_escaping_regions());
626621

627-
let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, d2());
628-
assert!(t_rptr_bound2.has_escaping_bound_vars());
622+
let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, D2);
623+
assert!(t_rptr_bound2.has_escaping_regions());
629624

630625
// t_fn = fn(A)
631626
let t_param = env.t_param(0);
@@ -640,7 +635,7 @@ fn escaping() {
640635
#[test]
641636
fn subst_region_renumber_region() {
642637
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
643-
let re_bound1 = env.re_late_bound_with_debruijn(1, d1());
638+
let re_bound1 = env.re_late_bound_with_debruijn(1, D1);
644639

645640
// type t_source<'a> = fn(&'a isize)
646641
let t_source = {
@@ -655,7 +650,7 @@ fn subst_region_renumber_region() {
655650
//
656651
// but not that the Debruijn index is different in the different cases.
657652
let t_expected = {
658-
let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, d2());
653+
let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, D2);
659654
env.t_fn(&[t_rptr_bound2], env.t_nil())
660655
};
661656

src/librustc_mir/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Rust MIR: a lowered representation of Rust. Also: an experiment!
3838
#![feature(try_from)]
3939
#![feature(reverse_bits)]
4040
#![feature(underscore_imports)]
41+
#![feature(const_nonzero_methods)]
4142

4243
#![recursion_limit="256"]
4344

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

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// compile-pass
2+
3+
#![feature(const_nonzero_methods)]
4+
5+
use std::num::NonZeroU8;
6+
7+
const X: NonZeroU8 = unsafe { NonZeroU8::new_unchecked(5) };
8+
const Y: u8 = X.get();
9+
10+
fn main() {
11+
}

0 commit comments

Comments
 (0)