Skip to content

Commit

Permalink
Fix #44 Ensure generic member access preserves its type arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
virzak committed Jan 21, 2024
1 parent c5017e7 commit f27382a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/Zomp.SyncMethodGenerator/AsyncToSyncRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ bool IsValidParameter(ParameterSyntax ps)
}
else if (@base.Name.Identifier.ValueText.EndsWithAsync())
{
return @base.WithName(SyntaxFactory.IdentifierName(RemoveAsync(@base.Name.Identifier.ValueText)));
return @base.WithName(@base.ChangeIdentifier(RemoveAsync(@base.Name.Identifier.ValueText)));
}

return @base;
Expand Down Expand Up @@ -1222,27 +1222,22 @@ private static InvocationExpressionSyntax UnwrapExtension(InvocationExpressionSy
newName = RemoveAsync(newName);
}

var id = Identifier($"{MakeType(reducedFrom.ContainingType)}.{newName}");
var fullyQualifiedName = $"{MakeType(reducedFrom.ContainingType)}.{newName}";

ExpressionSyntax es = ies.Expression is MemberAccessExpressionSyntax { Name: GenericNameSyntax gns }
? GenericName(id, gns.TypeArgumentList)
: IdentifierName(id);
var es = ies.Expression is MemberAccessExpressionSyntax mae
? mae.ChangeIdentifier(fullyQualifiedName)
: IdentifierName(Identifier(fullyQualifiedName));

return ies
.WithExpression(es)
.WithArgumentList(ArgumentList(newList));
}

private static string? GetNewName(IMethodSymbol methodSymbol)
private static string GetNewName(IMethodSymbol methodSymbol)
{
var containingType = methodSymbol.ContainingType;
var replacement = Regex.Replace(methodSymbol.Name, "Memory", "Span");
var newSymbol = containingType.GetMembers().FirstOrDefault(z => z.Name == replacement);
if (newSymbol is null)
{
return null;
}

return replacement;
}

Expand Down
7 changes: 7 additions & 0 deletions src/Zomp.SyncMethodGenerator/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ internal static T AppendSpace<T>(this T syntax)
where T : ExpressionSyntax
=> syntax.WithTrailingTrivia(Space);

internal static SimpleNameSyntax ChangeIdentifier(this MemberAccessExpressionSyntax mae, string id)
{
return mae is { Name: GenericNameSyntax gns }
? GenericName(Identifier(id), gns.TypeArgumentList)
: IdentifierName(Identifier(id));
}

internal static BlockSyntax CreateBlock(this ICollection<StatementSyntax> statements, int indentationLevel = 0)
{
var list = new List<StatementSyntax>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//HintName: Test.GenericClass.FooAsync.g.cs
// <auto-generated/>
#nullable enable
namespace Test;
partial class GenericClass<T>
{
public T Foo<T>()
=> this.InnerFoo<T>();
}
14 changes: 14 additions & 0 deletions tests/Generator.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ public async Task MethodAsync()
}
}
""".Verify(sourceType: SourceType.Full);

[Fact]
public Task GenericAsyncCall() => """
namespace Test;
partial class GenericClass<T>
{
[CreateSyncVersion]
public async Task<T> FooAsync<T>(CancellationToken ct = default)
=> await this.InnerFooAsync<T>(ct);
private async Task<T> InnerFooAsync<T>(CancellationToken ct = default) => default;
private async T InnerFoo<T>() => default;
}
""".Verify(sourceType: SourceType.Full);

[Fact]
Expand Down

0 comments on commit f27382a

Please sign in to comment.