From: Youssef Victor Date: Mon, 6 Feb 2023 20:23:54 +0000 (+0200) Subject: Simplify language version check in Regex generator (#81678) X-Git-Tag: accepted/tizen/unified/riscv/20231226.055536~4205 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ecd71ac2cb7ab3444ae9bc53aedc8f8e767aa576;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Simplify language version check in Regex generator (#81678) * Simplify language version check in Regex generator * Fix build --- diff --git a/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs b/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs index 2dd5c6d..bda1f16 100644 --- a/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs +++ b/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs @@ -36,7 +36,7 @@ namespace System.Text.RegularExpressions.Generator "#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 @@ namespace System.Text.RegularExpressions.Generator 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 @@ namespace System.Text.RegularExpressions.Generator // 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())); } @@ -271,9 +271,9 @@ namespace System.Text.RegularExpressions.Generator // 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(RegexMethod method, 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;