Skip to content

Add support for generic constraints to Actor source generator#1535

Open
oising wants to merge 7 commits intodapr:masterfrom
oising:actor-srcgen-generic-constraints
Open

Add support for generic constraints to Actor source generator#1535
oising wants to merge 7 commits intodapr:masterfrom
oising:actor-srcgen-generic-constraints

Conversation

@oising
Copy link

@oising oising commented May 12, 2025

Description

The actor source generator will now correctly add any type constraints to the client class. Supported:

  • where T: class
  • where T: struct
  • where T: unmanaged
  • where T: <class or interface>
  • where T: new()
  • where T: notnull

...and any legal combinations thereof.

I've added tests for a single case (single type parameter and one constraint) and a complex case (multiple type parametes and constraints.)

Issue reference

We strive to have all PR being opened based on an issue, where the problem or feature have been discussed prior to implementation.

Please reference the issue this PR will close: #1534

Checklist

Please make sure you've completed the relevant tasks for this PR, out of the following list:

  • Code compiles correctly
  • Created/updated tests
  • Extended the documentation

@oising oising requested review from a team as code owners May 12, 2025 17:55
Signed-off-by: Oisin Grehan <oisin.grehan@ionodes.com>
@oising oising force-pushed the actor-srcgen-generic-constraints branch from aa000f9 to 087f11b Compare May 12, 2025 17:58
Signed-off-by: Oisin Grehan <oisin.grehan@ionodes.com>
@oising oising force-pushed the actor-srcgen-generic-constraints branch from 1fb41e7 to 435933c Compare May 12, 2025 18:06
Copilot AI review requested due to automatic review settings August 7, 2025 16:36
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for generic type constraints to the Actor source generator, enabling it to properly handle and generate constraint clauses for generic actor interfaces. The generator now correctly preserves constraints like where T: class, where T: struct, where T: unmanaged, interface constraints, where T: new(), and where T: notnull when creating actor client classes.

  • Implemented constraint clause generation for all supported generic constraint types
  • Added logic to iterate through type parameters and build appropriate constraint syntax
  • Added comprehensive test coverage for both single and multiple type parameter scenarios

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/Dapr.Actors.Generators/ActorClientGenerator.cs Implements constraint clause generation logic for generic type parameters in the actor client generator
test/Dapr.Actors.Generators.Test/ActorClientGeneratorTests.cs Adds test cases for single and multiple type parameter constraints to verify the generator works correctly

// Add unmanaged constraint
if (typeParam.HasUnmanagedTypeConstraint)
{
constraints.Add(SyntaxFactory.TypeConstraint(SyntaxFactory.IdentifierName("unmanaged")));
Copy link

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The 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.
Copy link
Author

@oising oising Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot 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

oising and others added 3 commits August 7, 2025 14:37
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +907 to +908
public sealed class TestActorClient<TTrait> : Test.ITestActor<TTrait> where TTrait : Test.ITrait
{
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expected generated code has inconsistent type parameter names: the class is declared as TestActorClient<TTrait> but the constraint clause refers to TTrait. These must match (and should likely use the same type parameter identifier as the source interface).

Copilot uses AI. Check for mistakes.
Comment on lines +954 to +958
[GenerateActorClient]
public interface ITestActor<TTrait, TValidator>
where TTrait : ITrait, new()
where TValidator : class, IValidator<TTrait>
{
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test input source has a typo in the constraint clause: the interface type parameter is TTrait, but the where clause uses TTrait. As written, the originalSource won’t compile, so the test won’t validate constraint copying.

Copilot uses AI. Check for mistakes.
Comment on lines +982 to +985
public System.Threading.Tasks.Task<TTrait> GetValidatedTrait(string name)
{
return this.actorProxy.InvokeMethodAsync<string, TTrait>(""GetValidatedTrait"", name);
}
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expected generated code uses InvokeMethodAsync<string, TTrait>(...), but TTrait isn’t a type parameter on the generated class (it’s TTrait). This mismatch will make the expected output invalid and fail the snapshot comparison once the generator is fixed.

Copilot uses AI. Check for mistakes.
if (typeParam.HasReferenceTypeConstraint ||
typeParam.HasValueTypeConstraint ||
typeParam.HasUnmanagedTypeConstraint ||
typeParam.HasNotNullConstraint ||
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition deciding whether to emit a where ... clause doesn’t include HasConstructorConstraint. If a type parameter only has new() (no other constraints), no constraint clause will be generated, even though the PR claims new() is supported.

Suggested change
typeParam.HasNotNullConstraint ||
typeParam.HasNotNullConstraint ||
typeParam.HasConstructorConstraint ||

Copilot uses AI. Check for mistakes.
Comment on lines +200 to +211
// 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")));
}
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constraint ordering here can produce invalid C#: notnull is appended after the type/interface constraints, but C# requires special constraints (class/struct/unmanaged/notnull) to come before any type constraints, with new() last. Reorder the list so notnull is emitted before ConstraintTypes (and keep new() last).

Copilot uses AI. Check for mistakes.
Comment on lines +896 to +900
public interface ITestActor<in TTrait> where TTrait : ITrait
{
Task<bool> SetTrait(TTrait trait);
Task<ITrait> GetTrait(string name);
}
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new test input source won’t compile: the type parameter is TTrait (ITestActor<in TTrait>), but SetTrait uses TTrait as the parameter type. This should reference the declared type parameter name consistently so the generator test is exercising constraint copying rather than failing to compile the input.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Actor source generator does not copy generic constraints for type parameters

3 participants