]> granicus.if.org Git - vim/commitdiff
patch 8.2.3649: Vim9: error for variable declared in while loop v8.2.3649
authorBram Moolenaar <Bram@vim.org>
Mon, 22 Nov 2021 18:31:02 +0000 (18:31 +0000)
committerBram Moolenaar <Bram@vim.org>
Mon, 22 Nov 2021 18:31:02 +0000 (18:31 +0000)
Problem:    Vim9: error for variable declared in while loop.
Solution:   Do not keep the first variable. (closes #9191)

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

index d80500eb0b606982840843a64ace8e0039bf4c95..4e38e964917742184f4113a2f66a34ecf1b3f066 100644 (file)
@@ -1201,9 +1201,10 @@ ex_while(exarg_T *eap)
                                                                & CSF_FUNC_DEF;
 
                // Any variables defined in the previous round are no longer
-               // visible.  Keep the first one, it is the loop variable that
-               // we reuse every time around.
-               for (i = cstack->cs_script_var_len[cstack->cs_idx] + 1;
+               // visible.  Keep the first one for ":for", it is the loop
+               // variable that we reuse every time around.
+               for (i = cstack->cs_script_var_len[cstack->cs_idx]
+                                         + (eap->cmdidx == CMD_while ? 0 : 1);
                                               i < si->sn_var_vals.ga_len; ++i)
                {
                    svar_T      *sv = ((svar_T *)si->sn_var_vals.ga_data) + i;
index fe9abe418f36b4810e82310c86dec6d3fcaef1fc..ca5d7beb08498aa8aa7bdb0261ba0f28c61562a9 100644 (file)
@@ -3083,6 +3083,21 @@ def Test_while_loop()
   endwhile
 enddef
 
+def Test_while_loop_in_script()
+  var lines =<< trim END
+      vim9script
+      var result = ''
+      var cnt = 0
+      while cnt < 3
+        var s = 'v' .. cnt
+        result ..= s
+        cnt += 1
+      endwhile
+      assert_equal('v0v1v2', result)
+  END
+  CheckScriptSuccess(lines)
+enddef
+
 def Test_while_loop_fails()
   CheckDefFailure(['while xxx'], 'E1001:')
   CheckDefFailure(['endwhile'], 'E588:')
index bd4114780186977754f1157431315b2e3c36b808..3851e8043f2453ba1fb6fbb596b8b7cf021a9532 100644 (file)
@@ -757,6 +757,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    3649,
 /**/
     3648,
 /**/