Patch 9.0.0535
Problem: Closure gets wrong value in for loop with two loop variables.
Solution: Correctly compute the number of loop variables to clear.
Files: src/ex_eval.c, src/structs.h, src/evalvars.c,
src/testdir/test_vim9_script.vim
*** ../vim-9.0.0534/src/ex_eval.c 2022-09-14 00:30:47.077316538 +0100
--- src/ex_eval.c 2022-09-21 18:50:01.531630518 +0100
***************
*** 1240,1247 ****
// variable that we reuse every time around.
// 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);
for (i = si->sn_var_vals.ga_len - 1; i >= first; --i)
{
svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + i;
--- 1240,1253 ----
// variable that we reuse every time around.
// Do this backwards, so that vars defined in a later round are
// found first.
! first = cstack->cs_script_var_len[cstack->cs_idx];
! if (eap->cmdidx == CMD_for)
! {
! forinfo_T *fi = cstack->cs_forinfo[cstack->cs_idx];
!
! first += fi == NULL || fi->fi_varcount == 0
! ? 1 : fi->fi_varcount;
! }
for (i = si->sn_var_vals.ga_len - 1; i >= first; --i)
{
svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + i;
*** ../vim-9.0.0534/src/structs.h 2022-09-19 15:54:29.539117880 +0100
--- src/structs.h 2022-09-21 18:48:07.451767613 +0100
***************
*** 1630,1636 ****
typedef struct
{
int fi_semicolon; // TRUE if ending in '; var]'
! int fi_varcount; // nr of variables in the list
int fi_break_count; // nr of line breaks encountered
listwatch_T fi_lw; // keep an eye on the item used.
list_T *fi_list; // list being used
--- 1630,1636 ----
typedef struct
{
int fi_semicolon; // TRUE if ending in '; var]'
! int fi_varcount; // nr of variables in [] or zero
int fi_break_count; // nr of line breaks encountered
listwatch_T fi_lw; // keep an eye on the item used.
list_T *fi_list; // list being used
*** ../vim-9.0.0534/src/evalvars.c 2022-09-14 00:30:47.081316534 +0100
--- src/evalvars.c 2022-09-21 18:47:19.975828053 +0100
***************
*** 1315,1322 ****
}
return p + 1;
}
! else
! return skip_var_one(arg, include_type);
}
/*
--- 1315,1322 ----
}
return p + 1;
}
!
! return skip_var_one(arg, include_type);
}
/*
*** ../vim-9.0.0534/src/testdir/test_vim9_script.vim 2022-09-19
15:54:29.543117874 +0100
--- src/testdir/test_vim9_script.vim 2022-09-21 18:55:06.827305603 +0100
***************
*** 2323,2328 ****
--- 2323,2349 ----
endfor
END
v9.CheckDefAndScriptSuccess(lines)
+
+ # using two loop variables
+ lines =<< trim END
+ var lv_list: list<func>
+ var copy_list: list<func>
+ for [idx, c] in items('word')
+ var lidx = idx
+ var lc = c
+ lv_list[idx] = () => {
+ return idx .. c
+ }
+ copy_list[idx] = () => {
+ return lidx .. lc
+ }
+ endfor
+ for [i, c] in items('word')
+ assert_equal(3 .. 'd', lv_list[i]())
+ assert_equal(i .. c, copy_list[i]())
+ endfor
+ END
+ v9.CheckDefAndScriptSuccess(lines)
enddef
def Test_define_global_closure_in_loops()
*** ../vim-9.0.0534/src/version.c 2022-09-21 16:38:08.873814058 +0100
--- src/version.c 2022-09-21 18:34:10.188954353 +0100
***************
*** 701,702 ****
--- 701,704 ----
{ /* Add new patch number below this line */
+ /**/
+ 535,
/**/
--
hundred-and-one symptoms of being an internet addict:
125. You begin to wonder how often it REALLY is necessary to get up
and shower or bathe.
/// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/vim_dev/20220921175949.44AF51C0F2E%40moolenaar.net.