diff options
author | Bram Moolenaar <Bram@vim.org> | 2021-11-22 18:31:02 +0000 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2021-11-22 18:31:02 +0000 |
commit | 7a53f29c031f54ab67a803e5e3f8cb44e4edc4bc (patch) | |
tree | 7d2a143dc72f6fe415c7efba4d79faad3e413e1c | |
parent | 4671e88d7d3ed12206d9cdd8892fe3b2cbc0d6ab (diff) | |
download | vim-git-7a53f29c031f54ab67a803e5e3f8cb44e4edc4bc.tar.gz |
patch 8.2.3649: Vim9: error for variable declared in while loopv8.2.3649
Problem: Vim9: error for variable declared in while loop.
Solution: Do not keep the first variable. (closes #9191)
-rw-r--r-- | src/ex_eval.c | 7 | ||||
-rw-r--r-- | src/testdir/test_vim9_script.vim | 15 | ||||
-rw-r--r-- | src/version.c | 2 |
3 files changed, 21 insertions, 3 deletions
diff --git a/src/ex_eval.c b/src/ex_eval.c index d80500eb0..4e38e9649 100644 --- a/src/ex_eval.c +++ b/src/ex_eval.c @@ -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; diff --git a/src/testdir/test_vim9_script.vim b/src/testdir/test_vim9_script.vim index fe9abe418..ca5d7beb0 100644 --- a/src/testdir/test_vim9_script.vim +++ b/src/testdir/test_vim9_script.vim @@ -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:') diff --git a/src/version.c b/src/version.c index bd4114780..3851e8043 100644 --- a/src/version.c +++ b/src/version.c @@ -758,6 +758,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 3649, +/**/ 3648, /**/ 3647, |