Skip to content

Conversation

@GerardSmit
Copy link
Contributor

This PR detects never ending loops after the method was changed to synchronous code.

Example

From the file WhileNotCancelled.cs in GenerationSandbox.Tests:

using System;
using System.Threading;
using System.Threading.Tasks;

namespace GenerationSandbox.Tests;

internal static partial class WhileNotCancelled
{
    [Zomp.SyncMethodGenerator.CreateSyncVersion]
    public static async ValueTask SleepAsync(CancellationToken ct)
    {
        while (!ct.IsCancellationRequested)
        {
            await Task.Delay(120000, ct);
        }

        throw new OperationCanceledException();
    }
}

Before

The synchronized version of this code will never end, because !ct.IsCancellationRequested gets replaced with true. The last statement (throw) will also show an error that it'll never be reached:

image

After

In the new version the last statement gets removed:

public static void Sleep()
{
    while (true)
    {
        global::System.Threading.Thread.Sleep(120000);
    }
}

And a new diagnostic is added (ZSMGEN004) telling the user that it's detected that the loop will never end:

WhileNotCancelled.cs(12,9): Warning ZSMGEN004 : It is detected that the while loop will never end after transforming to synchronous version

Please validate if the error message is correct or should be changed:

1656a54#diff-59a20cb680fe0e33b6def06d74d7c85b3e090d4c9b716668e145f74715af9fecR29-R35

@virzak virzak force-pushed the master branch 2 times, most recently from 3dfda17 to b1a312d Compare July 8, 2024 02:54
@virzak virzak force-pushed the master branch 3 times, most recently from f25c292 to eafebd1 Compare March 10, 2025 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant