Escape U+2028/U+2029 in regex source generator XML doc comments#126242
Merged
danmoseley merged 1 commit intodotnet:mainfrom Mar 28, 2026
Merged
Escape U+2028/U+2029 in regex source generator XML doc comments#126242danmoseley merged 1 commit intodotnet:mainfrom
danmoseley merged 1 commit intodotnet:mainfrom
Conversation
U+2028 (Line Separator) and U+2029 (Paragraph Separator) are valid XML characters but are C# line terminators. When emitted literally into /// doc comments by the regex source generator, they break the comment across lines, causing compilation errors in the generated code. Exclude these two characters from the literal pass-through range in EscapeXmlComment so they are escaped as \u2028 and \u2029 text instead. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a source-generator compilation hazard where patterns containing U+2028 (Line Separator) / U+2029 (Paragraph Separator) could be emitted verbatim into /// XML doc comments, inadvertently terminating the C# line and breaking the generated source.
Changes:
- Update
EscapeXmlCommentto exclude U+2028 and U+2029 from the “pass-through” XML character ranges so they’re emitted as\u2028/\u2029text. - Add functional test cases with patterns containing U+2028, U+2029, and U+FFFE to ensure the source-generator engine can compile and execute these regexes.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs | Ensures U+2028/U+2029 don’t get emitted as literal line terminators inside generated /// doc comments. |
| src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/Regex.Match.Tests.cs | Adds coverage to exercise patterns containing these characters across all regex engines, including the source generator. |
danmoseley
approved these changes
Mar 28, 2026
Member
Author
|
/ba-g known failures |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Note
This PR was generated with the assistance of GitHub Copilot.
U+2028 (Line Separator) and U+2029 (Paragraph Separator) are valid XML characters but are C# line terminators. When the regex source generator emits these literally into
///doc comments, the compiler sees a line break mid-comment, and the continuation isn't a///line — causing XML parse errors andCS1519/CS1056compilation failures in the generated code.The fix excludes these two characters from the literal pass-through range in
EscapeXmlCommentso they are escaped as\u2028and\u2029text instead. All other C# line terminators (CR, LF, NEL) were already handled by falling outside the existing ranges.Changes
RegexGenerator.Emitter.cs: Addnot 0x2028 and not 0x2029to the valid-character pass-through pattern inEscapeXmlComment.Regex.Match.Tests.cs: Add test cases with literal\u2028,\u2029, and\uFFFEin regex patterns, exercised across all engines including the source generator.