Skip to content

Commit 9cb577a

Browse files
committed
patch 8.2.2543: Vim9: a return inside try/catch does not restore properly
Problem: Vim9: a return inside try/catch does not restore exception state properly. Solution: When there is no ":finally" jump to ":endtry". (closes #7882)
1 parent 41f0895 commit 9cb577a

3 files changed

Lines changed: 20 additions & 4 deletions

File tree

src/testdir/test_vim9_script.vim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,18 @@ def Test_try_catch_throw()
589589
assert_equal(4, ReturnInFinally())
590590
enddef
591591

592+
def Test_nocatch_return_in_try()
593+
# return in try block returns normally
594+
def ReturnInTry(): string
595+
try
596+
return '"some message"'
597+
catch
598+
endtry
599+
return 'not reached'
600+
enddef
601+
exe 'echoerr ' .. ReturnInTry()
602+
enddef
603+
592604
def Test_cnext_works_in_catch()
593605
var lines =<< trim END
594606
vim9script

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ static char *(features[]) =
750750

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2543,
753755
/**/
754756
2542,
755757
/**/

src/vim9execute.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2517,11 +2517,13 @@ call_def_function(
25172517
trycmd = ((trycmd_T *)trystack->ga_data)
25182518
+ trystack->ga_len - 1;
25192519
if (trycmd != NULL
2520-
&& trycmd->tcd_frame_idx == ectx.ec_frame_idx
2521-
&& trycmd->tcd_finally_idx != 0)
2520+
&& trycmd->tcd_frame_idx == ectx.ec_frame_idx)
25222521
{
2523-
// jump to ":finally" once
2524-
ectx.ec_iidx = trycmd->tcd_finally_idx;
2522+
// jump to ":finally" or ":endtry"
2523+
if (trycmd->tcd_finally_idx != 0)
2524+
ectx.ec_iidx = trycmd->tcd_finally_idx;
2525+
else
2526+
ectx.ec_iidx = trycmd->tcd_endtry_idx;
25252527
trycmd->tcd_return = TRUE;
25262528
}
25272529
else

0 commit comments

Comments
 (0)