Skip to content

[Tolk] Allow break/continue in a loop nested inside try/catch - #2581

Open
chiliec wants to merge 1 commit into
ton-blockchain:masterfrom
chiliec:tolk-break-in-loop-inside-try
Open

chiliec wants to merge 1 commit into
ton-blockchain:masterfrom
chiliec:tolk-break-in-loop-inside-try

Conversation

@chiliec

@chiliec chiliec commented Oct 5, 2026

Copy link
Copy Markdown

Tolk 1.5 (#2577) rejects break/continue when the whole loop sits inside a try block, even though the jump only leaves that inner loop and never crosses the try boundary:

fun f(n: int): int {
    var i = 0;
    try {
        while (true) {
            i += 1;
            if (i == n) { break }   // error: `break` is not allowed inside `try/catch`
        }
    } catch {
        i = -1;
    }
    return i;
}

The same thing happens for continue, for repeat/do-while, and for a loop inside catch.

The cause is in LoopBreakContinueValidator: inside_try_catch is never reset when a new loop starts, so a try outside the loop counts the same as a try inside it. The fix saves the counter, resets it to 0 for the loop body, and restores it afterwards. A break in a try that is inside the loop is still rejected, as before.

Tests:

  • break-continue-tests.tolk: 5 runtime cases (179–183). They cover break and continue in a loop inside try, break in a loop inside catch, a throw after a breakable loop inside try (the catch still sees the right values), and a try with a breakable loop nested in an outer breakable loop.
  • err-break-continue.tolk: try { while { try { break } } } is still a compile error.

I ran the full tolk-tester suite locally with and without the change. The only difference is break-continue-tests.tolk: it fails without the fix because of the new cases, and passes with it (107 cases).

The try/catch depth was not reset when entering a loop, so a break or
continue that only exits a loop sitting entirely inside a try body was
rejected as "not allowed inside try/catch". Reset the depth per loop
and restore it afterwards; break/continue inside a try that is itself
inside the loop is still rejected.
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