Skip to content

Fix SYSLIB1045 code fixer generating duplicate MyRegex names across partial class declarations#124698

Open
danmoseley wants to merge 2 commits intodotnet:mainfrom
danmoseley:fix/77409-codefixer-duplicate-namesc
Open

Fix SYSLIB1045 code fixer generating duplicate MyRegex names across partial class declarations#124698
danmoseley wants to merge 2 commits intodotnet:mainfrom
danmoseley:fix/77409-codefixer-duplicate-namesc

Conversation

@danmoseley
Copy link
Member

@danmoseley danmoseley commented Feb 21, 2026

Fixes #77409

Problem

When the SYSLIB1045 BatchFixer applies multiple code fixes concurrently to separate partial class declarations, each fix independently checks existing members via the semantic model. Since all fixers see the original compilation (before any fix is applied), they all conclude MyRegex is available and generate duplicate property names, causing CS0756.

Fix

In UpgradeToGeneratedRegexCodeFixer.CreateGeneratedRegexProperty, after finding the first available name via the semantic model, the new CountPrecedingRegexCallSites helper scans all DeclaringSyntaxReferences of the containing type for Regex constructor/static-method call sites that would also generate new property names. These are sorted deterministically (by file path, then span position). The current node's index in that sorted list determines how many names to skip, giving each concurrent fixer a unique name.

Test

Added CodeFixerGeneratesUniqueNamesAcrossPartialClassDeclarations — two new Regex(...) calls in separate partial class declarations of the same type, verifying MyRegex and MyRegex1 are generated. Verified the test fails without the fix (both declarations produce MyRegex).

Copy link
Contributor

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.

Pull request overview

This PR fixes issue #77409 where the SYSLIB1045 code fixer (which converts Regex constructor calls to GeneratedRegex) generates duplicate property names when applied to multiple partial class declarations via batch fixing. The root cause is that concurrent batch fixers all see the original compilation state before any fixes are applied, causing them to independently conclude that the same name (e.g., "MyRegex") is available.

The PR also includes two unrelated changes: a performance optimization using SetSearchValues for the regex interpreter, and a regression test for character class encoding validation.

Changes:

  • Adds CountPrecedingRegexCallSites helper to deterministically order all Regex call sites across partial declarations by file path and position, allowing each concurrent fixer to offset its name generation
  • Implements SetSearchValues optimization in the regex interpreter for vectorized character class matching
  • Adds validation for character class encoding before calling GetSetChars to prevent IndexOutOfRangeException on malformed input

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs Adds CountPrecedingRegexCallSites helper and name offset logic to prevent duplicate names in batch fixing
src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToGeneratedRegexAnalyzerTests.cs Adds test validating unique name generation across partial class declarations
src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexInterpreterCode.cs Adds SetSearchValues precomputation and character class encoding validation (unrelated optimization)
src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexInterpreter.cs Uses SetSearchValues for vectorized Set opcode matching (unrelated optimization)
src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/Regex.Match.Tests.cs Adds regression test for Multi literal resembling character class encoding (unrelated)

@danmoseley danmoseley force-pushed the fix/77409-codefixer-duplicate-namesc branch 2 times, most recently from 259abc1 to 3e46fc1 Compare February 21, 2026 05:04
Copilot AI review requested due to automatic review settings February 21, 2026 05:04
Copy link
Contributor

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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Copy link
Contributor

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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Copilot AI review requested due to automatic review settings February 21, 2026 05:16
Copy link
Contributor

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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

@danmoseley danmoseley force-pushed the fix/77409-codefixer-duplicate-namesc branch from ec91456 to ca22fda Compare February 21, 2026 05:24
Copilot AI review requested due to automatic review settings February 21, 2026 05:29
@danmoseley danmoseley force-pushed the fix/77409-codefixer-duplicate-namesc branch from ca22fda to ed09d48 Compare February 21, 2026 05:29
@danmoseley danmoseley force-pushed the fix/77409-codefixer-duplicate-namesc branch from ed09d48 to b83f949 Compare February 21, 2026 05:34
Copy link
Contributor

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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

…artial class declarations

When the BatchFixer applies multiple fixes concurrently, each fix sees the
original compilation and independently picks the same first-available name.
This causes CS0756 when two partial declarations of the same class both
contain Regex calls that need fixing.

The fix scans all partial declarations of the containing type for Regex call
sites that would generate new property names, orders them deterministically
(by file path, then position), and offsets the generated name by the current
node's index in that list.

Fixes dotnet#77409

Co-authored-by: Copilot <[email protected]>
Copy link
Contributor

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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Extract fixable method names into shared FixableMethodNames set on the
analyzer, referenced by both the analyzer and the fixer. Add test
verifying Regex.Escape calls don't inflate the generated name offset.

Co-authored-by: Copilot <[email protected]>
@danmoseley danmoseley force-pushed the fix/77409-codefixer-duplicate-namesc branch from 8af55cd to c9e46b8 Compare February 21, 2026 06:19
// Creates a HashSet of all of the method Symbols containing the static methods to analyze.
static HashSet<IMethodSymbol> GetMethodSymbolHash(INamedTypeSymbol regexTypeSymbol, HashSet<string> methodNames)
{
// This warning is due to a false positive bug https://github.com/dotnet/roslyn-analyzers/issues/5804
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noticed comment was dead, should have been removed with the pragma

Copy link
Contributor

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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

@danmoseley
Copy link
Member Author

ready for review

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.

SYSLIB1045 Use GeneratedRegex auto-correct causes CS0756 multiple defining declarations

2 participants