Skip to content

Commit d5c22b0

Browse files
authored
Remove constraints from Regex source generator partial class declaration (#63637)
1 parent 93c6134 commit d5c22b0

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
54
using System.Buffers.Binary;
65
using System.CodeDom.Compiler;
76
using System.Collections;
@@ -11,10 +10,7 @@
1110
using System.Globalization;
1211
using System.IO;
1312
using System.Linq;
14-
using System.Runtime.CompilerServices;
1513
using System.Runtime.InteropServices;
16-
using System.Security.Cryptography;
17-
using System.Text;
1814
using System.Threading;
1915
using Microsoft.CodeAnalysis;
2016
using Microsoft.CodeAnalysis.CSharp;
@@ -59,7 +55,7 @@ private static (string, ImmutableArray<Diagnostic>) EmitRegexType(RegexType rege
5955
var parentClasses = new Stack<string>();
6056
while (parent is not null)
6157
{
62-
parentClasses.Push($"partial {parent.Keyword} {parent.Name} {parent.Constraints}");
58+
parentClasses.Push($"partial {parent.Keyword} {parent.Name}");
6359
parent = parent.ParentClass;
6460
}
6561
while (parentClasses.Count != 0)
@@ -70,7 +66,7 @@ private static (string, ImmutableArray<Diagnostic>) EmitRegexType(RegexType rege
7066
}
7167

7268
// Emit the direct parent type
73-
writer.WriteLine($"partial {regexClass.Keyword} {regexClass.Name} {regexClass.Constraints}");
69+
writer.WriteLine($"partial {regexClass.Keyword} {regexClass.Name}");
7470
writer.WriteLine("{");
7571
writer.Indent++;
7672

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ private static bool IsSyntaxTargetForGeneration(SyntaxNode node) =>
202202
regexMethod,
203203
typeDec is RecordDeclarationSyntax rds ? $"{typeDec.Keyword.ValueText} {rds.ClassOrStructKeyword}" : typeDec.Keyword.ValueText,
204204
ns ?? string.Empty,
205-
$"{typeDec.Identifier}{typeDec.TypeParameterList}",
206-
typeDec.ConstraintClauses.ToString());
205+
$"{typeDec.Identifier}{typeDec.TypeParameterList}");
207206

208207
RegexType current = regexType;
209208
var parent = typeDec.Parent as TypeDeclarationSyntax;
@@ -214,8 +213,7 @@ private static bool IsSyntaxTargetForGeneration(SyntaxNode node) =>
214213
null,
215214
parent is RecordDeclarationSyntax rds2 ? $"{parent.Keyword.ValueText} {rds2.ClassOrStructKeyword}" : parent.Keyword.ValueText,
216215
ns ?? string.Empty,
217-
$"{parent.Identifier}{parent.TypeParameterList}",
218-
parent.ConstraintClauses.ToString());
216+
$"{parent.Identifier}{parent.TypeParameterList}");
219217

220218
current = current.ParentClass;
221219
parent = parent.Parent as TypeDeclarationSyntax;
@@ -235,7 +233,7 @@ static bool IsAllowedKind(SyntaxKind kind) =>
235233
internal sealed record RegexMethod(MethodDeclarationSyntax MethodSyntax, string MethodName, string Modifiers, string Pattern, RegexOptions Options, int MatchTimeout, RegexCode Code);
236234

237235
/// <summary>A type holding a regex method.</summary>
238-
internal sealed record RegexType(RegexMethod? Method, string Keyword, string Namespace, string Name, string Constraints)
236+
internal sealed record RegexType(RegexMethod? Method, string Keyword, string Namespace, string Name)
239237
{
240238
public RegexType? ParentClass { get; set; }
241239
}

src/libraries/System.Text.RegularExpressions/tests/System.Text.RegularExpressions.Generators.Tests/RegexGeneratorParserTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,30 @@ partial class C
442442
", compile: true));
443443
}
444444

445+
[Fact]
446+
public async Task Valid_ClassWithGenericConstraints()
447+
{
448+
Assert.Empty(await RunGenerator(@"
449+
using D;
450+
using System.Text.RegularExpressions;
451+
namespace A
452+
{
453+
public partial class B<U>
454+
{
455+
private partial class C<T> where T : IBlah
456+
{
457+
[RegexGenerator(""ab"")]
458+
private static partial Regex Valid();
459+
}
460+
}
461+
}
462+
namespace D
463+
{
464+
internal interface IBlah { }
465+
}
466+
", compile: true));
467+
}
468+
445469
public static IEnumerable<object[]> Valid_Modifiers_MemberData()
446470
{
447471
foreach (string type in new[] { "class", "struct", "record", "record struct", "record class", "interface" })

0 commit comments

Comments
 (0)