Patch 8.2.2590
Problem:    Vim9: default argument value may cause internal error.
Solution:   Hide later function arguments when compiling the expression.
            (closes #7948)
Files:      src/vim9compile.c, src/testdir/test_vim9_func.vim


*** ../vim-8.2.2589/src/vim9compile.c   2021-03-10 19:04:04.218736962 +0100
--- src/vim9compile.c   2021-03-11 20:00:14.234378887 +0100
***************
*** 8199,8204 ****
--- 8199,8205 ----
      {
        int     count = ufunc->uf_def_args.ga_len;
        int     first_def_arg = ufunc->uf_args.ga_len - count;
+       int     uf_args_len = ufunc->uf_args.ga_len;
        int     i;
        char_u  *arg;
        int     off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
***************
*** 8211,8226 ****
        ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
        if (ufunc->uf_def_arg_idx == NULL)
            goto erret;
        for (i = 0; i < count; ++i)
        {
            garray_T    *stack = &cctx.ctx_type_stack;
            type_T      *val_type;
            int         arg_idx = first_def_arg + i;
            where_T     where;
  
            ufunc->uf_def_arg_idx[i] = instr->ga_len;
            arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
!           if (compile_expr0(&arg, &cctx) == FAIL)
                goto erret;
  
            // If no type specified use the type of the default value.
--- 8212,8235 ----
        ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
        if (ufunc->uf_def_arg_idx == NULL)
            goto erret;
+       SOURCING_LNUM = 0;  // line number unknown
        for (i = 0; i < count; ++i)
        {
            garray_T    *stack = &cctx.ctx_type_stack;
            type_T      *val_type;
            int         arg_idx = first_def_arg + i;
            where_T     where;
+           int         r;
+ 
+           // Make sure later arguments are not found.
+           ufunc->uf_args.ga_len = i;
  
            ufunc->uf_def_arg_idx[i] = instr->ga_len;
            arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
!           r = compile_expr0(&arg, &cctx);
! 
!           ufunc->uf_args.ga_len = uf_args_len;
!           if (r == FAIL)
                goto erret;
  
            // If no type specified use the type of the default value.
*** ../vim-8.2.2589/src/testdir/test_vim9_func.vim      2021-03-06 
22:33:09.013928670 +0100
--- src/testdir/test_vim9_func.vim      2021-03-11 20:01:44.730223422 +0100
***************
*** 311,316 ****
--- 311,324 ----
    delfunc g:Func
    CheckScriptFailure(['def Func(arg: number = "text")', 'enddef', 
'defcompile'], 'E1013: Argument 1: type mismatch, expected number but got 
string')
    delfunc g:Func
+ 
+   var lines =<< trim END
+       vim9script
+       def Func(a = b == 0 ? 1 : 2, b = 0)
+       enddef
+       defcompile
+   END
+   CheckScriptFailure(lines, 'E1001: Variable not found: b')
  enddef
  
  def FuncWithComment(  # comment
*** ../vim-8.2.2589/src/version.c       2021-03-10 22:27:44.597737046 +0100
--- src/version.c       2021-03-11 19:56:59.338840512 +0100
***************
*** 752,753 ****
--- 752,755 ----
  {   /* Add new patch number below this line */
+ /**/
+     2590,
  /**/

-- 
ARTHUR:       You are indeed brave Sir knight, but the fight is mine.
BLACK KNIGHT: Had enough?
ARTHUR:       You stupid bastard.  You havn't got any arms left.
                 "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

 /// 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/202103111904.12BJ4mI43710541%40masaka.moolenaar.net.

Raspunde prin e-mail lui