Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/Dapr.Actors.Generators/ActorClientGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,65 @@
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));

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Build

) expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Build

Invalid expression term 'else'

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Build

Syntax error, '(' expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Build

'else' cannot start a statement.

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Build

; expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Build

) expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Build

Invalid expression term 'else'

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Build

Syntax error, '(' expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Build

'else' cannot start a statement.

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 9.0

) expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 9.0

Invalid expression term 'else'

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 9.0

Syntax error, '(' expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 9.0

'else' cannot start a statement.

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 9.0

; expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 9.0

) expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 9.0

Invalid expression term 'else'

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 9.0

Syntax error, '(' expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 9.0

'else' cannot start a statement.

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (8.0)

) expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (8.0)

Invalid expression term 'else'

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (8.0)

Syntax error, '(' expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (8.0)

'else' cannot start a statement.

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (8.0)

; expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (8.0)

) expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (8.0)

Invalid expression term 'else'

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (8.0)

Syntax error, '(' expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (8.0)

'else' cannot start a statement.

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 6.0

) expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 6.0

Invalid expression term 'else'

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 6.0

Syntax error, '(' expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 6.0

'else' cannot start a statement.

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 6.0

; expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 6.0

) expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 6.0

Invalid expression term 'else'

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 6.0

Syntax error, '(' expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 6.0

'else' cannot start a statement.

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 7.0

) expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 7.0

Invalid expression term 'else'

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 7.0

Syntax error, '(' expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 7.0

'else' cannot start a statement.

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 7.0

; expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 7.0

) expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 7.0

Invalid expression term 'else'

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 7.0

Syntax error, '(' expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 7.0

'else' cannot start a statement.

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (9.0)

) expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (9.0)

Invalid expression term 'else'

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (9.0)

Syntax error, '(' expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (9.0)

'else' cannot start a statement.

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (9.0)

; expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (9.0)

) expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (9.0)

Invalid expression term 'else'

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (9.0)

Syntax error, '(' expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (9.0)

'else' cannot start a statement.

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (7.0)

) expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (7.0)

Invalid expression term 'else'

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (7.0)

Syntax error, '(' expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (7.0)

'else' cannot start a statement.

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (7.0)

; expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (7.0)

) expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (7.0)

Invalid expression term 'else'

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (7.0)

Syntax error, '(' expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (7.0)

'else' cannot start a statement.

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 8.0

) expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 8.0

Invalid expression term 'else'

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 8.0

Syntax error, '(' expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 8.0

'else' cannot start a statement.

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 8.0

; expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 8.0

) expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 8.0

Invalid expression term 'else'

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 8.0

Syntax error, '(' expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 8.0

'else' cannot start a statement.

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (6.0)

) expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (6.0)

Invalid expression term 'else'

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (6.0)

Syntax error, '(' expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (6.0)

'else' cannot start a statement.

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (6.0)

; expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (6.0)

) expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (6.0)

Invalid expression term 'else'

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (6.0)

Syntax error, '(' expected

Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (6.0)

'else' cannot start a statement.
else if (typeParam.HasValueTypeConstraint && !typeParam.HasUnmanagedTypeConstraint)
{
constraints.Add(SyntaxFactory.ClassOrStructConstraint(SyntaxKind.StructConstraint));
}

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

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

Choose a reason for hiding this comment

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

}

// 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))
Expand All @@ -174,6 +233,7 @@
: 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),
Expand All @@ -192,7 +252,7 @@
.ToFullString();

context.AddSource($"{descriptor.FullyQualifiedTypeName}.g.cs", compilationOutput);
}

Check failure on line 255 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Build

} expected

Check failure on line 255 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 9.0

} expected

Check failure on line 255 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (8.0)

} expected

Check failure on line 255 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 6.0

} expected

Check failure on line 255 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 7.0

} expected

Check failure on line 255 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (9.0)

} expected

Check failure on line 255 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (7.0)

} expected

Check failure on line 255 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET 8.0

} expected

Check failure on line 255 in src/Dapr.Actors.Generators/ActorClientGenerator.cs

View workflow job for this annotation

GitHub Actions / run integration tests (6.0)

} expected
catch (DiagnosticsException e)
{
foreach (var diagnostic in e.Diagnostics)
Expand Down
116 changes: 116 additions & 0 deletions test/Dapr.Actors.Generators.Test/ActorClientGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -877,4 +877,120 @@ public interface ITestActor

await test.RunAsync();
}

[Fact]
public async Task TestGenericWithConstraints()
{
var originalSource = @"
using Dapr.Actors.Generators;
using System.Threading.Tasks;

namespace Test
{
public interface ITrait
{
string GetName();
}

[GenerateActorClient]
public interface ITestActor<in TTrait> where TTrait : ITrait
{
Task<bool> SetTrait(TTrait trait);
Task<ITrait> GetTrait(string name);
}
}";

var generatedSource = @"// <auto-generated/>
#nullable enable
namespace Test
{
public sealed class TestActorClient<TTrait> : Test.ITestActor<TTrait> where TTrait : Test.ITrait
{
private readonly Dapr.Actors.Client.ActorProxy actorProxy;
public TestActorClient(Dapr.Actors.Client.ActorProxy actorProxy)
{
if (actorProxy is null)
{
throw new System.ArgumentNullException(nameof(actorProxy));
}

this.actorProxy = actorProxy;
}

public System.Threading.Tasks.Task<Test.ITrait> GetTrait(string name)
{
return this.actorProxy.InvokeMethodAsync<string, Test.ITrait>(""GetTrait"", name);
}

public System.Threading.Tasks.Task<bool> SetTrait(TTrait trait)
{
return this.actorProxy.InvokeMethodAsync<TTrait, bool>(""SetTrait"", trait);
}
}
}";

await CreateTest(originalSource, "Test.TestActorClient.g.cs", generatedSource).RunAsync();
}

[Fact]
public async Task TestGenericWithMultipleTypeParametersAndConstraints()
{
var originalSource = @"
using Dapr.Actors.Generators;
using System.Threading.Tasks;

namespace Test
{
public interface ITrait
{
string GetName();
}

public interface IValidator<T>
{
bool Validate(T item);
}

[GenerateActorClient]
public interface ITestActor<TTrait, TValidator>
where TTrait : ITrait, new()
where TValidator : class, IValidator<TTrait>
{
Task<bool> SetTrait(TTrait trait);
Task<TTrait> GetValidatedTrait(string name);
}
}";

var generatedSource = @"// <auto-generated/>
#nullable enable
namespace Test
{
public sealed class TestActorClient<TTrait, TValidator> : Test.ITestActor<TTrait, TValidator> where TTrait : Test.ITrait, new()
where TValidator : class, Test.IValidator<TTrait>
{
private readonly Dapr.Actors.Client.ActorProxy actorProxy;
public TestActorClient(Dapr.Actors.Client.ActorProxy actorProxy)
{
if (actorProxy is null)
{
throw new System.ArgumentNullException(nameof(actorProxy));
}

this.actorProxy = actorProxy;
}

public System.Threading.Tasks.Task<TTrait> GetValidatedTrait(string name)
{
return this.actorProxy.InvokeMethodAsync<string, TTrait>(""GetValidatedTrait"", name);
}

public System.Threading.Tasks.Task<bool> SetTrait(TTrait trait)
{
return this.actorProxy.InvokeMethodAsync<TTrait, bool>(""SetTrait"", trait);
}
}
}";

await CreateTest(originalSource, "Test.TestActorClient.g.cs", generatedSource).RunAsync();
}
}
Loading