Skip to content

Commit f702bd6

Browse files
committed
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.
1 parent c67d518 commit f702bd6

File tree

5 files changed

+35
-13
lines changed

5 files changed

+35
-13
lines changed

src/librustc/dep_graph/graph.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ newtype_index! {
4444
}
4545

4646
impl DepNodeIndex {
47-
const INVALID: DepNodeIndex = unsafe {
48-
DepNodeIndex::from_u32_unchecked(::std::u32::MAX)
49-
};
47+
const INVALID: DepNodeIndex = DepNodeIndex::MAX;
5048
}
5149

5250
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]

src/librustc/hir/def_id.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,20 @@ newtype_index! {
2727
/// Virtual crate for builtin macros
2828
// FIXME(jseyfried): this is also used for custom derives until proc-macro crates get
2929
// `CrateNum`s.
30-
const BUILTIN_MACROS_CRATE = u32::MAX,
30+
const BUILTIN_MACROS_CRATE = CrateNum::MAX_AS_U32,
3131

3232
/// A CrateNum value that indicates that something is wrong.
33-
const INVALID_CRATE = u32::MAX - 1,
33+
const INVALID_CRATE = CrateNum::MAX_AS_U32 - 1,
3434

3535
/// A special CrateNum that we use for the tcx.rcache when decoding from
3636
/// the incr. comp. cache.
37-
const RESERVED_FOR_INCR_COMP_CACHE = u32::MAX - 2,
37+
const RESERVED_FOR_INCR_COMP_CACHE = CrateNum::MAX_AS_U32 - 2,
3838
}
3939
}
4040

4141
impl CrateNum {
4242
pub fn new(x: usize) -> CrateNum {
43-
assert!(x < (u32::MAX as usize));
44-
CrateNum::from_u32(x as u32)
43+
CrateNum::from_usize(x)
4544
}
4645

4746
pub fn as_def_id(&self) -> DefId { DefId { krate: *self, index: CRATE_DEF_INDEX } }

src/librustc_data_structures/indexed_vec.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ macro_rules! newtype_index {
7272
newtype_index!(
7373
// Leave out derives marker so we can use its absence to ensure it comes first
7474
@type [$name]
75-
@max [::std::u32::MAX]
75+
@max [::std::u32::MAX - 1]
7676
@vis [$v]
7777
@debug_format ["{}"]);
7878
);
@@ -82,7 +82,7 @@ macro_rules! newtype_index {
8282
newtype_index!(
8383
// Leave out derives marker so we can use its absence to ensure it comes first
8484
@type [$name]
85-
@max [::std::u32::MAX]
85+
@max [::std::u32::MAX - 1]
8686
@vis [$v]
8787
@debug_format ["{}"]
8888
$($tokens)+);
@@ -102,17 +102,21 @@ macro_rules! newtype_index {
102102
}
103103

104104
impl $type {
105+
$v const MAX_AS_U32: u32 = $max;
106+
107+
$v const MAX: $type = unsafe { $type::from_u32_unchecked($max) };
108+
105109
#[inline]
106110
$v fn from_usize(value: usize) -> Self {
107-
assert!(value < ($max as usize));
111+
assert!(value <= ($max as usize));
108112
unsafe {
109113
$type::from_u32_unchecked(value as u32)
110114
}
111115
}
112116

113117
#[inline]
114118
$v fn from_u32(value: u32) -> Self {
115-
assert!(value < $max);
119+
assert!(value <= $max);
116120
unsafe {
117121
$type::from_u32_unchecked(value)
118122
}
@@ -138,7 +142,7 @@ macro_rules! newtype_index {
138142
/// Extract value of this index as a u32.
139143
#[inline]
140144
$v const fn as_usize(self) -> usize {
141-
self.private as usize
145+
self.as_u32() as usize
142146
}
143147
}
144148

src/librustc_data_structures/stable_hasher.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,14 @@ impl_stable_hash_via_hash!(i128);
217217
impl_stable_hash_via_hash!(char);
218218
impl_stable_hash_via_hash!(());
219219

220+
impl<CTX> HashStable<CTX> for ::std::num::NonZeroU32 {
221+
fn hash_stable<W: StableHasherResult>(&self,
222+
ctx: &mut CTX,
223+
hasher: &mut StableHasher<W>) {
224+
self.get().hash_stable(ctx, hasher)
225+
}
226+
}
227+
220228
impl<CTX> HashStable<CTX> for f32 {
221229
fn hash_stable<W: StableHasherResult>(&self,
222230
ctx: &mut CTX,

src/libserialize/serialize.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,18 @@ impl Decodable for u32 {
361361
}
362362
}
363363

364+
impl Encodable for ::std::num::NonZeroU32 {
365+
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
366+
s.emit_u32(self.get())
367+
}
368+
}
369+
370+
impl Decodable for ::std::num::NonZeroU32 {
371+
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
372+
d.read_u32().map(|d| ::std::num::NonZeroU32::new(d).unwrap())
373+
}
374+
}
375+
364376
impl Encodable for u64 {
365377
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
366378
s.emit_u64(*self)
@@ -895,3 +907,4 @@ impl<T: UseSpecializedDecodable> Decodable for T {
895907
impl<'a, T: ?Sized + Encodable> UseSpecializedEncodable for &'a T {}
896908
impl<T: ?Sized + Encodable> UseSpecializedEncodable for Box<T> {}
897909
impl<T: Decodable> UseSpecializedDecodable for Box<T> {}
910+

0 commit comments

Comments
 (0)