Skip to content

fix try/finally rethrow when try block has return paths#1699

Open
RealColdFry wants to merge 1 commit intoTypeScriptToLua:masterfrom
RealColdFry:fix/try-finally-rethrow-with-return
Open

fix try/finally rethrow when try block has return paths#1699
RealColdFry wants to merge 1 commit intoTypeScriptToLua:masterfrom
RealColdFry:fix/try-finally-rethrow-with-return

Conversation

@RealColdFry
Copy link
Copy Markdown
Contributor

@RealColdFry RealColdFry commented Mar 27, 2026

Fixes a regression from #1692 where try/finally (no catch) with both return and throw paths silently lost the error. The re-throw used an undeclared ____error variable; use ____hasReturned instead, which is where pcall places the error on failure.

Fixes a regression from TypeScriptToLua#1692 where try/finally (no catch) with both
return and throw paths silently lost the error. The re-throw used an
undeclared ____error variable; use ____hasReturned instead, which is
where pcall places the error on failure.
@RealColdFry RealColdFry marked this pull request as ready for review March 27, 2026 17:48
Copy link
Copy Markdown
Member

@lolleko lolleko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes sense and it's good we have a lot of tests for this, as there are so many edge cases.
I would even at at least one more tests and double check we really have all combinations covered by tests.
So this is less likely to regress again.

try { foo(); return "no error"; } catch(e) { return e; }
`.expectToMatchJsResult();
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have one more test for return + throw + non-empty finally body

function foo(shouldReturn: boolean) {
    try {
        if (shouldReturn) return "ok";
        throw "err";
    } finally {
        sideEffect = true; // must still run on both paths
    }
}

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