Skip to content

Commit 6b882b4

Browse files
authored
Avoid using file in generated regexes pre-C# 11 (#81726)
1 parent 1f43fc7 commit 6b882b4

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,22 @@ private static void EmitRegexPartialMethod(RegexMethod regexMethod, IndentedText
7272

7373
/// <summary>Emits the Regex-derived type for a method where we're unable to generate custom code.</summary>
7474
private static void EmitRegexLimitedBoilerplate(
75-
IndentedTextWriter writer, RegexMethod rm, string reason)
75+
IndentedTextWriter writer, RegexMethod rm, string reason, LanguageVersion langVer)
7676
{
77-
writer.WriteLine($"/// <summary>Caches a <see cref=\"Regex\"/> instance for the {rm.MethodName} method.</summary>");
77+
string visibility;
78+
if (langVer >= LanguageVersion.CSharp11)
79+
{
80+
visibility = "file";
81+
writer.WriteLine($"/// <summary>Caches a <see cref=\"Regex\"/> instance for the {rm.MethodName} method.</summary>");
82+
}
83+
else
84+
{
85+
visibility = "internal";
86+
writer.WriteLine($"/// <summary>This class supports generated regexes and should not be used by other code directly.</summary>");
87+
}
7888
writer.WriteLine($"/// <remarks>A custom Regex-derived type could not be generated because {reason}.</remarks>");
7989
writer.WriteLine($"[{s_generatedCodeAttribute}]");
80-
writer.WriteLine($"file sealed class {rm.GeneratedName} : Regex");
90+
writer.WriteLine($"{visibility} sealed class {rm.GeneratedName} : Regex");
8191
writer.WriteLine($"{{");
8292
writer.WriteLine($" /// <summary>Cached, thread-safe singleton instance.</summary>");
8393
writer.Write($" internal static readonly Regex Instance = ");

src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ x.Options is CSharpCompilationOptions options ?
8080
// We'll still output a limited implementation that just caches a new Regex(...).
8181
if (!SupportsCodeGeneration(regexMethod, methodOrDiagnosticAndCompilationData.Right.LanguageVersion, out string? reason))
8282
{
83-
return (regexMethod, reason, Diagnostic.Create(DiagnosticDescriptors.LimitedSourceGeneration, regexMethod.MethodSyntax.GetLocation()));
83+
return (regexMethod, reason, Diagnostic.Create(DiagnosticDescriptors.LimitedSourceGeneration, regexMethod.MethodSyntax.GetLocation()), methodOrDiagnosticAndCompilationData.Right);
8484
}
8585

8686
// Generate the core logic for the regex.
@@ -150,7 +150,7 @@ x.Options is CSharpCompilationOptions options ?
150150
foreach (object? result in results)
151151
{
152152
RegexMethod? regexMethod = null;
153-
if (result is ValueTuple<RegexMethod, string, Diagnostic> limitedSupportResult)
153+
if (result is ValueTuple<RegexMethod, string, Diagnostic, CompilationData> limitedSupportResult)
154154
{
155155
context.ReportDiagnostic(limitedSupportResult.Item3);
156156
regexMethod = limitedSupportResult.Item1;
@@ -212,11 +212,11 @@ x.Options is CSharpCompilationOptions options ?
212212
writer.Indent++;
213213
foreach (object? result in results)
214214
{
215-
if (result is ValueTuple<RegexMethod, string, Diagnostic> limitedSupportResult)
215+
if (result is ValueTuple<RegexMethod, string, Diagnostic, CompilationData> limitedSupportResult)
216216
{
217217
if (!limitedSupportResult.Item1.IsDuplicate)
218218
{
219-
EmitRegexLimitedBoilerplate(writer, limitedSupportResult.Item1, limitedSupportResult.Item2);
219+
EmitRegexLimitedBoilerplate(writer, limitedSupportResult.Item1, limitedSupportResult.Item2, limitedSupportResult.Item4.LanguageVersion);
220220
writer.WriteLine();
221221
}
222222
}
@@ -273,7 +273,7 @@ x.Options is CSharpCompilationOptions options ?
273273
/// </remarks>
274274
private static bool SupportsCodeGeneration(RegexMethod method, LanguageVersion languageVersion, [NotNullWhen(false)] out string? reason)
275275
{
276-
if (languageVersion <= LanguageVersion.CSharp10)
276+
if (languageVersion < LanguageVersion.CSharp11)
277277
{
278278
reason = "the language version must be C# 11 or higher.";
279279
return false;

src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/RegexGeneratorHelper.netcoreapp.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ internal static async Task<IReadOnlyList<Diagnostic>> RunGenerator(
103103
EmitResult results = comp.Emit(Stream.Null, cancellationToken: cancellationToken);
104104
ImmutableArray<Diagnostic> generatorDiagnostics = generatorResults.Diagnostics.RemoveAll(d => d.Severity <= DiagnosticSeverity.Hidden);
105105
ImmutableArray<Diagnostic> resultsDiagnostics = results.Diagnostics.RemoveAll(d => d.Severity <= DiagnosticSeverity.Hidden);
106-
if (!results.Success || resultsDiagnostics.Length != 0 || generatorDiagnostics.Length != 0)
106+
if (!results.Success || resultsDiagnostics.Length != 0)
107107
{
108108
throw new ArgumentException(
109109
string.Join(Environment.NewLine, resultsDiagnostics.Concat(generatorDiagnostics)) + Environment.NewLine +

src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/RegexGeneratorParserTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ partial class C
252252
[GeneratedRegex(""ab"")]
253253
private static partial Regex InvalidLangVersion();
254254
}
255-
", langVersion: version);
255+
", langVersion: version, compile: true);
256256

257257
Assert.Equal("SYSLIB1044", Assert.Single(diagnostics).Id);
258258
}

0 commit comments

Comments
 (0)