Skip to content

Warn when a synchronized loop never ends - #69

Merged
virzak merged 4 commits into
zompinc:masterfrom
GerardSmit:feature/detect-never-ending-loops
Sep 12, 2026
Merged

virzak merged 4 commits into
zompinc:masterfrom
GerardSmit:feature/detect-never-ending-loops

Conversation

@GerardSmit

Copy link
Copy Markdown
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
GerardSmit and others added 4 commits September 12, 2026 11:13
Reports ZSMGEN006, since master already uses ZSMGEN004, through
master's statement processing and diagnostics. Judges the loop on its
rewritten body, counts throw statements, ignores a break belonging to
a nested switch, leaves hand-written while (true) loops alone, and
stays quiet once an #if SYNC_ONLY region replaces the loop.

Generated with Claude Code
@virzak
virzak force-pushed the feature/detect-never-ending-loops branch from c49b59f to 3cfcd3f Compare September 12, 2026 15:14
@virzak

virzak commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

Rebased onto master and reworked on top: the warning is now ZSMGEN006 (004 is taken), judged on the rewritten loop body, and it no longer fires for hand-written while (true) or once #if SYNC_ONLY replaces the loop.

@virzak virzak changed the title Detect never ending loops Warn when a synchronized loop never ends Sep 12, 2026
@virzak
virzak merged commit 65f333f into zompinc:master Sep 12, 2026
4 checks passed
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.

2 participants