Patch 9.0.0495
Problem:    Closure doesn't work properly in nested loop.
Solution:   Save variables up to the outer loop.
Files:      src/vim9execute.c, src/testdir/test_vim9_script.vim


*** ../vim-9.0.0494/src/vim9execute.c   2022-09-17 21:07:52.103993150 +0100
--- src/vim9execute.c   2022-09-18 11:19:16.428456980 +0100
***************
*** 2671,2677 ****
      {
        partial_T   *pt = ((partial_T **)gap->ga_data)[idx];
  
!       if (pt->pt_refcount > 1)
        {
            int refcount = pt->pt_refcount;
            int i;
--- 2671,2677 ----
      {
        partial_T   *pt = ((partial_T **)gap->ga_data)[idx];
  
!       if (pt->pt_refcount > 1 && pt->pt_loopvars == NULL)
        {
            int refcount = pt->pt_refcount;
            int i;
***************
*** 2727,2733 ****
      {
        partial_T   *pt = ((partial_T **)gap->ga_data)[idx];
  
!       if (pt->pt_refcount > 1)
        {
            ++loopvars->lvs_refcount;
            pt->pt_loopvars = loopvars;
--- 2727,2733 ----
      {
        partial_T   *pt = ((partial_T **)gap->ga_data)[idx];
  
!       if (pt->pt_refcount > 1 && pt->pt_loopvars == NULL)
        {
            ++loopvars->lvs_refcount;
            pt->pt_loopvars = loopvars;
*** ../vim-9.0.0494/src/testdir/test_vim9_script.vim    2022-09-17 
15:44:48.369409492 +0100
--- src/testdir/test_vim9_script.vim    2022-09-18 11:43:34.681092655 +0100
***************
*** 2300,2305 ****
--- 2300,2331 ----
        endfor
    END
    v9.CheckDefAndScriptSuccess(lines)
+ 
+   # Also works for a nested loop
+   lines =<< trim END
+       var flist: list<func>
+       var n = 0
+       for i in range(3)
+         var ii = i
+         for a in ['a', 'b', 'c']
+           var aa = a
+           flist[n] = () => ii .. aa
+           ++n
+         endfor
+       endfor
+ 
+       n = 0
+       for i in range(3)
+         for a in ['a', 'b', 'c']
+           assert_equal(i .. a, flist[n]())
+           ++n
+         endfor
+       endfor
+   END
+   v9.CheckScriptSuccess(['vim9script'] + lines)
+   # FIXME: not yet right for :def
+   lines[14] = 'assert_equal(2 .. a, flist[n]())'
+   v9.CheckDefSuccess(lines)
  enddef
  
  def Test_for_loop_fails()
*** ../vim-9.0.0494/src/version.c       2022-09-17 21:57:35.860822365 +0100
--- src/version.c       2022-09-18 11:27:19.589988988 +0100
***************
*** 701,702 ****
--- 701,704 ----
  {   /* Add new patch number below this line */
+ /**/
+     495,
  /**/

-- 
>From "know your smileys":
 :-F    Bucktoothed vampire with one tooth missing

 /// 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/20220918110105.5B54A1C0846%40moolenaar.net.

Raspunde prin e-mail lui