Skip to content

Commit

Permalink
Fix #72 Drop configurewAwait from and await using from IAsyncDisposable
Browse files Browse the repository at this point in the history
  • Loading branch information
virzak committed Apr 11, 2024
1 parent 2bc3cc9 commit 692f3ca
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageVersion Include="System.Linq.Async" Version="6.0.1" />
<PackageVersion Include="System.Runtime.Experimental" Version="6.0.2" />
<PackageVersion Include="Verify.SourceGenerators" Version="2.2.0" />
<PackageVersion Include="Verify.XUnit" Version="23.5.2" />
<PackageVersion Include="Verify.XUnit" Version="23.7.2" />
<PackageVersion Include="xunit" Version="2.7.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.7" />
</ItemGroup>
Expand Down
12 changes: 11 additions & 1 deletion src/Zomp.SyncMethodGenerator/AsyncToSyncRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ internal sealed class AsyncToSyncRewriter(SemanticModel semanticModel) : CSharpS
private const string ConfiguredTaskAwaitable = "System.Runtime.CompilerServices.ConfiguredTaskAwaitable";
private const string ConfiguredValueTaskAwaitable = "System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable";
private const string ConfiguredCancelableAsyncEnumerable = "System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable";
private const string ConfiguredAsyncDisposable = "System.Runtime.CompilerServices.ConfiguredAsyncDisposable";
private const string IAsyncEnumerator = "System.Collections.Generic.IAsyncEnumerator";
private const string FromResult = "FromResult";
private const string AsTask = "AsTask";
Expand Down Expand Up @@ -485,6 +486,12 @@ List<SyntaxTrivia> RemoveFirstEndIf(SyntaxTriviaList list)
return @base;
}

public override SyntaxNode? VisitUsingStatement(UsingStatementSyntax node)
{
var @base = (UsingStatementSyntax)base.VisitUsingStatement(node)!;
return @base.WithAwaitKeyword(default);
}

/// <inheritdoc/>
public override SyntaxNode? VisitInvocationExpression(InvocationExpressionSyntax node)
{
Expand Down Expand Up @@ -1539,7 +1546,10 @@ private static ExpressionSyntax RemoveParentheses(ExpressionSyntax condition)
private static bool IsTaskExtension(IMethodSymbol methodSymbol)
{
var returnType = GetNameWithoutTypeParams(methodSymbol.ReturnType);
return returnType is ConfiguredTaskAwaitable or ConfiguredValueTaskAwaitable or ConfiguredCancelableAsyncEnumerable;
return returnType is ConfiguredTaskAwaitable
or ConfiguredValueTaskAwaitable
or ConfiguredCancelableAsyncEnumerable
or ConfiguredAsyncDisposable;
}

private static bool CanDropEmptyStatement(StatementSyntax statement)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<NoWarn>$(NoWarn);CS0219;CS0162;CS1998;CS8603;CS8619</NoWarn>
<NoWarn>$(NoWarn);IDE0035</NoWarn>
<NoWarn>$(NoWarn);RS1035</NoWarn>
<NoWarn>$(NoWarn);SA1201;SA1402;SA1404</NoWarn>
<NoWarn>$(NoWarn);SA1201;SA1400;SA1402;SA1404</NoWarn>
<ImplicitUsings>false</ImplicitUsings>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">$(TargetFrameworks);net472</TargetFrameworks>
Expand Down
19 changes: 19 additions & 0 deletions tests/Generator.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,25 @@ public static async Task WriteAllTextAsync(string file, string contents,
""".Verify();
#endif

[Fact]
public Task PassIAsyncDisposable() => """

class ImplementsBothDisposables : IDisposable, IAsyncDisposable
{
public void Dispose() => throw new NotImplementedException();

public ValueTask DisposeAsync() => throw new NotImplementedException();
}

[CreateSyncVersion]
async Task MethodAsync(ImplementsBothDisposables a)
{
await using (a.ConfigureAwait(false))
{
}
}
""".Verify();

[Fact]
public Task CallWithTypeParameters() => """
[CreateSyncVersion]
Expand Down

0 comments on commit 692f3ca

Please sign in to comment.