diff --git a/src/librustc/dep_graph/graph.rs b/src/librustc/dep_graph/graph.rs index c4cfb5029233..f5a46060759d 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 ae5da1bfafda..420ffbcfee6c 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 571d00118a97..f7d0d7b8a36b 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 c70a0abe8c7e..215c44dec691 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 HashStable for ::std::num::NonZeroU32 { + fn hash_stable(&self, + ctx: &mut CTX, + hasher: &mut StableHasher) { + self.get().hash_stable(ctx, hasher) + } +} + impl HashStable for f32 { fn hash_stable(&self, ctx: &mut CTX, diff --git a/src/libserialize/serialize.rs b/src/libserialize/serialize.rs index 60bb5a0fec2c..416be50bfe9e 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(&self, s: &mut S) -> Result<(), S::Error> { + s.emit_u32(self.get()) + } +} + +impl Decodable for ::std::num::NonZeroU32 { + fn decode(d: &mut D) -> Result { + d.read_u32().map(|d| ::std::num::NonZeroU32::new(d).unwrap()) + } +} + impl Encodable for u64 { fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_u64(*self) @@ -895,3 +907,4 @@ impl Decodable for T { impl<'a, T: ?Sized + Encodable> UseSpecializedEncodable for &'a T {} impl UseSpecializedEncodable for Box {} impl UseSpecializedDecodable for Box {} +