Skip to content

Commit

Permalink
refactor(syntax): remove some unsafe code creating IDs (#6324)
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Oct 6, 2024
1 parent 3a4bcc7 commit 03bc041
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
11 changes: 5 additions & 6 deletions crates/oxc_syntax/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ impl NodeId {
/// # Panics
/// Panics if `idx` is `u32::MAX`.
pub const fn new(idx: u32) -> Self {
// We could use `NonMaxU32::new(idx).unwrap()` but `Option::unwrap` is not a const function
// and we want this function to be
assert!(idx != u32::MAX);
// SAFETY: We have checked that `idx` is not `u32::MAX`
unsafe { Self::new_unchecked(idx) }
if let Some(idx) = NonMaxU32::new(idx) {
return Self(idx);
}
panic!();
}

/// Create `NodeId` from `u32` unchecked.
Expand All @@ -38,7 +37,7 @@ impl Idx for NodeId {
#[allow(clippy::cast_possible_truncation)]
fn from_usize(idx: usize) -> Self {
assert!(idx < u32::MAX as usize);
// SAFETY: We just checked `idx` is valid for `NonMaxU32`
// SAFETY: We just checked `idx` is a legal value for `NonMaxU32`
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_syntax/src/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Idx for ReferenceId {
#[allow(clippy::cast_possible_truncation)]
fn from_usize(idx: usize) -> Self {
assert!(idx < u32::MAX as usize);
// SAFETY: We just checked `idx` is valid for `NonMaxU32`
// SAFETY: We just checked `idx` is a legal value for `NonMaxU32`
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
}

Expand Down
11 changes: 5 additions & 6 deletions crates/oxc_syntax/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ impl ScopeId {
/// # Panics
/// Panics if `idx` is `u32::MAX`.
pub const fn new(idx: u32) -> Self {
// We could use `NonMaxU32::new(idx).unwrap()` but `Option::unwrap` is not a const function
// and we want this function to be
assert!(idx != u32::MAX);
// SAFETY: We have checked that `idx` is not `u32::MAX`
unsafe { Self::new_unchecked(idx) }
if let Some(idx) = NonMaxU32::new(idx) {
return Self(idx);
}
panic!();
}

/// Create `ScopeId` from `u32` unchecked.
Expand All @@ -35,7 +34,7 @@ impl Idx for ScopeId {
#[allow(clippy::cast_possible_truncation)]
fn from_usize(idx: usize) -> Self {
assert!(idx < u32::MAX as usize);
// SAFETY: We just checked `idx` is valid for `NonMaxU32`
// SAFETY: We just checked `idx` is a legal value for `NonMaxU32`
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_syntax/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl Idx for SymbolId {
#[allow(clippy::cast_possible_truncation)]
fn from_usize(idx: usize) -> Self {
assert!(idx < u32::MAX as usize);
// SAFETY: We just checked `idx` is valid for `NonMaxU32`
// SAFETY: We just checked `idx` is a legal value for `NonMaxU32`
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
}

Expand Down

0 comments on commit 03bc041

Please sign in to comment.