]> git.lizzy.rs Git - rust.git/commitdiff
switch to using `NonZeroU32` to represent indices
authorNiko Matsakis <niko@alum.mit.edu>
Thu, 30 Aug 2018 17:58:42 +0000 (13:58 -0400)
committerNiko Matsakis <niko@alum.mit.edu>
Fri, 7 Sep 2018 15:37:47 +0000 (11:37 -0400)
src/librustc/ty/sty.rs
src/librustc_data_structures/indexed_vec.rs
src/librustc_driver/test.rs

index fc5faf235764b78e88d5367689e7622d8192a106..acd35e1aa958d0686a59d61504011826a0071dc4 100644 (file)
@@ -1273,7 +1273,7 @@ impl DebruijnIndex {
     ///
     /// you would need to shift the index for `'a` into 1 new binder.
     #[must_use]
-    pub const fn shifted_in(self, amount: u32) -> DebruijnIndex {
+    pub fn shifted_in(self, amount: u32) -> DebruijnIndex {
         unsafe {
             DebruijnIndex::from_u32_unchecked(self.as_u32() + amount)
         }
@@ -1288,7 +1288,7 @@ pub fn shift_in(&mut self, amount: u32) {
     /// Returns the resulting index when this value is moved out from
     /// `amount` number of new binders.
     #[must_use]
-    pub const fn shifted_out(self, amount: u32) -> DebruijnIndex {
+    pub fn shifted_out(self, amount: u32) -> DebruijnIndex {
         unsafe {
             DebruijnIndex::from_u32_unchecked(self.as_u32() - amount)
         }
index f7d0d7b8a36ba6a28b7e469b3191742d16cbbfb5..f068fc77d322d2e33e33db791e2b342ba724a30c 100644 (file)
@@ -98,7 +98,7 @@ macro_rules! newtype_index {
      @debug_format [$debug_format:tt]) => (
         #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, $($derives),*)]
         $v struct $type {
-            private: u32
+            private: ::std::num::NonZeroU32
         }
 
         impl $type {
@@ -124,7 +124,7 @@ impl $type {
 
             #[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.
@@ -135,13 +135,13 @@ impl $type {
 
             /// Extract value of this index as a usize.
             #[inline]
-            $v const fn as_u32(self) -> u32 {
-                self.private
+            $v fn as_u32(self) -> u32 {
+                self.private.get() - 1
             }
 
             /// Extract value of this index as a u32.
             #[inline]
-            $v const fn as_usize(self) -> usize {
+            $v fn as_usize(self) -> usize {
                 self.as_u32() as usize
             }
         }
index 175422975e006cb43e14adffee121df70e8eaf51..57c00f252ef16f0caa647db89595d5453f776283 100644 (file)
@@ -183,8 +183,13 @@ fn test_env_with_pool<F>(
     });
 }
 
-const D1: ty::DebruijnIndex = ty::INNERMOST;
-const D2: ty::DebruijnIndex = D1.shifted_in(1);
+fn d1() -> ty::DebruijnIndex {
+    ty::INNERMOST
+}
+
+fn d2() -> ty::DebruijnIndex {
+    d1().shifted_in(1)
+}
 
 impl<'a, 'gcx, 'tcx> Env<'a, 'gcx, 'tcx> {
     pub fn tcx(&self) -> TyCtxt<'a, 'gcx, 'tcx> {
@@ -337,7 +342,7 @@ pub fn t_rptr(&self, r: ty::Region<'tcx>) -> Ty<'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)
     }
 
@@ -494,7 +499,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())
         };
 
@@ -531,7 +536,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()))
         };
 
@@ -559,10 +564,10 @@ fn escaping() {
         let t_rptr_free1 = env.t_rptr_free(1);
         assert!(!t_rptr_free1.has_escaping_regions());
 
-        let t_rptr_bound1 = env.t_rptr_late_bound_with_debruijn(1, D1);
+        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);
+        let t_rptr_bound2 = env.t_rptr_late_bound_with_debruijn(1, d2());
         assert!(t_rptr_bound2.has_escaping_regions());
 
         // t_fn = fn(A)
@@ -578,7 +583,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 = {
@@ -593,7 +598,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())
         };