diff options
author | Bram Moolenaar <Bram@vim.org> | 2022-09-13 21:10:45 +0100 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2022-09-13 21:10:45 +0100 |
commit | 353b68a99189875a8460124d44fc33eae6def74e (patch) | |
tree | 36d318c0420bfdf050766b9bcaa5dc4ce433c770 /src | |
parent | 3b93cf218fc70897c11de0415221e7899e9a527a (diff) |
patch 9.0.0459: Vim9: block in for loop doesn't behave like a code blockv9.0.0459
Problem: Vim9: block in for loop doesn't behave like a code block.
Solution: Use a new block ID for each loop at the script level.
Diffstat (limited to 'src')
-rw-r--r-- | src/ex_eval.c | 13 | ||||
-rw-r--r-- | src/testdir/test_vim9_script.vim | 12 | ||||
-rw-r--r-- | src/version.c | 2 |
3 files changed, 21 insertions, 6 deletions
diff --git a/src/ex_eval.c b/src/ex_eval.c index 2f31b3e1c5..4315c2a62c 100644 --- a/src/ex_eval.c +++ b/src/ex_eval.c @@ -1230,15 +1230,18 @@ ex_while(exarg_T *eap) { scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); int i; + int first; int func_defined = cstack->cs_flags[cstack->cs_idx] & CSF_FUNC_DEF; // Any variables defined in the previous round are no longer // 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] + // Do this backwards, so that vars defined in a later round are + // found first. + first = cstack->cs_script_var_len[cstack->cs_idx] + (eap->cmdidx == CMD_while ? 0 : 1); - i < si->sn_var_vals.ga_len; ++i) + for (i = si->sn_var_vals.ga_len - 1; i >= first; --i) { svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + i; @@ -1250,6 +1253,12 @@ ex_while(exarg_T *eap) // still exists, from sn_vars. hide_script_var(si, i, func_defined); } + + // Start a new block ID, so that variables defined inside the + // loop are created new and not shared with the previous loop. + // Matters when used in a closure. + cstack->cs_block_id[cstack->cs_idx] = ++si->sn_last_block_id; + si->sn_current_block_id = si->sn_last_block_id; } } cstack->cs_flags[cstack->cs_idx] = diff --git a/src/testdir/test_vim9_script.vim b/src/testdir/test_vim9_script.vim index f746ca1782..fa9bb0cc20 100644 --- a/src/testdir/test_vim9_script.vim +++ b/src/testdir/test_vim9_script.vim @@ -2266,10 +2266,12 @@ def Test_for_loop_with_closure() flist[i] = () => inloop endfor for i in range(5) - assert_equal(4, flist[i]()) + assert_equal(i, flist[i]()) endfor END - v9.CheckDefAndScriptSuccess(lines) + # FIXME + # v9.CheckDefAndScriptSuccess(lines) + v9.CheckScriptSuccess(['vim9script'] + lines) lines =<< trim END var flist: list<func> @@ -2280,10 +2282,12 @@ def Test_for_loop_with_closure() } endfor for i in range(5) - assert_equal(4, flist[i]()) + assert_equal(i, flist[i]()) endfor END - v9.CheckDefAndScriptSuccess(lines) + # FIXME + # v9.CheckDefAndScriptSuccess(lines) + v9.CheckScriptSuccess(['vim9script'] + lines) enddef def Test_for_loop_fails() diff --git a/src/version.c b/src/version.c index 25c28f847b..dede808bb5 100644 --- a/src/version.c +++ b/src/version.c @@ -704,6 +704,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 459, +/**/ 458, /**/ 457, |