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

Simplify language version check in Regex generator #81678

Merged
merged 2 commits into from
Feb 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public partial class RegexGenerator : IIncrementalGenerator
"#pragma warning disable CS0219 // Variable assigned but never used",
};

private record struct CompilationData(bool AllowUnsafe, bool CheckOverflow);
private record struct CompilationData(bool AllowUnsafe, bool CheckOverflow, LanguageVersion LanguageVersion);

public void Initialize(IncrementalGeneratorInitializationContext context)
{
Expand All @@ -46,7 +46,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
context.CompilationProvider
.Select((x, _) =>
x.Options is CSharpCompilationOptions options ?
new CompilationData(options.AllowUnsafe, options.CheckOverflow) :
new CompilationData(options.AllowUnsafe, options.CheckOverflow, ((CSharpCompilation)x).LanguageVersion) :
default);

// Produces one entry per generated regex. This may be:
Expand Down Expand Up @@ -78,7 +78,7 @@ x.Options is CSharpCompilationOptions options ?

// If we're unable to generate a full implementation for this regex, report a diagnostic.
// We'll still output a limited implementation that just caches a new Regex(...).
if (!SupportsCodeGeneration(regexMethod, out string? reason))
if (!SupportsCodeGeneration(regexMethod, methodOrDiagnosticAndCompilationData.Right.LanguageVersion, out string? reason))
{
return (regexMethod, reason, Diagnostic.Create(DiagnosticDescriptors.LimitedSourceGeneration, regexMethod.MethodSyntax.GetLocation()));
}
Expand Down Expand Up @@ -271,9 +271,9 @@ x.Options is CSharpCompilationOptions options ?
// It also provides a human-readable string to explain the reason. It will be emitted by the source generator
// as a comment into the C# code, hence there's no need to localize.
/// </remarks>
private static bool SupportsCodeGeneration(RegexMethod method, [NotNullWhen(false)] out string? reason)
private static bool SupportsCodeGeneration(RegexMethod method, LanguageVersion languageVersion, [NotNullWhen(false)] out string? reason)
{
if (method.MethodSyntax.SyntaxTree.Options is CSharpParseOptions { LanguageVersion: <= LanguageVersion.CSharp10 })
Copy link
Member Author

Choose a reason for hiding this comment

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

Ideally, MethodSyntax shouldn't exist in the RegexMethod model at all. I could eliminate this usage, but not the other one. (the other usage can be changed to be Location instead of MethodDeclarationSyntax but that is not good as well)

So I simplified this usage for now.

Someone from Roslyn team should be able to advise about the other usage.

if (languageVersion <= LanguageVersion.CSharp10)
{
reason = "the language version must be C# 11 or higher.";
return false;
Expand Down