Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(syntax): remove some unsafe code creating IDs #6324

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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