Skip to content
Merged
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
21 changes: 18 additions & 3 deletions src/Zomp.SyncMethodGenerator/AsyncToSyncRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,24 @@ _ when GetReplacement(symbol) is { } name => @base.WithIdentifier(Identifier(Glo

/// <inheritdoc/>
public override SyntaxNode? VisitIdentifierName(IdentifierNameSyntax node)
=> renamedLocalFunctions.TryGetValue(node.Identifier.ValueText, out var newName)
? node.WithIdentifier(Identifier(newName))
: base.VisitIdentifierName(node);
{
if (renamedLocalFunctions.TryGetValue(node.Identifier.ValueText, out var newName))
{
return node.WithIdentifier(Identifier(newName));
}

// A statement or an argument which mentions a removed CancellationToken is dropped whole,
// but an expression such as a catch filter has to keep its shape. CancellationToken.None
// stands in for the parameter: it is never cancelled, which is what the synchronized
// method does.
if (GetSymbol(node) is IParameterSymbol { Type: INamedTypeSymbol { IsCancellationToken: true } } parameter
&& removedParameters.Contains(parameter))
{
return IdentifierName(Global("System.Threading.CancellationToken.None")).WithTriviaFrom(node);
}

return base.VisitIdentifierName(node);
}

public override SyntaxNode? VisitLocalFunctionStatement(LocalFunctionStatementSyntax node)
{
Expand Down
43 changes: 43 additions & 0 deletions tests/Generator.Tests/IsCancellationRequestedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,47 @@ public Task WhileNotCancelledReplacedBySyncOnly() => """
}
#endif
""".Verify(sourceType: SourceType.MethodBody);

[Fact]
public Task CatchFilterChecksCancellation() => """
try
{
await Task.Delay(1, ct);
}
catch (OperationCanceledException) when (ct.IsCancellationRequested)
{
throw;
}
catch (Exception)
{
}
""".Verify(sourceType: SourceType.MethodBody);

[Fact]
public Task CatchFilterChecksCancellationAndMore() => """
try
{
await Task.Delay(1, ct);
}
catch (OperationCanceledException) when (ct.IsCancellationRequested || Environment.TickCount > 0)
{
throw;
}
""".Verify(sourceType: SourceType.MethodBody);

[Fact]
public Task CatchFilterKeepsPreservedToken() => """
[CreateSyncVersion(PreserveCancellationToken = true)]
public async Task MethodAsync(CancellationToken ct)
{
try
{
await Task.Delay(1, ct);
}
catch (OperationCanceledException) when (ct.IsCancellationRequested)
{
throw;
}
}
""".Verify();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//HintName: Test.Class.MethodAsync.g.cs
try
{
global::System.Threading.Thread.Sleep(1);
}
catch (global::System.OperationCanceledException) when (global::System.Threading.CancellationToken.None.IsCancellationRequested)
{
throw;
}
catch (global::System.Exception)
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//HintName: Test.Class.MethodAsync.g.cs
try
{
global::System.Threading.Thread.Sleep(1);
}
catch (global::System.OperationCanceledException) when (global::System.Threading.CancellationToken.None.IsCancellationRequested || global::System.Environment.TickCount > 0)
{
throw;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//HintName: Test.Class.MethodAsync.g.cs
public void Method(global::System.Threading.CancellationToken ct)
{
try
{
global::System.Threading.Thread.Sleep(1);
}
catch (global::System.OperationCanceledException) when (ct.IsCancellationRequested)
{
throw;
}
}
Loading