Skip to content

Auto-possessification loses its place after an optional sibling: (?i)(?:a*\+*)A{2} misses "aA" #898

Closed

Description

@AndreasOlausson

Description

let regex = try Regex(#"(?:a*\+*)A{2}"#).ignoresCase()
print("aA".matches(of: regex).count)   // 0, expected 1 (the whole "aA")

It is the same with inline (?i), and with a capture group: (a*\+*)A{2}. Without the group (a*\+*A{2}) it matches, and so does (?:a*)A{2}. Swift on macOS (the OS's own _StringProcessing) matches all of them. I reproduced the miss with _StringProcessing built from the swift-6.4.0-RELEASE tag. It is still present on main.

Cause

In Sources/_StringProcessing/Optimizations/AutoPossessification.swift, the walk loses its place in the flattened list:

  • In _requiredAtomImpl, the .quantification case returns .some(nil) for an optional quantification (\+*) without moving position past the quantification's child.
  • The .concatenation case of autoPossessifyNextQuantification then returns the quantification it found (a*) to the enclosing concatenation. That concatenation does position += 1, which lands on the + atom inside \+* rather than on A{2}.
  • a excludes +, so a* is made possessive, even though a and A are the same when ignoring case. The possessive a* takes all of "aA", and A{2} fails.

More generally, the functions in this file seem to disagree about where position is left on return (on the node, on its last descendant, or past it).

Suggested fix

Make each child step explicit: probe with a copy of the cursor, then skipNode past the child's whole subtree, as in the forward-progress fix. As a stop-gap, not calling root.autoPossessify() in emitRoot restores correct results. It is only an optimization, and Swift on Apple's platforms doesn't have it.

Activity

  1. added a commit that references this issue on Oct 29, 2026
    fd8128f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions