Skip to content

Commit

Permalink
refactor(traverse): add debug asserts for safety invariants
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Aug 27, 2024
1 parent ea2a267 commit 085ce20
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions crates/oxc_traverse/src/context/ancestry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ impl<'a> TraverseAncestry<'a> {
/// Get parent of current node.
#[inline]
pub fn parent<'t>(&'t self) -> Ancestor<'a, 't> {
debug_assert!(!self.stack.is_empty());
// SAFETY: Stack contains 1 entry initially. Entries are pushed as traverse down the AST,
// and popped as go back up. So even when visiting `Program`, the initial entry is in the stack.
let ancestor = unsafe { *self.stack.last().unwrap_unchecked() };
Expand Down Expand Up @@ -117,6 +118,7 @@ impl<'a> TraverseAncestry<'a> {
/// This method must not be public outside this crate, or consumer could break safety invariants.
#[inline]
pub(crate) unsafe fn pop_stack(&mut self) {
debug_assert!(!self.stack.is_empty());
self.stack.pop().unwrap_unchecked();
}

Expand All @@ -143,6 +145,7 @@ impl<'a> TraverseAncestry<'a> {
#[inline]
#[allow(unsafe_code, clippy::ptr_as_ptr, clippy::ref_as_ptr)]
pub(crate) unsafe fn retag_stack(&mut self, ty: AncestorType) {
debug_assert!(!self.stack.is_empty());
*(self.stack.last_mut().unwrap_unchecked() as *mut _ as *mut AncestorType) = ty;
}
}

0 comments on commit 085ce20

Please sign in to comment.