fix(syntax): correctly check for valid RedeclarationIds (#5759)

Previously we truncated `usize` to `u32` and *then* checked validity of the `u32`. Fix that by checking validity *before* truncating.
This commit is contained in:
overlookmotel 2024-09-13 12:15:20 +00:00
parent 8ff013ada1
commit 042afa9fd6

View file

@ -36,7 +36,9 @@ pub struct RedeclarationId(NonMaxU32);
impl Idx for RedeclarationId {
#[allow(clippy::cast_possible_truncation)]
fn from_usize(idx: usize) -> Self {
Self(NonMaxU32::new(idx as u32).unwrap())
assert!(idx < u32::MAX as usize);
// SAFETY: We just checked `idx` is valid for `NonMaxU32`
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
}
fn index(self) -> usize {