Skip to content

fix(query): prevent cross-branch capture contamination in alternations with quantifiers#5317

Merged
WillLillis merged 1 commit intotree-sitter:masterfrom
WillLillis:query_dbg
Feb 10, 2026
Merged

fix(query): prevent cross-branch capture contamination in alternations with quantifiers#5317
WillLillis merged 1 commit intotree-sitter:masterfrom
WillLillis:query_dbg

Conversation

@WillLillis
Copy link
Member

When a branch inside an alternation has a + or * quantifier, the quantifier's pass_through step loops back to the branch's first step. The alternation linking also sets that step's alternative_index to point to the next branch. This causes the quantifier loop-back to incorrectly explore other branches in the case of a failed match that follows a successful match, contaminating captures.

This is corrected by redirecting the quantifier loop-back to a "clean" copy of the target step without the alternative index pointing to the next alternation branch.

CC @ribru17 Thanks for providing such a nice report and test case for this bug!

…s with quantifiers

When a branch inside an alternation has a + or * quantifier, the
quantifier's pass_through step loops back to the branch's first step.
The alternation linking also sets that step's alternative_index to
point to the next branch. This causes the quantifier loop-back to
incorrectly explore other branches in the case of a failed match that
follows a successful match, contaminating captures.

This is corrected by redirecting the quantifier loop-back to a
"clean" copy of the target step without the alternative index pointing
to the next alternation branch.

Co-authored-by: Riley Bruins <ribru17@hotmail.com>
@WillLillis WillLillis merged commit 596a4d6 into tree-sitter:master Feb 10, 2026
17 of 20 checks passed
@WillLillis WillLillis deleted the query_dbg branch February 10, 2026 02:57
@tree-sitter-ci-bot
Copy link

Successfully created backport PR for release-0.26:

@ribru17
Copy link
Contributor

ribru17 commented Feb 10, 2026

awesome fix!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c-library ci:backport release-0.26 Backport label query Related to query execution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Quantifiers get applied to successive alternants when iterating query matches

3 participants