Skip to content
Closed
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
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ unicode-id-start = "1"
javascript-globals = "1"
json-strip-comments = "3"
oxc-browserslist = "2"
oxc_index = "3"
oxc_index = { git = "https://github.com/oxc-project/oxc-index-vec.git", branch = "reduce-bounds-checks" }
oxc_resolver = "11"
oxc_sourcemap = "4"

Expand Down
8 changes: 8 additions & 0 deletions crates/oxc_cfg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@ pub type BlockNodeId = petgraph::stable_graph::NodeIndex;
pub struct BasicBlockId(NonMaxU32);

impl Idx for BasicBlockId {
const MAX: usize = (u32::MAX - 1) as usize;

#[expect(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`
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
}

#[expect(clippy::cast_possible_truncation)]
unsafe fn from_usize_unchecked(idx: usize) -> Self {
// SAFETY: Caller must ensure `idx` is a legal value for `NonMaxU32`
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
}

fn index(self) -> usize {
self.0.get() as usize
}
Expand Down
8 changes: 8 additions & 0 deletions crates/oxc_codegen/src/sourcemap_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@ const LINE_SEARCH_LINEAR_ITERATIONS: usize = 16;
pub struct ColumnOffsetsId(NonMaxU32);

impl Idx for ColumnOffsetsId {
const MAX: usize = (u32::MAX - 1) as usize;

#[expect(clippy::cast_possible_truncation)]
fn from_usize(idx: usize) -> Self {
assert!(idx < u32::MAX as usize);
// SAFETY: We just checked `idx` is a legal value for `NonMaxU32`
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
}

#[expect(clippy::cast_possible_truncation)]
unsafe fn from_usize_unchecked(idx: usize) -> Self {
// SAFETY: Caller must ensure `idx` is a legal value for `NonMaxU32`
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
}

fn index(self) -> usize {
self.0.get() as usize
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,21 @@ enum BackRefInfoTarget<'a> {
pub struct RegexNodeId(NonMaxU32);

impl Idx for RegexNodeId {
const MAX: usize = (u32::MAX - 1) as usize;

#[expect(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`
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
}

#[expect(clippy::cast_possible_truncation)]
unsafe fn from_usize_unchecked(idx: usize) -> Self {
// SAFETY: Caller must ensure `idx` is a legal value for `NonMaxU32`
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
}

fn index(self) -> usize {
self.0.get() as usize
}
Expand Down
6 changes: 6 additions & 0 deletions crates/oxc_syntax/src/comment_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ impl CommentNodeId {
}

impl Idx for CommentNodeId {
const MAX: usize = u32::MAX as usize;

fn from_usize(idx: usize) -> Self {
Self(u32::try_from(idx).expect("`idx` is greater than `u32::MAX`"))
}

unsafe fn from_usize_unchecked(idx: usize) -> Self {
Self(idx as u32)
}

fn index(self) -> usize {
self.0 as usize
}
Expand Down
8 changes: 8 additions & 0 deletions crates/oxc_syntax/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,21 @@ impl NodeId {
}

impl Idx for NodeId {
const MAX: usize = (u32::MAX - 1) as usize;

#[expect(clippy::cast_possible_truncation)]
fn from_usize(idx: usize) -> Self {
assert!(idx < u32::MAX as usize);
// SAFETY: We just checked `idx` is a legal value for `NonMaxU32`
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
}

#[expect(clippy::cast_possible_truncation)]
unsafe fn from_usize_unchecked(idx: usize) -> Self {
// SAFETY: Caller must ensure `idx` is a legal value for `NonMaxU32`
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
}

fn index(self) -> usize {
self.0.get() as usize
}
Expand Down
8 changes: 8 additions & 0 deletions crates/oxc_syntax/src/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ use oxc_ast_macros::ast;
pub struct ReferenceId(NonMaxU32);

impl Idx for ReferenceId {
const MAX: usize = (u32::MAX - 1) as usize;

#[expect(clippy::cast_possible_truncation)]
fn from_usize(idx: usize) -> Self {
assert!(idx < u32::MAX as usize);
// SAFETY: We just checked `idx` is a legal value for `NonMaxU32`
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
}

#[expect(clippy::cast_possible_truncation)]
unsafe fn from_usize_unchecked(idx: usize) -> Self {
// SAFETY: Caller must ensure `idx` is a legal value for `NonMaxU32`
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
}

fn index(self) -> usize {
self.0.get() as usize
}
Expand Down
8 changes: 8 additions & 0 deletions crates/oxc_syntax/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,21 @@ impl ScopeId {
}

impl Idx for ScopeId {
const MAX: usize = (u32::MAX - 1) as usize;

#[expect(clippy::cast_possible_truncation)]
fn from_usize(idx: usize) -> Self {
assert!(idx < u32::MAX as usize);
// SAFETY: We just checked `idx` is a legal value for `NonMaxU32`
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
}

#[expect(clippy::cast_possible_truncation)]
unsafe fn from_usize_unchecked(idx: usize) -> Self {
// SAFETY: Caller must ensure `idx` is a legal value for `NonMaxU32`
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
}

fn index(self) -> usize {
self.0.get() as usize
}
Expand Down
16 changes: 16 additions & 0 deletions crates/oxc_syntax/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,21 @@ impl SymbolId {
}

impl Idx for SymbolId {
const MAX: usize = (u32::MAX - 1) as usize;

#[expect(clippy::cast_possible_truncation)]
fn from_usize(idx: usize) -> Self {
assert!(idx < u32::MAX as usize);
// SAFETY: We just checked `idx` is a legal value for `NonMaxU32`
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
}

#[expect(clippy::cast_possible_truncation)]
unsafe fn from_usize_unchecked(idx: usize) -> Self {
// SAFETY: Caller must ensure `idx` is a legal value for `NonMaxU32`
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
}

fn index(self) -> usize {
self.0.get() as usize
}
Expand Down Expand Up @@ -77,13 +85,21 @@ impl Serialize for SymbolId {
pub struct RedeclarationId(NonMaxU32);

impl Idx for RedeclarationId {
const MAX: usize = (u32::MAX - 1) as usize;

#[expect(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`
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
}

#[expect(clippy::cast_possible_truncation)]
unsafe fn from_usize_unchecked(idx: usize) -> Self {
// SAFETY: Caller must ensure `idx` is a legal value for `NonMaxU32`
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
}

fn index(self) -> usize {
self.0.get() as usize
}
Expand Down
Loading