diff --git a/naga/src/arena/handle.rs b/naga/src/arena/handle.rs index 5c67383c1..5b9c2441e 100644 --- a/naga/src/arena/handle.rs +++ b/naga/src/arena/handle.rs @@ -109,11 +109,6 @@ impl Handle { Handle::new(handle_index) } - /// Convert a `usize` index into a `Handle`, without range checks. - pub(super) const unsafe fn from_usize_unchecked(index: usize) -> Self { - Handle::new(Index::new_unchecked(index as u32)) - } - /// Write this handle's index to `formatter`, preceded by `prefix`. pub fn write_prefixed( &self, diff --git a/naga/src/arena/mod.rs b/naga/src/arena/mod.rs index 687fcf905..5ae388bb9 100644 --- a/naga/src/arena/mod.rs +++ b/naga/src/arena/mod.rs @@ -98,7 +98,7 @@ impl Arena { self.data .iter() .enumerate() - .map(|(i, v)| unsafe { (Handle::from_usize_unchecked(i), v) }) + .map(|(i, v)| (Handle::from_usize(i), v)) } /// Returns an iterator over the items stored in this arena, returning both @@ -110,7 +110,7 @@ impl Arena { .iter_mut() .zip(self.span_info.iter()) .enumerate() - .map(|(i, (v, span))| unsafe { (Handle::from_usize_unchecked(i), v, span) }) + .map(|(i, (v, span))| (Handle::from_usize(i), v, span)) } /// Drains the arena, returning an iterator over the items stored. @@ -121,7 +121,7 @@ impl Arena { .into_iter() .zip(arena.span_info) .enumerate() - .map(|(i, (v, span))| unsafe { (Handle::from_usize_unchecked(i), v, span) }) + .map(|(i, (v, span))| (Handle::from_usize(i), v, span)) } /// Returns a iterator over the items stored in this arena, @@ -130,7 +130,7 @@ impl Arena { self.data .iter_mut() .enumerate() - .map(|(i, v)| unsafe { (Handle::from_usize_unchecked(i), v) }) + .map(|(i, v)| (Handle::from_usize(i), v)) } /// Adds a new value to the arena, returning a typed handle. @@ -146,7 +146,7 @@ impl Arena { self.data .iter() .position(fun) - .map(|index| unsafe { Handle::from_usize_unchecked(index) }) + .map(|index| Handle::from_usize(index)) } /// Adds a value with a custom check for uniqueness: @@ -160,7 +160,7 @@ impl Arena { fun: F, ) -> Handle { if let Some(index) = self.data.iter().position(|d| fun(d, &value)) { - unsafe { Handle::from_usize_unchecked(index) } + Handle::from_usize(index) } else { self.append(value, span) } diff --git a/naga/src/arena/unique_arena.rs b/naga/src/arena/unique_arena.rs index 5b9f52976..308089230 100644 --- a/naga/src/arena/unique_arena.rs +++ b/naga/src/arena/unique_arena.rs @@ -108,7 +108,7 @@ impl UniqueArena { /// the item's handle and a reference to it. pub fn iter(&self) -> impl DoubleEndedIterator, &T)> + ExactSizeIterator { self.set.iter().enumerate().map(|(i, v)| { - let index = unsafe { Index::new_unchecked(i as u32) }; + let index = Index::new(i as u32).unwrap(); (Handle::new(index), v) }) } @@ -160,7 +160,7 @@ impl UniqueArena { pub fn get(&self, value: &T) -> Option> { self.set .get_index_of(value) - .map(|index| unsafe { Handle::from_usize_unchecked(index) }) + .map(|index| Handle::from_usize(index)) } /// Return this arena's value at `handle`, if that is a valid handle. diff --git a/naga/src/back/spv/helpers.rs b/naga/src/back/spv/helpers.rs index 3adf1e1f2..6013a4e4c 100644 --- a/naga/src/back/spv/helpers.rs +++ b/naga/src/back/spv/helpers.rs @@ -189,8 +189,7 @@ impl StrUnstable for str { .iter() .rposition(|b| b.is_utf8_char_boundary_polyfill()); - // SAFETY: we know that the character boundary will be within four bytes - unsafe { lower_bound + new_index.unwrap_unchecked() } + lower_bound + new_index.unwrap() } } } diff --git a/naga/src/lib.rs b/naga/src/lib.rs index e0acceec7..1bf72e475 100644 --- a/naga/src/lib.rs +++ b/naga/src/lib.rs @@ -99,6 +99,7 @@ void main() { ) )] #![no_std] +#![forbid(unsafe_code)] #[cfg(std)] extern crate std; diff --git a/naga/src/non_max_u32.rs b/naga/src/non_max_u32.rs index 530ebb450..dea5d0bfd 100644 --- a/naga/src/non_max_u32.rs +++ b/naga/src/non_max_u32.rs @@ -70,24 +70,6 @@ impl NonMaxU32 { self.0.get() - 1 } - /// Construct a [`NonMaxU32`] whose value is `n`. - /// - /// # Safety - /// - /// The value of `n` must not be [`u32::MAX`]. - pub const unsafe fn new_unchecked(n: u32) -> NonMaxU32 { - NonMaxU32(unsafe { NonZeroU32::new_unchecked(n + 1) }) - } - - /// Construct a [`NonMaxU32`] whose value is `index`. - /// - /// # Safety - /// - /// - The value of `index` must be strictly less than [`u32::MAX`]. - pub const unsafe fn from_usize_unchecked(index: usize) -> Self { - NonMaxU32(unsafe { NonZeroU32::new_unchecked(index as u32 + 1) }) - } - pub fn checked_add(self, n: u32) -> Option { // Adding `n` to `self` produces `u32::MAX` if and only if // adding `n` to `self.0` produces `0`. So we can simply diff --git a/naga/src/proc/layouter.rs b/naga/src/proc/layouter.rs index 02c78085b..bff0667dd 100644 --- a/naga/src/proc/layouter.rs +++ b/naga/src/proc/layouter.rs @@ -22,8 +22,7 @@ impl Alignment { pub const fn new(n: u32) -> Option { if n.is_power_of_two() { - // SAFETY: value can't be 0 since we just checked if it's a power of 2 - Some(Self(unsafe { NonZeroU32::new_unchecked(n) })) + Some(Self(NonZeroU32::new(n).unwrap())) } else { None } @@ -71,8 +70,7 @@ impl ops::Mul for Alignment { type Output = Alignment; fn mul(self, rhs: Alignment) -> Self::Output { - // SAFETY: both lhs and rhs are powers of 2, the result will be a power of 2 - Self(unsafe { NonZeroU32::new_unchecked(self.0.get() * rhs.0.get()) }) + Self(NonZeroU32::new(self.0.get() * rhs.0.get()).unwrap()) } } @@ -88,7 +86,7 @@ impl From for Alignment { impl From for Alignment { fn from(size: crate::CooperativeSize) -> Self { - Self(unsafe { NonZeroU32::new_unchecked(size as u32) }) + Self(NonZeroU32::new(size as u32).unwrap()) } }