Skip to content

Comments

feat(data_structures): add is_exhausted method to NonEmptyStack and SparseStack#13672

Merged
graphite-app[bot] merged 1 commit intomainfrom
09-11-feat_data_structures_add_is_exhausted_method_to_nonemptystack_and_sparsestack_
Sep 11, 2025
Merged

feat(data_structures): add is_exhausted method to NonEmptyStack and SparseStack#13672
graphite-app[bot] merged 1 commit intomainfrom
09-11-feat_data_structures_add_is_exhausted_method_to_nonemptystack_and_sparsestack_

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Sep 11, 2025

When using stacks, typically you create a stack at start of AST traversal, and then debug_assert! that the stack has been emptied again at the end of traversal, to make sure that the stack has not got out of sync - that every push is followed by a corresponding pop.

With NonEmptyStack and SparseStack it's a little confusing how to do this, because these stacks can never be empty. stack.is_empty() is not correct. stack.len() == 1 is correct, but it's unclear when reading that code what exactly this is testing.

Add is_exhausted methods to NonEmptyStack and SparseStack to fulfil this role.

Naming: is_exhausted is perhaps not the best name, but I couldn't come up with a better one. I considered is_emptied, which is more descriptive, but I think it's too close to is_empty.

(prompted by #13632)

@github-actions github-actions bot added the C-enhancement Category - New feature or request label Sep 11, 2025
Copy link
Member Author

overlookmotel commented Sep 11, 2025


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds an is_exhausted method to both NonEmptyStack and SparseStack data structures to provide a clear way to check if all pushed items have been popped, returning the stack to its initial state. This is useful for debugging AST traversals where you want to ensure proper stack balance.

Key changes:

  • Added is_exhausted() method to NonEmptyStack that checks if cursor == start
  • Added is_exhausted() method to SparseStack that delegates to the underlying NonEmptyStack
  • Added comprehensive tests for both implementations

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
crates/oxc_data_structures/src/stack/non_empty.rs Adds is_exhausted() method and test for NonEmptyStack
crates/oxc_data_structures/src/stack/sparse.rs Adds is_exhausted() method and test for SparseStack

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@codspeed-hq
Copy link

codspeed-hq bot commented Sep 11, 2025

CodSpeed Instrumentation Performance Report

Merging #13672 will not alter performance

Comparing 09-11-feat_data_structures_add_is_exhausted_method_to_nonemptystack_and_sparsestack_ (1a58e99) with main (8f37e88)1

Summary

✅ 37 untouched benchmarks

Footnotes

  1. No successful run was found on main (1a58e99) during the generation of this report, so 8f37e88 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@overlookmotel overlookmotel marked this pull request as draft September 11, 2025 05:59
@overlookmotel overlookmotel force-pushed the 09-11-feat_data_structures_add_is_exhausted_method_to_nonemptystack_and_sparsestack_ branch from d39f9d7 to a6e1ace Compare September 11, 2025 06:01
@overlookmotel overlookmotel self-assigned this Sep 11, 2025
@overlookmotel overlookmotel marked this pull request as ready for review September 11, 2025 06:02
@overlookmotel overlookmotel added the 0-merge Merge with Graphite Merge Queue label Sep 11, 2025
Copy link
Member Author

overlookmotel commented Sep 11, 2025

Merge activity

…nd `SparseStack` (#13672)

When using stacks, typically you create a stack at start of AST traversal, and then `debug_assert!` that the stack has been emptied again at the end of traversal, to make sure that the stack has not got out of sync - that every `push` is followed by a corresponding `pop`.

With `NonEmptyStack` and `SparseStack` it's a little confusing how to do this, because these stacks can never be empty. `stack.is_empty()` is not correct. `stack.len() == 1` is correct, but it's unclear when reading that code what exactly this is testing.

Add `is_exhausted` methods to `NonEmptyStack` and `SparseStack` to fulfil this role.

Naming: `is_exhausted` is perhaps not the best name, but I couldn't come up with a better one. I considered `is_emptied`, which is more descriptive, but I think it's too close to `is_empty`.

(prompted by #13632)
@graphite-app graphite-app bot force-pushed the 09-11-feat_data_structures_add_is_exhausted_method_to_nonemptystack_and_sparsestack_ branch from a6e1ace to 1a58e99 Compare September 11, 2025 06:06
graphite-app bot pushed a commit that referenced this pull request Sep 11, 2025
…time error (#13673)

Continuation from #13672.

Calling `NonEmptyStack::is_empty` is always a logic error. `NonEmptyStack` can never be empty, and `NonEmptyStack::is_exhausted` is very likely what was intended.

Make calling `NonEmptyStack::is_empty` a compile-time error, to prevent this erroneous usage.

Ideally, we'd remove the method entirely, but `&NonEmptyStack<T>` derefs to `&[T]`, so without defining `is_empty` method on `NonEmptyStack`, `stack.is_empty()` falls through to `[T]::is_empty`, which is not what we want.
@graphite-app graphite-app bot merged commit 1a58e99 into main Sep 11, 2025
26 checks passed
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Sep 11, 2025
@graphite-app graphite-app bot deleted the 09-11-feat_data_structures_add_is_exhausted_method_to_nonemptystack_and_sparsestack_ branch September 11, 2025 06:11
graphite-app bot pushed a commit that referenced this pull request Sep 11, 2025
…k methods (#13674)

Utilize `is_exhausted` methods of `NonEmptyStack` and `SparseStack` (added in #13672) in transformer and `oxc_estree` to clarify what these checks are doing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant