]> granicus.if.org Git - vim/commitdiff
patch 8.2.4499: Vim9: at the script level declarations leak to next block v8.2.4499
authorBram Moolenaar <Bram@vim.org>
Thu, 3 Mar 2022 15:11:20 +0000 (15:11 +0000)
committerBram Moolenaar <Bram@vim.org>
Thu, 3 Mar 2022 15:11:20 +0000 (15:11 +0000)
Problem:    Vim9: at the script level declarations leak from try block to
            catch and finally block.
Solution:   End the block and start a new one. (closes #9883)

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

index ab30798622dc4f0bc2fbe191edcd17553e1d5df9..e3c544b892ab72ae6dd775dbe8ff99631ed9accd 100644 (file)
@@ -1827,6 +1827,16 @@ ex_catch(exarg_T *eap)
            cstack->cs_flags[idx] |= CSF_ACTIVE | CSF_CAUGHT;
            did_emsg = got_int = did_throw = FALSE;
            catch_exception((except_T *)cstack->cs_exception[idx]);
+
+           if (cstack->cs_idx >= 0
+                              && (cstack->cs_flags[cstack->cs_idx] & CSF_TRY))
+           {
+               // Variables declared in the previous block can no longer be
+               // used.
+               leave_block(cstack);
+               enter_block(cstack);
+           }
+
            // It's mandatory that the current exception is stored in the cstack
            // so that it can be discarded at the next ":catch", ":finally", or
            // ":endtry" or when the catch clause is left by a ":continue",
@@ -1930,6 +1940,15 @@ ex_finally(exarg_T *eap)
             */
            cleanup_conditionals(cstack, CSF_TRY, FALSE);
 
+           if (cstack->cs_idx >= 0
+                              && (cstack->cs_flags[cstack->cs_idx] & CSF_TRY))
+           {
+               // Variables declared in the previous block can no longer be
+               // used.
+               leave_block(cstack);
+               enter_block(cstack);
+           }
+
            /*
             * Make did_emsg, got_int, did_throw pending.  If set, they overrule
             * a pending ":continue", ":break", ":return", or ":finish".  Then
index 3c3434d14228c4705969392d35933ad55d729f79..68178cfc68a50c33db4526821c3a946e7be9c9a1 100644 (file)
@@ -763,6 +763,30 @@ def Test_try_catch_throw()
   v9.CheckDefAndScriptSuccess(lines)
 enddef
 
+def Test_try_var_decl()
+  var lines =<< trim END
+      vim9script
+      try
+        var in_try = 1
+        assert_equal(1, get(s:, 'in_try', -1))
+        throw "getout"
+      catch
+        var in_catch = 2
+        assert_equal(-1, get(s:, 'in_try', -1))
+        assert_equal(2, get(s:, 'in_catch', -1))
+      finally
+        var in_finally = 3
+        assert_equal(-1, get(s:, 'in_try', -1))
+        assert_equal(-1, get(s:, 'in_catch', -1))
+        assert_equal(3, get(s:, 'in_finally', -1))
+      endtry
+      assert_equal(-1, get(s:, 'in_try', -1))
+      assert_equal(-1, get(s:, 'in_catch', -1))
+      assert_equal(-1, get(s:, 'in_finally', -1))
+  END
+  v9.CheckScriptSuccess(lines)
+enddef
+
 def Test_try_ends_in_return()
   var lines =<< trim END
       vim9script
index 6131427f8b258f82af7043c92a8301a07bbc51f0..c6a9f73d2dd2328d9a7aaea2d1038604bce2a624 100644 (file)
@@ -754,6 +754,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    4499,
 /**/
     4498,
 /**/