]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 8.2.4873: Vim9: using "else" differs from using "endif/if !cond" v8.2.4873
authorBram Moolenaar <Bram@vim.org>
Thu, 5 May 2022 16:02:46 +0000 (17:02 +0100)
committerBram Moolenaar <Bram@vim.org>
Thu, 5 May 2022 16:02:46 +0000 (17:02 +0100)
Problem:    Vim9: using "else" differs from using "endif/if !cond".
Solution:   Leave the block and enter another one. (closes #10320)

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

index e9dbd107c85cdac5226b5f9f7384efde301b1a51..7b4ac1750dd37e254a5db32e1a7624f571eea4f2 100644 (file)
@@ -1124,6 +1124,11 @@ ex_else(exarg_T *eap)
        skip = TRUE;
     }
 
+    // Variables declared in the previous block can no longer be
+    // used.  Needs to be done before setting "cs_flags".
+    leave_block(cstack);
+    enter_block(cstack);
+
     // if skipping or the ":if" was TRUE, reset ACTIVE, otherwise set it
     if (skip || cstack->cs_flags[cstack->cs_idx] & CSF_TRUE)
     {
index 4fa6904509713bae5d88a01bdb44dd18a7d8465c..8811382e783d099e71ee9766f6b0f762fa1401ab 100644 (file)
@@ -1640,6 +1640,50 @@ def Test_if_elseif_else_fails()
   v9.CheckDefAndScriptFailure(lines, ['E1143:', 'E15:'], 4)
 enddef
 
+def Test_if_else_func_using_var()
+  var lines =<< trim END
+      vim9script
+
+      const debug = true
+      if debug
+        var mode_chars = 'something'
+        def Bits2Ascii()
+          var x = mode_chars
+          g:where = 'in true'
+        enddef
+      else
+        def Bits2Ascii()
+          g:where = 'in false'
+        enddef
+      endif
+
+      Bits2Ascii()
+  END
+  v9.CheckScriptSuccess(lines)
+  assert_equal('in true', g:where)
+  unlet g:where
+
+  lines =<< trim END
+      vim9script
+
+      const debug = false
+      if debug
+        var mode_chars = 'something'
+        def Bits2Ascii()
+          g:where = 'in true'
+        enddef
+      else
+        def Bits2Ascii()
+          var x = mode_chars
+          g:where = 'in false'
+        enddef
+      endif
+
+      Bits2Ascii()
+  END
+  v9.CheckScriptFailure(lines, 'E1001: Variable not found: mode_chars')
+enddef
+
 let g:bool_true = v:true
 let g:bool_false = v:false
 
index 5ad75510f8788c3323a3e19979b56b3418720a9d..3242c1456250c069e5298bab67c0d943096486cc 100644 (file)
@@ -746,6 +746,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    4873,
 /**/
     4872,
 /**/