Skip to content

Commit

Permalink
Fix control flow from catch block if try block returns and catch doesn't
Browse files Browse the repository at this point in the history
  • Loading branch information
Sainan committed Dec 3, 2023
1 parent 5171392 commit 51c5ee1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/lparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4662,6 +4662,12 @@ static void trystat (LexState *ls) {

check_match(ls, TK_END, TK_PCATCH, then_line);
leaveblock(ls->fs);

int escapelist = NO_JUMP;
if (!prop.empty()) { /* try block has return statement? */
luaK_concat(ls->fs, &escapelist, luaK_jump(ls->fs)); /* catch block has to skip over the try-return code */
}

luaK_patchtohere(ls->fs, econd.t);

if (!prop.empty()) { /* try block has return statement? */
Expand All @@ -4684,6 +4690,8 @@ static void trystat (LexState *ls) {
int first = luaY_nvarstack(ls->fs); /* first slot to be returned */
luaK_setmultret(ls->fs, &tunpack);
luaK_ret(ls->fs, first, LUA_MULTRET);

luaK_patchtohere(ls->fs, escapelist);
}
}

Expand Down
11 changes: 11 additions & 0 deletions testes/pluto/basic.pluto
Original file line number Diff line number Diff line change
Expand Up @@ -1857,6 +1857,17 @@ do
end
assert(ensure_error(error, { what = "err" }).what == "err")
end
do
-- If try block returns, catch block needs to jump over the try-return code (in case it doesn't return.)
local function f()
try
return error("deez")
catch e then
end
return true
end
assert(f() == true)
end

print "Testing compatibility."
do
Expand Down

0 comments on commit 51c5ee1

Please sign in to comment.