mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(syntax): remove some unsafe code creating IDs (#6324)
This commit is contained in:
parent
3a4bcc77fc
commit
03bc041ddf
4 changed files with 12 additions and 14 deletions
|
|
@ -16,11 +16,10 @@ impl NodeId {
|
||||||
/// # Panics
|
/// # Panics
|
||||||
/// Panics if `idx` is `u32::MAX`.
|
/// Panics if `idx` is `u32::MAX`.
|
||||||
pub const fn new(idx: u32) -> Self {
|
pub const fn new(idx: u32) -> Self {
|
||||||
// We could use `NonMaxU32::new(idx).unwrap()` but `Option::unwrap` is not a const function
|
if let Some(idx) = NonMaxU32::new(idx) {
|
||||||
// and we want this function to be
|
return Self(idx);
|
||||||
assert!(idx != u32::MAX);
|
}
|
||||||
// SAFETY: We have checked that `idx` is not `u32::MAX`
|
panic!();
|
||||||
unsafe { Self::new_unchecked(idx) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create `NodeId` from `u32` unchecked.
|
/// Create `NodeId` from `u32` unchecked.
|
||||||
|
|
@ -38,7 +37,7 @@ impl Idx for NodeId {
|
||||||
#[allow(clippy::cast_possible_truncation)]
|
#[allow(clippy::cast_possible_truncation)]
|
||||||
fn from_usize(idx: usize) -> Self {
|
fn from_usize(idx: usize) -> Self {
|
||||||
assert!(idx < u32::MAX as usize);
|
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) })
|
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ impl Idx for ReferenceId {
|
||||||
#[allow(clippy::cast_possible_truncation)]
|
#[allow(clippy::cast_possible_truncation)]
|
||||||
fn from_usize(idx: usize) -> Self {
|
fn from_usize(idx: usize) -> Self {
|
||||||
assert!(idx < u32::MAX as usize);
|
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) })
|
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,11 +13,10 @@ impl ScopeId {
|
||||||
/// # Panics
|
/// # Panics
|
||||||
/// Panics if `idx` is `u32::MAX`.
|
/// Panics if `idx` is `u32::MAX`.
|
||||||
pub const fn new(idx: u32) -> Self {
|
pub const fn new(idx: u32) -> Self {
|
||||||
// We could use `NonMaxU32::new(idx).unwrap()` but `Option::unwrap` is not a const function
|
if let Some(idx) = NonMaxU32::new(idx) {
|
||||||
// and we want this function to be
|
return Self(idx);
|
||||||
assert!(idx != u32::MAX);
|
}
|
||||||
// SAFETY: We have checked that `idx` is not `u32::MAX`
|
panic!();
|
||||||
unsafe { Self::new_unchecked(idx) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create `ScopeId` from `u32` unchecked.
|
/// Create `ScopeId` from `u32` unchecked.
|
||||||
|
|
@ -35,7 +34,7 @@ impl Idx for ScopeId {
|
||||||
#[allow(clippy::cast_possible_truncation)]
|
#[allow(clippy::cast_possible_truncation)]
|
||||||
fn from_usize(idx: usize) -> Self {
|
fn from_usize(idx: usize) -> Self {
|
||||||
assert!(idx < u32::MAX as usize);
|
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) })
|
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ impl Idx for SymbolId {
|
||||||
#[allow(clippy::cast_possible_truncation)]
|
#[allow(clippy::cast_possible_truncation)]
|
||||||
fn from_usize(idx: usize) -> Self {
|
fn from_usize(idx: usize) -> Self {
|
||||||
assert!(idx < u32::MAX as usize);
|
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) })
|
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue