-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use invariant culture when formatting capture transfer in regex source generator #113081
Conversation
…e generator A balancing group can result in TransferCapture being emitted with a negative "capnum". If the compiler is running under a culture that uses something other than '-' as the negative sign, the resulting generated code will fail to compile.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Overview
This PR fixes a bug where a balancing group could result in the generated code emitting a negative capture number using the wrong culture-specific negative sign.
- Introduces a custom culture (s_cultureWithMinusNegativeSign) for testing that uses a non-standard negative sign.
- Updates code in the emitter to format the capnum using CultureInfo.InvariantCulture.
Reviewed Changes
File | Description |
---|---|
src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/RegexGeneratorHelper.netcoreapp.cs | Added a custom culture to simulate an alternate negative sign and updated culture handling in the generator run block. |
src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs | Modified the TransferCapture call to format capnum with CultureInfo.InvariantCulture. |
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
Comments suppressed due to low confidence (2)
src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs:2576
- The change correctly formats capnum with CultureInfo.InvariantCulture; consider reviewing if other numeric parameters (e.g., uncapnum) should also be formatted invariantly for consistency.
writer.WriteLine($"base.TransferCapture({capnum.ToString(CultureInfo.InvariantCulture)}, {uncapnum}, {startingPos}, pos);");
src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/RegexGeneratorHelper.netcoreapp.cs:137
- [nitpick] Consider using an explicit culture identifier (e.g., "en-US") when initializing s_cultureWithMinusNegativeSign to clarify its intended purpose, rather than an empty string.
private static readonly CultureInfo s_cultureWithMinusNegativeSign = new CultureInfo("")
Tip: Copilot code review supports C#, Go, Java, JavaScript, Markdown, Python, Ruby and TypeScript, with more languages coming soon. Learn more
src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs
Show resolved
Hide resolved
/backport to release/9.0-staging |
Started backporting to release/9.0-staging: https://github.com/dotnet/runtime/actions/runs/13657696434 |
/backport to release/8.0-staging |
Started backporting to release/8.0-staging: https://github.com/dotnet/runtime/actions/runs/13657711369 |
@stephentoub backporting to "release/9.0-staging" failed, the patch most likely resulted in conflicts: $ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch
Applying: Use invariant culture when formatting transfer capture in regex source generator
Using index info to reconstruct a base tree...
M src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs
Falling back to patching base and 3-way merge...
Auto-merging src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs
CONFLICT (content): Merge conflict in src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0001 Use invariant culture when formatting transfer capture in regex source generator
Error: The process '/usr/bin/git' failed with exit code 128 Please backport manually! |
@stephentoub backporting to "release/8.0-staging" failed, the patch most likely resulted in conflicts: $ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch
Applying: Use invariant culture when formatting transfer capture in regex source generator
Using index info to reconstruct a base tree...
M src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs
M src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/RegexGeneratorHelper.netcoreapp.cs
Falling back to patching base and 3-way merge...
Auto-merging src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/RegexGeneratorHelper.netcoreapp.cs
Auto-merging src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs
CONFLICT (content): Merge conflict in src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0001 Use invariant culture when formatting transfer capture in regex source generator
Error: The process '/usr/bin/git' failed with exit code 128 Please backport manually! |
…e generator (dotnet#113081) A balancing group can result in TransferCapture being emitted with a negative "capnum". If the compiler is running under a culture that uses something other than '-' as the negative sign, the resulting generated code will fail to compile.
…e generator (dotnet#113081) A balancing group can result in TransferCapture being emitted with a negative "capnum". If the compiler is running under a culture that uses something other than '-' as the negative sign, the resulting generated code will fail to compile.
A balancing group can result in TransferCapture being emitted with a negative "capnum". If the compiler is running under a culture that uses something other than '-' as the negative sign, the resulting generated code will fail to compile.
Fixes #113077