Skip to content

Refactor regex source generator to follow best practices. - #135079

Open
teo-tsirpanis wants to merge 3 commits into
dotnet:mainfrom
teo-tsirpanis:regex-sg-opt
Open

teo-tsirpanis wants to merge 3 commits into
dotnet:mainfrom
teo-tsirpanis:regex-sg-opt

Conversation

@teo-tsirpanis

Copy link
Copy Markdown
Contributor
  • Moved emission of diagnostics to an analyzer.
    • Follows best practices, and ensures optimal performance.
    • Previous issues like the inability to suppress diagnostics are now trivially fixed, without us needing to do anything special.
    • Both the generator and the analyzer are calling the same helper methods, that call an optional Action<Diagnostic> to report diagnostics. The analyzer passes a callback, while the generator passes null.
    • This allows us to remove Locations and Diagnostics from the generator's pipeline.
    • This also allows us to make the pipeline strongly-typed, instead of using object.
  • Simplified the generator's pipeline.
    • Regexes are uniformly represented with the same type, regardless of whether they support code generation or not.
    • We update the equality semantics of all pipeline types to only consider the RegexTypeAndPattern they contain. We can do this, because all other properties of the types depend on it.
  • Updated tests to run the analyzer after the generator.
    • All RegexGeneratorParserTests are passing locally.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 2, 2026
@teo-tsirpanis teo-tsirpanis added tenet-performance Performance related issue source-generator Indicates an issue with a source generator feature labels Oct 2, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Ambiguous attribute lookup can silently disable regex diagnostics, and incremental caching needs regression coverage.

2 open findings
What changed in this PR

Refactors the regex source generator to separate diagnostics from generation and improve incremental caching, addressing #125409.

Changes:

  • Moves diagnostics into an analyzer using shared parsing helpers.
  • Introduces strongly typed pipeline models with value-based equality.
  • Updates test helpers and diagnostic suppression coverage.

Validation was limited to source inspection; builds and tests were not run.

File Description
src/​libraries/​System.Text.RegularExpressions/​tests/​FunctionalTests/​RegexGeneratorParserTests.cs Updates pragma suppression expectations.
src/​libraries/​System.Text.RegularExpressions/​tests/​FunctionalTests/​RegexGeneratorHelper.netcoreapp.cs Runs the analyzer after generation.
src/​libraries/​System.Text.RegularExpressions/​gen/​System.Text.RegularExpressions.Generator.csproj Enables Roslyn component checks.
src/​libraries/​System.Text.RegularExpressions/​gen/​RegexGenerator.Parser.cs Shares validation and defines model equality.
src/​libraries/​System.Text.RegularExpressions/​gen/​RegexGenerator.Emitter.cs Reads emission settings from the model.
src/​libraries/​System.Text.RegularExpressions/​gen/​RegexGenerator.cs Simplifies the typed incremental pipeline.
src/​libraries/​System.Text.RegularExpressions/​gen/​RegexGenerator.Analyzer.cs Adds dedicated regex diagnostics analysis.

🧠 Review effort: Balanced


context.RegisterCompilationStartAction(context =>
{
var generatedRegexAttributeSymbol = context.Compilation.GetTypeByMetadataName(GeneratedRegexAttributeName);
Comment on lines +320 to +321
public override bool Equals(object? obj) => obj is RegexMethod other && PatternAndSyntax.Equals(other.PatternAndSyntax);
public override int GetHashCode() => PatternAndSyntax.GetHashCode();

This branch was previously deployed

1 inactive (outdated) deployment
copilot-pat-pool — fc4234ba Deployed Oct 2, 2026 by teo-tsirpanis via conclusion #9539
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Text.RegularExpressions community-contribution Indicates that the PR has been added by a community member source-generator Indicates an issue with a source generator feature tenet-performance Performance related issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RegexGenerator source model lacks value equality, preventing effective incremental caching

2 participants