Skip to content

Leave {0}-quantified captures nil instead of empty - #905

Open
hippi345 wants to merge 1 commit into
swiftlang:mainfrom
hippi345:fix/902-zero-quantifier-nil-capture
Open

hippi345 wants to merge 1 commit into
swiftlang:mainfrom
hippi345:fix/902-zero-quantifier-nil-capture

Conversation

@hippi345

Copy link
Copy Markdown

Summary

Matching (a){0}b (and (a){0,0}b) against "b" reported the capture as an empty match ("") instead of nil. A group quantified {0} never participates, so its capture must stay unset, consistent with (a)?b, (a)*b, and (a){0,1}b.

The {0} / {0,0} compiler path dropped the quantified body but still emitted begin/end capture around the skip, which stored an empty range at the current position. This change still drops the never-run body and still allocates capture registers (so later groups keep the correct numbering), but it no longer begins or ends those captures.

Fixes #902

Changes

  • In emitQuantification, skip a {0} / {0,0} operand with preservingCaptures: false and allocatingCaptures: true.
  • skipNode allocates nested capture registers when requested, without recording an empty match.
  • Added flatCaptureTest coverage for (a){0}b, (a){0,0}b, nested ((a)){0}(b), and a {0,1} regression check.

Testing

Fail-before (unmodified compiler, swift test --filter testMatchCaptureBehavior):

Test Case 'RegexTests.testMatchCaptureBehavior' started at 2026-10-17 02:44:24.745
/workspace/Tests/RegexTests/MatchTests.swift:2085: error: RegexTests.testMatchCaptureBehavior : failed - MatchError(message: "Capture mismatch:\n  [nil]\n  [Optional(\"\")]")
/workspace/Tests/RegexTests/MatchTests.swift:2085: error: RegexTests.testMatchCaptureBehavior : failed - MatchError(message: "Capture mismatch:\n  [nil]\n  [Optional(\"\")]")
/workspace/Tests/RegexTests/MatchTests.swift:2089: error: RegexTests.testMatchCaptureBehavior : failed - MatchError(message: "Capture mismatch:\n  [nil]\n  [Optional(\"\")]")
/workspace/Tests/RegexTests/MatchTests.swift:2089: error: RegexTests.testMatchCaptureBehavior : failed - MatchError(message: "Capture mismatch:\n  [nil]\n  [Optional(\"\")]")
/workspace/Tests/RegexTests/MatchTests.swift:2095: error: RegexTests.testMatchCaptureBehavior : failed - MatchError(message: "Capture mismatch:\n  [nil, nil, Optional(\"b\")]\n  [Optional(\"\"), Optional(\"\"), Optional(\"b\")]")
Test Case 'RegexTests.testMatchCaptureBehavior' failed (0.053 seconds)
Test Suite 'RegexTests' failed at 2026-10-17 02:44:24.799
	 Executed 1 test, with 5 failures (0 unexpected) in 0.053 (0.053) seconds

Pass-after (swift test --filter testMatchCaptureBehavior, then RegexTests target):

Test Case 'RegexTests.testMatchCaptureBehavior' started at 2026-10-17 02:44:46.473
Test Case 'RegexTests.testMatchCaptureBehavior' passed (0.05 seconds)
Test Suite 'RegexTests' passed at 2026-10-17 02:44:46.523
	 Executed 1 test, with 0 failures (0 unexpected) in 0.05 (0.05) seconds
Test Suite 'RegexTests' passed at 2026-10-17 02:45:03.397
	 Executed 76 tests, with 0 failures (0 unexpected) in 2.657 (2.657) seconds
Test Suite 'Selected tests' passed at 2026-10-17 02:45:03.661
	 Executed 130 tests, with 0 failures (0 unexpected) in 3.759 (3.759) seconds

This change was prepared with AI assistance and reviewed by the author.

Groups quantified {0} or {0,0} never run, so nested captures must
stay nil. Skipping the body was still emitting begin/end capture,
which stored an empty match at the current position.

Fixes swiftlang#902.
@hippi345
hippi345 marked this pull request as ready for review October 17, 2026 09:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

group quantified {0} reports an empty capture instead of nil

1 participant