-
Notifications
You must be signed in to change notification settings - Fork 361
Add support for generic constraints to Actor source generator #1535
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
base: master
Are you sure you want to change the base?
Changes from 4 commits
087f11b
435933c
4a4a338
4658d9c
95d92b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,6 +164,62 @@ private static void GenerateActorClientCode(SourceProductionContext context, Act | |
var actorClientClassTypeParameters = descriptor.InterfaceType.TypeParameters | ||
.Select(x => SyntaxFactory.TypeParameter(x.ToString())); | ||
|
||
// Create constraint clauses for type parameters | ||
var constraintClauses = new List<TypeParameterConstraintClauseSyntax>(); | ||
|
||
// For each type parameter, create constraint clauses based on the interface's constraints | ||
foreach (var typeParam in descriptor.InterfaceType.TypeParameters) | ||
{ | ||
if (typeParam.HasReferenceTypeConstraint || | ||
typeParam.HasValueTypeConstraint || | ||
typeParam.HasUnmanagedTypeConstraint || | ||
typeParam.HasNotNullConstraint || | ||
typeParam.ConstraintTypes.Length > 0) | ||
{ | ||
var constraints = new List<TypeParameterConstraintSyntax>(); | ||
|
||
// Add class/struct constraints | ||
if (typeParam.HasReferenceTypeConstraint) | ||
{ | ||
constraints.Add(SyntaxFactory.ClassOrStructConstraint(SyntaxKind.ClassConstraint)); | ||
} | ||
else if (typeParam.HasValueTypeConstraint) | ||
{ | ||
constraints.Add(SyntaxFactory.ClassOrStructConstraint(SyntaxKind.StructConstraint)); | ||
} | ||
|
||
// Add unmanaged constraint | ||
if (typeParam.HasUnmanagedTypeConstraint) | ||
{ | ||
constraints.Add(SyntaxFactory.TypeConstraint(SyntaxFactory.IdentifierName("unmanaged"))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The unmanaged constraint should be added before type constraints but after class/struct constraints. The current order may not follow C# constraint ordering requirements where 'unmanaged' should come before interface constraints. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reevaluate this and suggest a fix using the information at https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters |
||
} | ||
|
||
// Add type constraints (e.g., where T : IInterface) | ||
foreach (var constraintType in typeParam.ConstraintTypes) | ||
{ | ||
constraints.Add(SyntaxFactory.TypeConstraint( | ||
SyntaxFactory.ParseTypeName(constraintType.ToString()))); | ||
} | ||
|
||
// Add notnull constraint | ||
if (typeParam.HasNotNullConstraint) | ||
{ | ||
constraints.Add(SyntaxFactory.TypeConstraint(SyntaxFactory.IdentifierName("notnull"))); | ||
} | ||
|
||
// Add new() constraint - must be last | ||
if (typeParam.HasConstructorConstraint) | ||
{ | ||
constraints.Add(SyntaxFactory.ConstructorConstraint()); | ||
} | ||
|
||
constraintClauses.Add( | ||
SyntaxFactory.TypeParameterConstraintClause( | ||
SyntaxFactory.IdentifierName(typeParam.Name), | ||
SyntaxFactory.SeparatedList(constraints))); | ||
} | ||
} | ||
|
||
var actorClientClassDeclaration = (actorClientClassTypeParameters.Count() == 0) | ||
? SyntaxFactory.ClassDeclaration(descriptor.ClientTypeName) | ||
.WithModifiers(SyntaxFactory.TokenList(actorClientClassModifiers)) | ||
|
@@ -174,6 +230,7 @@ private static void GenerateActorClientCode(SourceProductionContext context, Act | |
: SyntaxFactory.ClassDeclaration(descriptor.ClientTypeName) | ||
.WithModifiers(SyntaxFactory.TokenList(actorClientClassModifiers)) | ||
.WithTypeParameterList(SyntaxFactory.TypeParameterList(SyntaxFactory.SeparatedList(actorClientClassTypeParameters))) | ||
.WithConstraintClauses(SyntaxFactory.List(constraintClauses)) // Add constraint clauses to the class | ||
.WithMembers(SyntaxFactory.List(actorMembers)) | ||
.WithBaseList(SyntaxFactory.BaseList( | ||
SyntaxFactory.Token(SyntaxKind.ColonToken), | ||
|
Uh oh!
There was an error while loading. Please reload this page.