Skip to content

Commit

Permalink
refactor(transformer): add more debug assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Sep 26, 2024
1 parent ec70b59 commit 4f48a21
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/oxc_transformer/src/es2015/arrow_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ impl<'a> Traverse<'a> for ArrowFunctions<'a> {
);
}
debug_assert!(self.this_var_stack.len() == 1);
debug_assert!(self.this_var_stack.last().is_none());
}

fn enter_function(&mut self, _func: &mut Function<'a>, _ctx: &mut TraverseCtx<'a>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl<'a> Traverse<'a> for ExponentiationOperator<'a> {
#[inline] // Inline because it's no-op in release mode
fn exit_program(&mut self, _program: &mut Program<'a>, _ctx: &mut TraverseCtx<'a>) {
debug_assert!(self.var_declarations.len() == 1);
debug_assert!(self.var_declarations.last().is_none());
}

fn enter_statements(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl<'a> Traverse<'a> for NullishCoalescingOperator<'a> {
#[inline] // Inline because it's no-op in release mode
fn exit_program(&mut self, _program: &mut Program<'a>, _ctx: &mut TraverseCtx<'a>) {
debug_assert!(self.var_declarations.len() == 1);
debug_assert!(self.var_declarations.last().is_none());
}

fn enter_statements(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ impl<'a> Traverse<'a> for LogicalAssignmentOperators<'a> {
#[inline] // Inline because it's no-op in release mode
fn exit_program(&mut self, _program: &mut Program<'a>, _ctx: &mut TraverseCtx<'a>) {
debug_assert!(self.var_declarations.len() == 1);
debug_assert!(self.var_declarations.last().is_none());
}

fn enter_statements(
Expand Down
18 changes: 18 additions & 0 deletions crates/oxc_transformer/src/helpers/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ impl<T> SparseStack<T> {
}
}

/// Get value of last entry on the stack.
#[inline]
pub fn last(&self) -> Option<&T> {
debug_assert!(!self.has_values.is_empty());
// SAFETY: `self.has_values` starts with 1 entry. Only `pop` removes entries from it,
// and it ensures `self.has_values` always has at least 1 entry.
let has_value = unsafe { *self.has_values.last().unwrap_unchecked() };
if has_value {
debug_assert!(!self.values.is_empty());
// SAFETY: Last `self.has_values` is only `true` if there's a corresponding value in `self.values`.
// This invariant is maintained in `push`, `pop`, `take_last`, `last_or_init`, and `last_mut_or_init`.
let value = unsafe { self.values.last().unwrap_unchecked() };
Some(value)
} else {
None
}
}

/// Take value from last entry on the stack, leaving last entry empty.
#[inline]
pub fn take_last(&mut self) -> Option<T> {
Expand Down

0 comments on commit 4f48a21

Please sign in to comment.