From c780c4d915cdd2b2053a1eb23f825d0e65ce5c95 Mon Sep 17 00:00:00 2001 From: Youssef Victor Date: Mon, 6 Feb 2023 09:38:52 +0200 Subject: [PATCH 1/2] Simplify language version check in Regex generator --- .../gen/RegexGenerator.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs b/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs index 2dd5c6d0d551b..7abee19bb0099 100644 --- a/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs +++ b/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs @@ -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) { @@ -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: @@ -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(methodOrDiagnosticAndCompilationData.Right.LanguageVersion, out string? reason)) { return (regexMethod, reason, Diagnostic.Create(DiagnosticDescriptors.LimitedSourceGeneration, regexMethod.MethodSyntax.GetLocation())); } @@ -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. /// - private static bool SupportsCodeGeneration(RegexMethod method, [NotNullWhen(false)] out string? reason) + private static bool SupportsCodeGeneration(LanguageVersion languageVersion, [NotNullWhen(false)] out string? reason) { - if (method.MethodSyntax.SyntaxTree.Options is CSharpParseOptions { LanguageVersion: <= LanguageVersion.CSharp10 }) + if (languageVersion <= LanguageVersion.CSharp10) { reason = "the language version must be C# 11 or higher."; return false; From d615677a0e7019dd2e4e25c33d28bf2e7361ee31 Mon Sep 17 00:00:00 2001 From: Youssef Victor Date: Mon, 6 Feb 2023 13:37:28 +0200 Subject: [PATCH 2/2] Fix build --- .../System.Text.RegularExpressions/gen/RegexGenerator.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs b/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs index 7abee19bb0099..bda1f161f0472 100644 --- a/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs +++ b/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs @@ -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(methodOrDiagnosticAndCompilationData.Right.LanguageVersion, out string? reason)) + if (!SupportsCodeGeneration(regexMethod, methodOrDiagnosticAndCompilationData.Right.LanguageVersion, out string? reason)) { return (regexMethod, reason, Diagnostic.Create(DiagnosticDescriptors.LimitedSourceGeneration, regexMethod.MethodSyntax.GetLocation())); } @@ -271,7 +271,7 @@ 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. /// - private static bool SupportsCodeGeneration(LanguageVersion languageVersion, [NotNullWhen(false)] out string? reason) + private static bool SupportsCodeGeneration(RegexMethod method, LanguageVersion languageVersion, [NotNullWhen(false)] out string? reason) { if (languageVersion <= LanguageVersion.CSharp10) {