Patch 8.2.1812
Problem:    Vim9: nested closure throws an internal error.
Solution:   Do not skip a local variable with a partial. (closes #7065)
Files:      src/vim9execute.c, src/testdir/test_vim9_func.vim


*** ../vim-8.2.1811/src/vim9execute.c   2020-10-04 16:06:00.513884339 +0200
--- src/vim9execute.c   2020-10-07 19:07:19.221900383 +0200
***************
*** 377,386 ****
            tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
  
            // Do not copy a partial created for a local function.
!           // TODO: this won't work if the closure actually uses it.  But when
            // keeping it it gets complicated: it will create a reference cycle
            // inside the partial, thus needs special handling for garbage
            // collection.
            if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
            {
                int i;
--- 377,387 ----
            tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
  
            // Do not copy a partial created for a local function.
!           // TODO: This won't work if the closure actually uses it.  But when
            // keeping it it gets complicated: it will create a reference cycle
            // inside the partial, thus needs special handling for garbage
            // collection.
+           // For now, decide on the reference count.
            if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
            {
                int i;
***************
*** 389,395 ****
                {
                    partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
                                                          - closure_count + i];
!                   if (tv->vval.v_partial == pt)
                        break;
                }
                if (i < closure_count)
--- 390,397 ----
                {
                    partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
                                                          - closure_count + i];
! 
!                   if (tv->vval.v_partial == pt && pt->pt_refcount < 2)
                        break;
                }
                if (i < closure_count)
*** ../vim-8.2.1811/src/testdir/test_vim9_func.vim      2020-10-04 
14:17:28.965363295 +0200
--- src/testdir/test_vim9_func.vim      2020-10-07 19:04:25.074355899 +0200
***************
*** 1388,1393 ****
--- 1388,1407 ----
    CheckScriptSuccess(lines)
  enddef
  
+ def Test_nested_closure_used()
+   var lines =<< trim END
+       vim9script
+       def Func()
+         var x = 'hello'
+         var Closure = {-> x}
+         g:Myclosure = {-> Closure()}
+       enddef
+       Func()
+       assert_equal('hello', g:Myclosure())
+   END
+   CheckScriptSuccess(lines)
+ enddef
+ 
  def Test_nested_closure_fails()
    var lines =<< trim END
      vim9script
*** ../vim-8.2.1811/src/version.c       2020-10-07 17:28:47.477370289 +0200
--- src/version.c       2020-10-07 19:05:49.734134647 +0200
***************
*** 752,753 ****
--- 752,755 ----
  {   /* Add new patch number below this line */
+ /**/
+     1812,
  /**/

-- 
hundred-and-one symptoms of being an internet addict:
40. You tell the cab driver you live at
    http://123.elm.street/house/bluetrim.html
41. You actually try that 123.elm.street address.

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
 \\\            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/202010071708.097H8Vw72995201%40masaka.moolenaar.net.

Raspunde prin e-mail lui