Skip to content

Conversation

stephentoub
Copy link
Member

Backport of #113081 to release/9.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 9.0.x milestone Mar 5, 2025
@Copilot Copilot AI review requested due to automatic review settings March 5, 2025 03:01
@stephentoub
Copy link
Member Author

cc: @jeffhandley, @artl93

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 pull request backports a fix to ensure that regex source generation uses invariant culture for formatting transfer capture values. The key changes include updating the test helper to run under a modified culture with a non-standard negative sign and modifying the regex emitter to explicitly format numeric values with CultureInfo.InvariantCulture.

  • Added a custom CultureInfo in test code to simulate cultures with a non-standard negative sign.
  • Updated the generator driver block to temporarily set the current culture during regex generation.
  • Modified the numeric formatting in RegexGenerator.Emitter.cs to use CultureInfo.InvariantCulture.

Reviewed Changes

File Description
src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/RegexGeneratorHelper.netcoreapp.cs Added test culture and updated generator culture handling.
src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs Updated numeric formatting for transfer capture to use invariant culture.

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/tests/FunctionalTests/RegexGeneratorHelper.netcoreapp.cs:137

  • Constructing a CultureInfo with an empty string may lead to unexpected behavior; consider basing it on a known culture (e.g., CultureInfo.InvariantCulture) and then modifying its NumberFormat to ensure predictable testing.
private static readonly CultureInfo s_cultureWithMinusNegativeSign = new CultureInfo("")

src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs:2552

  • This change correctly enforces invariant culture formatting for capnum; verify that similar formatting safeguards are in place for other numerical values where culture-specific formatting may cause issues.
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 api-approved API was approved in API review, it can be implemented Servicing-approved Approved for servicing release and removed Servicing-consider Issue for next servicing release review api-approved API was approved in API review, it can be implemented labels Mar 5, 2025
@stephentoub
Copy link
Member Author

/ba-g test failures are unrelated

@stephentoub stephentoub merged commit a5d80ca into dotnet:release/9.0-staging Mar 6, 2025
79 of 82 checks passed
@stephentoub stephentoub deleted the backport113081_9 branch March 6, 2025 15:59
@github-actions github-actions bot locked and limited conversation to collaborators Apr 6, 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