Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Merged

Conversation

stephentoub
Copy link
Member

Backport of #113081 to release/8.0-staging

/cc @stephentoub

Customer Impact

  • Customer reported
  • Found internally

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:

using System.Text.RegularExpressions;

Example.M().IsMatch("test");

partial class Example
{
    [GeneratedRegex(@"(?<open>)(?<-open>)")]
    public static partial Regex M();
}

on a system in Sweden, it is likely to fail to build.

Regression

  • Yes
  • No

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.

…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.
@stephentoub stephentoub added Servicing-consider Issue for next servicing release review area-System.Text.RegularExpressions labels Mar 5, 2025
@stephentoub stephentoub added this to the 8.0.x milestone Mar 5, 2025
@Copilot Copilot AI review requested due to automatic review settings March 5, 2025 03:03
@stephentoub
Copy link
Member Author

cc: @jeffhandley, @artl93

@stephentoub stephentoub changed the title [release/8.0-staging] Use invariant culture when formatting transfer capture in regex sourc… [release/8.0-staging] Use invariant culture when formatting transfer capture in regex source generator Mar 5, 2025
Copy link
Contributor

@Copilot Copilot AI left a 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

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions
See info in area-owners.md if you want to be subscribed.

@stephentoub stephentoub added Servicing-approved Approved for servicing release and removed Servicing-consider Issue for next servicing release review labels Mar 5, 2025
@stephentoub stephentoub merged commit bbfb162 into dotnet:release/8.0-staging Mar 5, 2025
112 of 120 checks passed
@stephentoub stephentoub deleted the backport113081_8 branch March 5, 2025 18:25
@github-actions github-actions bot locked and limited conversation to collaborators Apr 5, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants