-
Notifications
You must be signed in to change notification settings - Fork 5k
[release/8.0-staging] Use invariant culture when formatting transfer capture in regex source generator #113151
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
[release/8.0-staging] Use invariant culture when formatting transfer capture in regex source generator #113151
Conversation
…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.
cc: @jeffhandley, @artl93 |
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 backports a fix to the regex source generator to ensure number formatting uses invariant culture across all scenarios, even when the system culture differs.
- Introduces a custom CultureInfo in tests that simulates a non-standard negative sign.
- Wraps the generator invocation in a try‐finally block to temporarily set the CurrentCulture to the custom culture.
- Applies invariant culture formatting to the capnum parameter in the regex emitter.
Reviewed Changes
File | Description |
---|---|
src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/RegexGeneratorHelper.netcoreapp.cs | Adds a custom culture instance and a try-finally block to enforce proper culture handling during generator execution |
src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs | Updates numeric formatting to use invariant culture when emitting transfer capture calls |
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs:2422
- The use of CultureInfo.InvariantCulture ensures consistent number formatting; verify that similar numeric format operations within the emitter make use of invariant culture if required for consistency.
writer.WriteLine($"base.TransferCapture({capnum.ToString(CultureInfo.InvariantCulture)}, {uncapnum}, {startingPos}, pos);");
Tip: Copilot only keeps its highest confidence comments to reduce noise and keep you focused. Learn more
...ries/System.Text.RegularExpressions/tests/FunctionalTests/RegexGeneratorHelper.netcoreapp.cs
Show resolved
Hide resolved
Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions |
bbfb162
into
dotnet:release/8.0-staging
Backport of #113081 to release/8.0-staging
/cc @stephentoub
Customer Impact
Using the regex source generator with certain patterns (any containing a "balancing group") will cause the containing project to fail to build when compiling on a system where the culture uses something other than '-' as a negative sign. ~10% of cultures in CultureInfo.GetCultures fit this category. For example, if you try to compile this:
on a system in Sweden, it is likely to fail to build.
Regression
Testing
Updated the test suite to run source generator tests in such a culture.
Risk
Low. It's changing how a single number is rendered, using the invariant culture rather than the current culture. The new code will succeed in all the places the old code did, and where the old code failed, it would fail to compile.