Fix regex lowering issue for pattern .|\r|\n #121753
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes a bug in the regex alternation reduction logic where patterns like
.|\r|\nwere not being correctly reduced to[\s\S](match any character).Root Cause
The issue was in the
ReduceSingleLetterAndNestedAlternationsmethod inRegexNode.cs. When merging character nodes in alternations (e.g.,NotoneandOnenodes), the code would:SetnodeRegexCharClass.ToStringClass()method would canonicalize this Set, potentially converting it to a negated form (e.g.,[^\n])lastNodeCannotMergeflag was not updated to reflect whether the merged Set was mergeableThis caused the pattern
.|\r|\nto fail reduction:.(Notone('\n')) |\r(One('\r')) → merged to[^\n](negated Set, not mergeable)\ncould not merge with the negated Set, leaving the pattern as[^\n]|\ninstead of[\s\S]The Fix
After creating the merged Set node:
ReduceSet()to simplify it (e.g.,[^\n]→Notone('\n'))lastNodeCannotMergebased on the final node typeThis ensures:
.|\r→Notone('\n')(since\ris already matched by.)Notone('\n')|\n→[\s\S](matches everything)Changes Made
ReduceSingleLetterAndNestedAlternationsinRegexNode.csto reduce merged Set nodes and update mergeability tracking.|\r|\npattern inRegexReductionTests.csTesting
.|\r|\n[\s\S]The changes are minimal and surgical, affecting only the specific part of the code that was causing the issue.
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.