Patch 8.2.2191
Problem:    Vim9: using wrong name with lambda in nested function.
Solution:   Copy the lambda name earlier. (closes #7525)
Files:      src/vim9compile.c, src/testdir/test_vim9_func.vim


*** ../vim-8.2.2190/src/vim9compile.c   2020-12-22 18:33:23.395493734 +0100
--- src/vim9compile.c   2020-12-22 18:55:39.264022130 +0100
***************
*** 1428,1447 ****
  
  /*
   * Generate an ISN_NEWFUNC instruction.
   */
      static int
  generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
  {
      isn_T     *isn;
-     char_u    *name;
  
!     RETURN_OK_IF_SKIP(cctx);
!     name = vim_strsave(lambda_name);
!     if (name == NULL)
!       return FAIL;
      if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
        return FAIL;
!     isn->isn_arg.newfunc.nf_lambda = name;
      isn->isn_arg.newfunc.nf_global = func_name;
  
      return OK;
--- 1428,1454 ----
  
  /*
   * Generate an ISN_NEWFUNC instruction.
+  * "lambda_name" and "func_name" must be in allocated memory and will be
+  * consumed.
   */
      static int
  generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
  {
      isn_T     *isn;
  
!     if (cctx->ctx_skip == SKIP_YES)
!     {
!       vim_free(lambda_name);
!       vim_free(func_name);
!       return OK;
!     }
      if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
+     {
+       vim_free(lambda_name);
+       vim_free(func_name);
        return FAIL;
!     }
!     isn->isn_arg.newfunc.nf_lambda = lambda_name;
      isn->isn_arg.newfunc.nf_global = func_name;
  
      return OK;
***************
*** 4840,4846 ****
      char_u    *name_end = to_name_end(eap->arg, TRUE);
      char_u    *lambda_name;
      ufunc_T   *ufunc;
!     int               r;
  
      if (eap->forceit)
      {
--- 4847,4853 ----
      char_u    *name_end = to_name_end(eap->arg, TRUE);
      char_u    *lambda_name;
      ufunc_T   *ufunc;
!     int               r = FAIL;
  
      if (eap->forceit)
      {
***************
*** 4883,4898 ****
      eap->cookie = cctx;
      eap->skip = cctx->ctx_skip == SKIP_YES;
      eap->forceit = FALSE;
!     lambda_name = get_lambda_name();
      ufunc = define_function(eap, lambda_name);
  
      if (ufunc == NULL)
!       return eap->skip ? (char_u *)"" : NULL;
      if (ufunc->uf_def_status == UF_TO_BE_COMPILED
            && compile_def_function(ufunc, TRUE, cctx) == FAIL)
      {
        func_ptr_unref(ufunc);
!       return NULL;
      }
  
      if (is_global)
--- 4890,4910 ----
      eap->cookie = cctx;
      eap->skip = cctx->ctx_skip == SKIP_YES;
      eap->forceit = FALSE;
!     lambda_name = vim_strsave(get_lambda_name());
!     if (lambda_name == NULL)
!       return NULL;
      ufunc = define_function(eap, lambda_name);
  
      if (ufunc == NULL)
!     {
!       r = eap->skip ? OK : FAIL;
!       goto theend;
!     }
      if (ufunc->uf_def_status == UF_TO_BE_COMPILED
            && compile_def_function(ufunc, TRUE, cctx) == FAIL)
      {
        func_ptr_unref(ufunc);
!       goto theend;
      }
  
      if (is_global)
***************
*** 4903,4909 ****
--- 4915,4924 ----
        if (func_name == NULL)
            r = FAIL;
        else
+       {
            r = generate_NEWFUNC(cctx, lambda_name, func_name);
+           lambda_name = NULL;
+       }
      }
      else
      {
***************
*** 4913,4921 ****
        int block_depth = cctx->ctx_ufunc->uf_block_depth;
  
        if (lvar == NULL)
!           return NULL;
        if (generate_FUNCREF(cctx, ufunc) == FAIL)
!           return NULL;
        r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
  
        // copy over the block scope IDs
--- 4928,4936 ----
        int block_depth = cctx->ctx_ufunc->uf_block_depth;
  
        if (lvar == NULL)
!           goto theend;
        if (generate_FUNCREF(cctx, ufunc) == FAIL)
!           goto theend;
        r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
  
        // copy over the block scope IDs
***************
*** 4930,4937 ****
            }
        }
      }
- 
      // TODO: warning for trailing text?
      return r == FAIL ? NULL : (char_u *)"";
  }
  
--- 4945,4955 ----
            }
        }
      }
      // TODO: warning for trailing text?
+     r = OK;
+ 
+ theend:
+     vim_free(lambda_name);
      return r == FAIL ? NULL : (char_u *)"";
  }
  
*** ../vim-8.2.2190/src/testdir/test_vim9_func.vim      2020-12-22 
18:33:23.395493734 +0100
--- src/testdir/test_vim9_func.vim      2020-12-22 18:52:33.572588189 +0100
***************
*** 303,308 ****
--- 303,321 ----
  
    lines =<< trim END
        vim9script
+       def Outer()
+         def g:Inner()
+           echo map([1, 2, 3], {_, v -> v + 1})
+         enddef
+         g:Inner()
+       enddef
+       Outer()
+   END
+   CheckScriptSuccess(lines)
+   delfunc g:Inner
+ 
+   lines =<< trim END
+       vim9script
        def Func()
          echo 'script'
        enddef
*** ../vim-8.2.2190/src/version.c       2020-12-22 18:33:23.395493734 +0100
--- src/version.c       2020-12-22 18:50:29.928958564 +0100
***************
*** 752,753 ****
--- 752,755 ----
  {   /* Add new patch number below this line */
+ /**/
+     2191,
  /**/

-- 
A)bort, R)etry, D)o it right this time

 /// 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/202012221757.0BMHvJsR2630050%40masaka.moolenaar.net.

Raspunde prin e-mail lui