about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNiko Matsakis <niko@alum.mit.edu>2018-08-23 07:46:53 -0400
committerNiko Matsakis <niko@alum.mit.edu>2018-09-07 11:37:47 -0400
commitf702bd6a529ce9d2e311ea0254a04228a4a2f129 (patch)
tree0bb022359f5fd9b0ec5d81fe0fa4f5a5a97b011a
parentc67d518b0d92785323f2c9c94dda25273821b47b (diff)
downloadrust-f702bd6a529ce9d2e311ea0254a04228a4a2f129.tar.gz
rust-f702bd6a529ce9d2e311ea0254a04228a4a2f129.zip
rewrite constants to use NewType::MAX instead of u32::MAX
Also, adjust the MAX to be `u32::MAX - 1`, leaving room for `u32::MAX`
to become a sentinel value in the future.
-rw-r--r--src/librustc/dep_graph/graph.rs4
-rw-r--r--src/librustc/hir/def_id.rs9
-rw-r--r--src/librustc_data_structures/indexed_vec.rs14
-rw-r--r--src/librustc_data_structures/stable_hasher.rs8
-rw-r--r--src/libserialize/serialize.rs13
5 files changed, 35 insertions, 13 deletions
diff --git a/src/librustc/dep_graph/graph.rs b/src/librustc/dep_graph/graph.rs
index c4cfb502923..f5a46060759 100644
--- a/src/librustc/dep_graph/graph.rs
+++ b/src/librustc/dep_graph/graph.rs
@@ -44,9 +44,7 @@ newtype_index! {
 }
 
 impl DepNodeIndex {
-    const INVALID: DepNodeIndex = unsafe {
-        DepNodeIndex::from_u32_unchecked(::std::u32::MAX)
-    };
+    const INVALID: DepNodeIndex = DepNodeIndex::MAX;
 }
 
 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
diff --git a/src/librustc/hir/def_id.rs b/src/librustc/hir/def_id.rs
index ae5da1bfafd..420ffbcfee6 100644
--- a/src/librustc/hir/def_id.rs
+++ b/src/librustc/hir/def_id.rs
@@ -27,21 +27,20 @@ newtype_index! {
         /// Virtual crate for builtin macros
         // FIXME(jseyfried): this is also used for custom derives until proc-macro crates get
         // `CrateNum`s.
-        const BUILTIN_MACROS_CRATE = u32::MAX,
+        const BUILTIN_MACROS_CRATE = CrateNum::MAX_AS_U32,
 
         /// A CrateNum value that indicates that something is wrong.
-        const INVALID_CRATE = u32::MAX - 1,
+        const INVALID_CRATE = CrateNum::MAX_AS_U32 - 1,
 
         /// A special CrateNum that we use for the tcx.rcache when decoding from
         /// the incr. comp. cache.
-        const RESERVED_FOR_INCR_COMP_CACHE = u32::MAX - 2,
+        const RESERVED_FOR_INCR_COMP_CACHE = CrateNum::MAX_AS_U32 - 2,
     }
 }
 
 impl CrateNum {
     pub fn new(x: usize) -> CrateNum {
-        assert!(x < (u32::MAX as usize));
-        CrateNum::from_u32(x as u32)
+        CrateNum::from_usize(x)
     }
 
     pub fn as_def_id(&self) -> DefId { DefId { krate: *self, index: CRATE_DEF_INDEX } }
diff --git a/src/librustc_data_structures/indexed_vec.rs b/src/librustc_data_structures/indexed_vec.rs
index 571d00118a9..f7d0d7b8a36 100644
--- a/src/librustc_data_structures/indexed_vec.rs
+++ b/src/librustc_data_structures/indexed_vec.rs
@@ -72,7 +72,7 @@ macro_rules! newtype_index {
         newtype_index!(
             // Leave out derives marker so we can use its absence to ensure it comes first
             @type         [$name]
-            @max          [::std::u32::MAX]
+            @max          [::std::u32::MAX - 1]
             @vis          [$v]
             @debug_format ["{}"]);
     );
@@ -82,7 +82,7 @@ macro_rules! newtype_index {
         newtype_index!(
             // Leave out derives marker so we can use its absence to ensure it comes first
             @type         [$name]
-            @max          [::std::u32::MAX]
+            @max          [::std::u32::MAX - 1]
             @vis          [$v]
             @debug_format ["{}"]
                           $($tokens)+);
@@ -102,9 +102,13 @@ macro_rules! newtype_index {
         }
 
         impl $type {
+            $v const MAX_AS_U32: u32 = $max;
+
+            $v const MAX: $type = unsafe { $type::from_u32_unchecked($max) };
+
             #[inline]
             $v fn from_usize(value: usize) -> Self {
-                assert!(value < ($max as usize));
+                assert!(value <= ($max as usize));
                 unsafe {
                     $type::from_u32_unchecked(value as u32)
                 }
@@ -112,7 +116,7 @@ macro_rules! newtype_index {
 
             #[inline]
             $v fn from_u32(value: u32) -> Self {
-                assert!(value < $max);
+                assert!(value <= $max);
                 unsafe {
                     $type::from_u32_unchecked(value)
                 }
@@ -138,7 +142,7 @@ macro_rules! newtype_index {
             /// Extract value of this index as a u32.
             #[inline]
             $v const fn as_usize(self) -> usize {
-                self.private as usize
+                self.as_u32() as usize
             }
         }
 
diff --git a/src/librustc_data_structures/stable_hasher.rs b/src/librustc_data_structures/stable_hasher.rs
index c70a0abe8c7..215c44dec69 100644
--- a/src/librustc_data_structures/stable_hasher.rs
+++ b/src/librustc_data_structures/stable_hasher.rs
@@ -217,6 +217,14 @@ impl_stable_hash_via_hash!(i128);
 impl_stable_hash_via_hash!(char);
 impl_stable_hash_via_hash!(());
 
+impl<CTX> HashStable<CTX> for ::std::num::NonZeroU32 {
+    fn hash_stable<W: StableHasherResult>(&self,
+                                          ctx: &mut CTX,
+                                          hasher: &mut StableHasher<W>) {
+        self.get().hash_stable(ctx, hasher)
+    }
+}
+
 impl<CTX> HashStable<CTX> for f32 {
     fn hash_stable<W: StableHasherResult>(&self,
                                           ctx: &mut CTX,
diff --git a/src/libserialize/serialize.rs b/src/libserialize/serialize.rs
index 60bb5a0fec2..416be50bfe9 100644
--- a/src/libserialize/serialize.rs
+++ b/src/libserialize/serialize.rs
@@ -361,6 +361,18 @@ impl Decodable for u32 {
     }
 }
 
+impl Encodable for ::std::num::NonZeroU32 {
+    fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
+        s.emit_u32(self.get())
+    }
+}
+
+impl Decodable for ::std::num::NonZeroU32 {
+    fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
+        d.read_u32().map(|d| ::std::num::NonZeroU32::new(d).unwrap())
+    }
+}
+
 impl Encodable for u64 {
     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
         s.emit_u64(*self)
@@ -895,3 +907,4 @@ impl<T: UseSpecializedDecodable> Decodable for T {
 impl<'a, T: ?Sized + Encodable> UseSpecializedEncodable for &'a T {}
 impl<T: ?Sized + Encodable> UseSpecializedEncodable for Box<T> {}
 impl<T: Decodable> UseSpecializedDecodable for Box<T> {}
+