Skip to content

Commit 3805ebe

Browse files
committed
remove use of from_u32_unchecked
1 parent 3b6361d commit 3805ebe

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/librustc/ty/sty.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,9 +1274,7 @@ impl DebruijnIndex {
12741274
/// you would need to shift the index for `'a` into 1 new binder.
12751275
#[must_use]
12761276
pub fn shifted_in(self, amount: u32) -> DebruijnIndex {
1277-
unsafe {
1278-
DebruijnIndex::from_u32_unchecked(self.as_u32() + amount)
1279-
}
1277+
DebruijnIndex::from_u32(self.as_u32() + amount)
12801278
}
12811279

12821280
/// Update this index in place by shifting it "in" through
@@ -1289,9 +1287,7 @@ impl DebruijnIndex {
12891287
/// `amount` number of new binders.
12901288
#[must_use]
12911289
pub fn shifted_out(self, amount: u32) -> DebruijnIndex {
1292-
unsafe {
1293-
DebruijnIndex::from_u32_unchecked(self.as_u32() - amount)
1294-
}
1290+
DebruijnIndex::from_u32(self.as_u32() - amount)
12951291
}
12961292

12971293
/// Update in place by shifting out from `amount` binders.

src/librustc_data_structures/indexed_vec.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ macro_rules! newtype_index {
104104
impl $type {
105105
$v const MAX_AS_U32: u32 = $max;
106106

107-
$v const MAX: $type = unsafe { $type::from_u32_unchecked($max) };
107+
$v const MAX: $type = $type::from_u32_const($max);
108108

109109
#[inline]
110110
$v fn from_usize(value: usize) -> Self {
@@ -122,6 +122,24 @@ macro_rules! newtype_index {
122122
}
123123
}
124124

125+
/// Hacky variant of `from_u32` for use in constants.
126+
/// This version checks the "max" constraint by using an
127+
/// invalid array dereference.
128+
#[inline]
129+
$v const fn from_u32_const(value: u32) -> Self {
130+
// This will fail at const eval time unless `value <=
131+
// max` is true (in which case we get the index 0).
132+
// It will also fail at runtime, of course, but in a
133+
// kind of wacky way.
134+
let _ = ["out of range value used"][
135+
!(value <= $max) as usize
136+
];
137+
138+
unsafe {
139+
$type::from_u32_unchecked(value)
140+
}
141+
}
142+
125143
#[inline]
126144
$v const unsafe fn from_u32_unchecked(value: u32) -> Self {
127145
$type { private: ::std::num::NonZeroU32::new_unchecked(value + 1) }
@@ -424,7 +442,7 @@ macro_rules! newtype_index {
424442
const $name:ident = $constant:expr,
425443
$($tokens:tt)*) => (
426444
$(#[doc = $doc])*
427-
pub const $name: $type = unsafe { $type::from_u32_unchecked($constant) };
445+
pub const $name: $type = $type::from_u32_const($constant);
428446
newtype_index!(
429447
@derives [$($derives,)*]
430448
@type [$type]

0 commit comments

Comments
 (0)