Skip to content
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

Merged
merged 1 commit into from
Mar 4, 2025

Conversation

stephentoub
Copy link
Member

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

…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.
@Copilot Copilot bot review requested due to automatic review settings March 3, 2025 14:07

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

@stephentoub stephentoub merged commit f0aa3ab into dotnet:main Mar 4, 2025
82 of 84 checks passed
@stephentoub stephentoub deleted the fixrsgculture branch March 4, 2025 15:56
@stephentoub
Copy link
Member Author

/backport to release/9.0-staging

Copy link
Contributor

github-actions bot commented Mar 4, 2025

Started backporting to release/9.0-staging: https://github.com/dotnet/runtime/actions/runs/13657696434

@stephentoub
Copy link
Member Author

/backport to release/8.0-staging

Copy link
Contributor

github-actions bot commented Mar 4, 2025

Started backporting to release/8.0-staging: https://github.com/dotnet/runtime/actions/runs/13657711369

Copy link
Contributor

github-actions bot commented Mar 4, 2025

@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!

Copy link
Contributor

github-actions bot commented Mar 4, 2025

@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!

stephentoub added a commit to stephentoub/runtime that referenced this pull request Mar 5, 2025
…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 added a commit to stephentoub/runtime that referenced this pull request Mar 5, 2025
…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 added a commit that referenced this pull request Mar 5, 2025
…e generator (#113081) (#113151)

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 added a commit that referenced this pull request Mar 6, 2025
…e generator (#113081) (#113150)

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

error CS1056: Unexpected character '−' with Regex on Mac
3 participants