Skip to content

Commit 3b6361d

Browse files
committed
switch to using NonZeroU32 to represent indices
1 parent f702bd6 commit 3b6361d

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

src/librustc/ty/sty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,7 @@ impl DebruijnIndex {
12731273
///
12741274
/// you would need to shift the index for `'a` into 1 new binder.
12751275
#[must_use]
1276-
pub const fn shifted_in(self, amount: u32) -> DebruijnIndex {
1276+
pub fn shifted_in(self, amount: u32) -> DebruijnIndex {
12771277
unsafe {
12781278
DebruijnIndex::from_u32_unchecked(self.as_u32() + amount)
12791279
}
@@ -1288,7 +1288,7 @@ impl DebruijnIndex {
12881288
/// Returns the resulting index when this value is moved out from
12891289
/// `amount` number of new binders.
12901290
#[must_use]
1291-
pub const fn shifted_out(self, amount: u32) -> DebruijnIndex {
1291+
pub fn shifted_out(self, amount: u32) -> DebruijnIndex {
12921292
unsafe {
12931293
DebruijnIndex::from_u32_unchecked(self.as_u32() - amount)
12941294
}

src/librustc_data_structures/indexed_vec.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ macro_rules! newtype_index {
9898
@debug_format [$debug_format:tt]) => (
9999
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, $($derives),*)]
100100
$v struct $type {
101-
private: u32
101+
private: ::std::num::NonZeroU32
102102
}
103103

104104
impl $type {
@@ -124,7 +124,7 @@ macro_rules! newtype_index {
124124

125125
#[inline]
126126
$v const unsafe fn from_u32_unchecked(value: u32) -> Self {
127-
$type { private: value }
127+
$type { private: ::std::num::NonZeroU32::new_unchecked(value + 1) }
128128
}
129129

130130
/// Extract value of this index as an integer.
@@ -135,13 +135,13 @@ macro_rules! newtype_index {
135135

136136
/// Extract value of this index as a usize.
137137
#[inline]
138-
$v const fn as_u32(self) -> u32 {
139-
self.private
138+
$v fn as_u32(self) -> u32 {
139+
self.private.get() - 1
140140
}
141141

142142
/// Extract value of this index as a u32.
143143
#[inline]
144-
$v const fn as_usize(self) -> usize {
144+
$v fn as_usize(self) -> usize {
145145
self.as_u32() as usize
146146
}
147147
}

src/librustc_driver/test.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,13 @@ fn test_env_with_pool<F>(
183183
});
184184
}
185185

186-
const D1: ty::DebruijnIndex = ty::INNERMOST;
187-
const D2: ty::DebruijnIndex = D1.shifted_in(1);
186+
fn d1() -> ty::DebruijnIndex {
187+
ty::INNERMOST
188+
}
189+
190+
fn d2() -> ty::DebruijnIndex {
191+
d1().shifted_in(1)
192+
}
188193

189194
impl<'a, 'gcx, 'tcx> Env<'a, 'gcx, 'tcx> {
190195
pub fn tcx(&self) -> TyCtxt<'a, 'gcx, 'tcx> {
@@ -337,7 +342,7 @@ impl<'a, 'gcx, 'tcx> Env<'a, 'gcx, 'tcx> {
337342
}
338343

339344
pub fn t_rptr_late_bound(&self, id: u32) -> Ty<'tcx> {
340-
let r = self.re_late_bound_with_debruijn(id, D1);
345+
let r = self.re_late_bound_with_debruijn(id, d1());
341346
self.infcx.tcx.mk_imm_ref(r, self.tcx().types.isize)
342347
}
343348

@@ -494,7 +499,7 @@ fn subst_ty_renumber_bound() {
494499

495500
// t_expected = fn(&'a isize)
496501
let t_expected = {
497-
let t_ptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, D2);
502+
let t_ptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, d2());
498503
env.t_fn(&[t_ptr_bound2], env.t_nil())
499504
};
500505

@@ -531,7 +536,7 @@ fn subst_ty_renumber_some_bounds() {
531536
//
532537
// but not that the Debruijn index is different in the different cases.
533538
let t_expected = {
534-
let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, D2);
539+
let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, d2());
535540
env.t_pair(t_rptr_bound1, env.t_fn(&[t_rptr_bound2], env.t_nil()))
536541
};
537542

@@ -559,10 +564,10 @@ fn escaping() {
559564
let t_rptr_free1 = env.t_rptr_free(1);
560565
assert!(!t_rptr_free1.has_escaping_regions());
561566

562-
let t_rptr_bound1 = env.t_rptr_late_bound_with_debruijn(1, D1);
567+
let t_rptr_bound1 = env.t_rptr_late_bound_with_debruijn(1, d1());
563568
assert!(t_rptr_bound1.has_escaping_regions());
564569

565-
let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, D2);
570+
let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, d2());
566571
assert!(t_rptr_bound2.has_escaping_regions());
567572

568573
// t_fn = fn(A)
@@ -578,7 +583,7 @@ fn escaping() {
578583
#[test]
579584
fn subst_region_renumber_region() {
580585
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
581-
let re_bound1 = env.re_late_bound_with_debruijn(1, D1);
586+
let re_bound1 = env.re_late_bound_with_debruijn(1, d1());
582587

583588
// type t_source<'a> = fn(&'a isize)
584589
let t_source = {
@@ -593,7 +598,7 @@ fn subst_region_renumber_region() {
593598
//
594599
// but not that the Debruijn index is different in the different cases.
595600
let t_expected = {
596-
let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, D2);
601+
let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, d2());
597602
env.t_fn(&[t_rptr_bound2], env.t_nil())
598603
};
599604

0 commit comments

Comments
 (0)