]> granicus.if.org Git - vim/commitdiff
patch 8.2.2543: Vim9: a return inside try/catch does not restore properly v8.2.2543
authorBram Moolenaar <Bram@vim.org>
Mon, 22 Feb 2021 21:45:10 +0000 (22:45 +0100)
committerBram Moolenaar <Bram@vim.org>
Mon, 22 Feb 2021 21:45:10 +0000 (22:45 +0100)
Problem:    Vim9: a return inside try/catch does not restore exception state
            properly.
Solution:   When there is no ":finally" jump to ":endtry". (closes #7882)

src/testdir/test_vim9_script.vim
src/version.c
src/vim9execute.c

index 0fb09fd817a7e6f5838d0b495b25e7cbfb7b39a2..a262bcb62e98ab7e72404e50b8eb24372c1cf3a3 100644 (file)
@@ -589,6 +589,18 @@ def Test_try_catch_throw()
   assert_equal(4, ReturnInFinally())
 enddef
 
+def Test_nocatch_return_in_try()
+  # return in try block returns normally
+  def ReturnInTry(): string
+    try
+      return '"some message"'
+    catch
+    endtry
+    return 'not reached'
+  enddef
+  exe 'echoerr ' .. ReturnInTry()
+enddef
+
 def Test_cnext_works_in_catch()
   var lines =<< trim END
       vim9script
index 757a14dfdd35313a596dce7948fe31316d0e75be..235e344926665771525ebff22b5b90cfb38bbd14 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2543,
 /**/
     2542,
 /**/
index d2cc62f9ae267f83c6924f4e18abb4f1f218274b..232f0471ea50b227046f1324b31b3dbfd9c1f3d3 100644 (file)
@@ -2517,11 +2517,13 @@ call_def_function(
                        trycmd = ((trycmd_T *)trystack->ga_data)
                                                        + trystack->ga_len - 1;
                    if (trycmd != NULL
-                                 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
-                                 && trycmd->tcd_finally_idx != 0)
+                                && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
                    {
-                       // jump to ":finally" once
-                       ectx.ec_iidx = trycmd->tcd_finally_idx;
+                       // jump to ":finally" or ":endtry"
+                       if (trycmd->tcd_finally_idx != 0)
+                           ectx.ec_iidx = trycmd->tcd_finally_idx;
+                       else
+                           ectx.ec_iidx = trycmd->tcd_endtry_idx;
                        trycmd->tcd_return = TRUE;
                    }
                    else