Patch 8.2.0206
Problem:    Calling Vim9 function using default argument fails.
Solution:   Give an appropriate error. (closes #5572)
Files:      src/testdir/test_vim9_script.vim, src/vim9compile.c,
            src/vim9execute.c


*** ../vim-8.2.0205/src/testdir/test_vim9_script.vim    2020-02-03 
20:50:55.672929674 +0100
--- src/testdir/test_vim9_script.vim    2020-02-04 21:22:45.706793695 +0100
***************
*** 131,136 ****
--- 131,164 ----
    assert_equal('one,two,three', MyVarargs('one', 'two', 'three'))
  enddef
  
+ "def Test_call_func_defined_later()
+ "  call assert_equal('one', DefineLater('one'))
+ "  call assert_fails('call NotDefined("one")', 'E99:')
+ "enddef
+ 
+ func DefineLater(arg)
+   return a:arg
+ endfunc
+ 
+ def MyDefaultArgs(name = 'string'): string
+   return name
+ enddef
+ 
+ func Test_call_default_args_from_func()
+   " TODO: implement using default value for optional argument
+   "call assert_equal('string', MyDefaultArgs())
+   call assert_fails('call MyDefaultArgs()', 'optional arguments not 
implemented yet')
+   call assert_equal('one', MyDefaultArgs('one'))
+   call assert_fails('call MyDefaultArgs("one", "two")', 'E118:')
+ endfunc
+ 
+ def Test_call_default_args()
+   " TODO: implement using default value for optional argument
+   "assert_equal('string', MyDefaultArgs())
+   assert_equal('one', MyDefaultArgs('one'))
+   assert_fails('call MyDefaultArgs("one", "two")', 'E118:')
+ enddef
+ 
  def Test_return_type_wrong()
    " TODO: why is ! needed for Mac and FreeBSD?
    CheckScriptFailure(['def Func(): number', 'return "a"', 'enddef'], 
'expected number but got string')
*** ../vim-8.2.0205/src/vim9compile.c   2020-02-03 20:50:55.672929674 +0100
--- src/vim9compile.c   2020-02-04 20:38:32.255838821 +0100
***************
*** 1024,1032 ****
      isn->isn_arg.ufunc.cuf_argcount = argcount;
  
      stack->ga_len -= argcount; // drop the arguments
! 
!     // drop the funcref/partial, get back the return value
!     ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any;
  
      return OK;
  }
--- 1024,1034 ----
      isn->isn_arg.ufunc.cuf_argcount = argcount;
  
      stack->ga_len -= argcount; // drop the arguments
!     if (ga_grow(stack, 1) == FAIL)
!       return FAIL;
!     // add return value
!     ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
!     ++stack->ga_len;
  
      return OK;
  }
*** ../vim-8.2.0205/src/vim9execute.c   2020-02-02 22:24:00.624827188 +0100
--- src/vim9execute.c   2020-02-04 21:20:32.035226505 +0100
***************
*** 362,367 ****
--- 362,368 ----
      int               idx;
      int               ret = FAIL;
      dfunc_T   *dfunc;
+     int               optcount = ufunc_argcount(ufunc) - argc;
  
  // Get pointer to item in the stack.
  #define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
***************
*** 392,397 ****
--- 393,404 ----
      ectx.ec_frame = ectx.ec_stack.ga_len;
      initial_frame_ptr = ectx.ec_frame;
  
+ // TODO: Put omitted argument default values on the stack.
+     if (optcount > 0)
+     {
+       emsg("optional arguments not implemented yet");
+       return FAIL;
+     }
      // dummy frame entries
      for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
      {
*** ../vim-8.2.0205/src/version.c       2020-02-03 22:58:45.263138668 +0100
--- src/version.c       2020-02-04 20:25:34.411243907 +0100
***************
*** 744,745 ****
--- 744,747 ----
  {   /* Add new patch number below this line */
+ /**/
+     206,
  /**/

-- 
`The Guide says there is an art to flying,' said Ford, `or at least a
knack. The knack lies in learning how to throw yourself at the ground
and miss.' He smiled weakly.
                -- Douglas Adams, "The Hitchhiker's Guide to the Galaxy"

 /// 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/202002042025.014KPbFc028015%40masaka.moolenaar.net.

Raspunde prin e-mail lui