Patch 8.2.1551
Problem:    Vim9: error for argument type does not mention the number.
Solution:   Pass the argument number to where the error is given.
Files:      src/vim9type.c, src/proto/vim9type.pro, src/vim9compile.c,
            src/vim9execute.c, src/vim9script.c, src/eval.c,
            src/testdir/test_vim9_func.vim


*** ../vim-8.2.1550/src/vim9type.c      2020-08-21 22:36:43.662719906 +0200
--- src/vim9type.c      2020-08-30 23:09:38.569363080 +0200
***************
*** 304,310 ****
   * Return FAIL if "expected" and "actual" don't match.
   */
      int
! check_typval_type(type_T *expected, typval_T *actual_tv)
  {
      garray_T  type_list;
      type_T    *actual_type;
--- 304,310 ----
   * Return FAIL if "expected" and "actual" don't match.
   */
      int
! check_typval_type(type_T *expected, typval_T *actual_tv, int argidx)
  {
      garray_T  type_list;
      type_T    *actual_type;
***************
*** 313,319 ****
      ga_init2(&type_list, sizeof(type_T *), 10);
      actual_type = typval2type(actual_tv, &type_list);
      if (actual_type != NULL)
!       res = check_type(expected, actual_type, TRUE);
      clear_type_list(&type_list);
      return res;
  }
--- 313,319 ----
      ga_init2(&type_list, sizeof(type_T *), 10);
      actual_type = typval2type(actual_tv, &type_list);
      if (actual_type != NULL)
!       res = check_type(expected, actual_type, TRUE, argidx);
      clear_type_list(&type_list);
      return res;
  }
***************
*** 321,342 ****
      void
  type_mismatch(type_T *expected, type_T *actual)
  {
!     char *tofree1, *tofree2;
! 
!     semsg(_(e_type_mismatch_expected_str_but_got_str),
!                  type_name(expected, &tofree1), type_name(actual, &tofree2));
!     vim_free(tofree1);
!     vim_free(tofree2);
  }
  
      void
  arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
  {
      char *tofree1, *tofree2;
  
!     semsg(_(e_argument_nr_type_mismatch_expected_str_but_got_str),
!           argidx,
!           type_name(expected, &tofree1), type_name(actual, &tofree2));
      vim_free(tofree1);
      vim_free(tofree2);
  }
--- 321,342 ----
      void
  type_mismatch(type_T *expected, type_T *actual)
  {
!     arg_type_mismatch(expected, actual, 0);
  }
  
      void
  arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
  {
      char *tofree1, *tofree2;
+     char *typename1 = type_name(expected, &tofree1);
+     char *typename2 = type_name(actual, &tofree2);
  
!     if (argidx > 0)
!       semsg(_(e_argument_nr_type_mismatch_expected_str_but_got_str),
!                                                argidx, typename1, typename2);
!     else
!       semsg(_(e_type_mismatch_expected_str_but_got_str),
!                                                        typename1, typename2);
      vim_free(tofree1);
      vim_free(tofree2);
  }
***************
*** 344,352 ****
  /*
   * Check if the expected and actual types match.
   * Does not allow for assigning "any" to a specific type.
   */
      int
! check_type(type_T *expected, type_T *actual, int give_msg)
  {
      int ret = OK;
  
--- 344,353 ----
  /*
   * Check if the expected and actual types match.
   * Does not allow for assigning "any" to a specific type.
+  * When "argidx" > 0 it is included in the error message.
   */
      int
! check_type(type_T *expected, type_T *actual, int give_msg, int argidx)
  {
      int ret = OK;
  
***************
*** 359,377 ****
        if (expected->tt_type != actual->tt_type)
        {
            if (give_msg)
!               type_mismatch(expected, actual);
            return FAIL;
        }
        if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
        {
            // "unknown" is used for an empty list or dict
            if (actual->tt_member != &t_unknown)
!               ret = check_type(expected->tt_member, actual->tt_member, FALSE);
        }
        else if (expected->tt_type == VAR_FUNC)
        {
            if (expected->tt_member != &t_unknown)
!               ret = check_type(expected->tt_member, actual->tt_member, FALSE);
            if (ret == OK && expected->tt_argcount != -1
                    && (actual->tt_argcount < expected->tt_min_argcount
                        || actual->tt_argcount > expected->tt_argcount))
--- 360,380 ----
        if (expected->tt_type != actual->tt_type)
        {
            if (give_msg)
!               arg_type_mismatch(expected, actual, argidx);
            return FAIL;
        }
        if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
        {
            // "unknown" is used for an empty list or dict
            if (actual->tt_member != &t_unknown)
!               ret = check_type(expected->tt_member, actual->tt_member,
!                                                                    FALSE, 0);
        }
        else if (expected->tt_type == VAR_FUNC)
        {
            if (expected->tt_member != &t_unknown)
!               ret = check_type(expected->tt_member, actual->tt_member,
!                                                                    FALSE, 0);
            if (ret == OK && expected->tt_argcount != -1
                    && (actual->tt_argcount < expected->tt_min_argcount
                        || actual->tt_argcount > expected->tt_argcount))
***************
*** 383,389 ****
                for (i = 0; i < expected->tt_argcount; ++i)
                    // Allow for using "any" argument type, lambda's have them.
                    if (actual->tt_args[i] != &t_any && check_type(
!                              expected->tt_args[i], actual->tt_args[i], FALSE)
                                                                       == FAIL)
                    {
                        ret = FAIL;
--- 386,392 ----
                for (i = 0; i < expected->tt_argcount; ++i)
                    // Allow for using "any" argument type, lambda's have them.
                    if (actual->tt_args[i] != &t_any && check_type(
!                           expected->tt_args[i], actual->tt_args[i], FALSE, 0)
                                                                       == FAIL)
                    {
                        ret = FAIL;
***************
*** 392,398 ****
            }
        }
        if (ret == FAIL && give_msg)
!           type_mismatch(expected, actual);
      }
      return ret;
  }
--- 395,401 ----
            }
        }
        if (ret == FAIL && give_msg)
!           arg_type_mismatch(expected, actual, argidx);
      }
      return ret;
  }
*** ../vim-8.2.1550/src/proto/vim9type.pro      2020-08-09 17:21:25.662269290 
+0200
--- src/proto/vim9type.pro      2020-08-30 23:11:57.348693803 +0200
***************
*** 7,16 ****
  int func_type_add_arg_types(type_T *functype, int argcount, garray_T 
*type_gap);
  type_T *typval2type(typval_T *tv, garray_T *type_gap);
  type_T *typval2type_vimvar(typval_T *tv, garray_T *type_gap);
! int check_typval_type(type_T *expected, typval_T *actual_tv);
  void type_mismatch(type_T *expected, type_T *actual);
  void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
! int check_type(type_T *expected, type_T *actual, int give_msg);
  char_u *skip_type(char_u *start, int optional);
  type_T *parse_type(char_u **arg, garray_T *type_gap);
  void common_type(type_T *type1, type_T *type2, type_T **dest, garray_T 
*type_gap);
--- 7,16 ----
  int func_type_add_arg_types(type_T *functype, int argcount, garray_T 
*type_gap);
  type_T *typval2type(typval_T *tv, garray_T *type_gap);
  type_T *typval2type_vimvar(typval_T *tv, garray_T *type_gap);
! int check_typval_type(type_T *expected, typval_T *actual_tv, int argidx);
  void type_mismatch(type_T *expected, type_T *actual);
  void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
! int check_type(type_T *expected, type_T *actual, int give_msg, int argidx);
  char_u *skip_type(char_u *start, int optional);
  type_T *parse_type(char_u **arg, garray_T *type_gap);
  void common_type(type_T *type1, type_T *type2, type_T **dest, garray_T 
*type_gap);
*** ../vim-8.2.1550/src/vim9compile.c   2020-08-29 13:39:12.578557657 +0200
--- src/vim9compile.c   2020-08-30 23:17:17.068340991 +0200
***************
*** 729,735 ****
        cctx_T  *cctx,
        int     silent)
  {
!     if (check_type(expected, actual, FALSE) == OK)
        return OK;
      if (actual->tt_type != VAR_ANY
            && actual->tt_type != VAR_UNKNOWN
--- 729,735 ----
        cctx_T  *cctx,
        int     silent)
  {
!     if (check_type(expected, actual, FALSE, 0) == OK)
        return OK;
      if (actual->tt_type != VAR_ANY
            && actual->tt_type != VAR_UNKNOWN
***************
*** 3581,3587 ****
  
        generate_ppconst(cctx, ppconst);
        actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
!       if (check_type(want_type, actual, FALSE) == FAIL)
        {
            if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
                return FAIL;
--- 3581,3587 ----
  
        generate_ppconst(cctx, ppconst);
        actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
!       if (check_type(want_type, actual, FALSE, 0) == FAIL)
        {
            if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
                return FAIL;
***************
*** 6500,6512 ****
                did_set_arg_type = TRUE;
                ufunc->uf_arg_types[arg_idx] = val_type;
            }
!           else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
!                                                                      == FAIL)
!           {
!               arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
!                                                                 arg_idx + 1);
                goto erret;
-           }
  
            if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
                goto erret;
--- 6500,6508 ----
                did_set_arg_type = TRUE;
                ufunc->uf_arg_types[arg_idx] = val_type;
            }
!           else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
!                                                   TRUE, arg_idx + 1) == FAIL)
                goto erret;
  
            if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
                goto erret;
*** ../vim-8.2.1550/src/vim9execute.c   2020-08-29 17:47:05.013718945 +0200
--- src/vim9execute.c   2020-08-30 23:11:33.048810366 +0200
***************
*** 792,799 ****
      for (idx = 0; idx < argc; ++idx)
      {
        if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
!               && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx])
!                                                                      == FAIL)
            goto failed_early;
        copy_tv(&argv[idx], STACK_TV_BOT(0));
        ++ectx.ec_stack.ga_len;
--- 792,799 ----
      for (idx = 0; idx < argc; ++idx)
      {
        if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
!               && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
!                                                             idx + 1) == FAIL)
            goto failed_early;
        copy_tv(&argv[idx], STACK_TV_BOT(0));
        ++ectx.ec_stack.ga_len;
***************
*** 822,828 ****
  
            for (idx = 0; idx < vararg_count; ++idx)
            {
!               if (check_typval_type(expected, &li->li_tv) == FAIL)
                    goto failed_early;
                li = li->li_next;
            }
--- 822,829 ----
  
            for (idx = 0; idx < vararg_count; ++idx)
            {
!               if (check_typval_type(expected, &li->li_tv,
!                                                      argc + idx + 1) == FAIL)
                    goto failed_early;
                li = li->li_next;
            }
*** ../vim-8.2.1550/src/vim9script.c    2020-08-21 22:36:43.662719906 +0200
--- src/vim9script.c    2020-08-30 23:11:39.232780679 +0200
***************
*** 580,586 ****
                semsg(_(e_readonlyvar), name);
                return FAIL;
            }
!           return check_typval_type(sv->sv_type, value);
        }
      }
      iemsg("check_script_var_type(): not found");
--- 580,586 ----
                semsg(_(e_readonlyvar), name);
                return FAIL;
            }
!           return check_typval_type(sv->sv_type, value, 0);
        }
      }
      iemsg("check_script_var_type(): not found");
*** ../vim-8.2.1550/src/eval.c  2020-08-28 16:38:07.762363597 +0200
--- src/eval.c  2020-08-30 23:09:50.617304609 +0200
***************
*** 1291,1297 ****
        else
        {
            if (lp->ll_type != NULL
!                             && check_typval_type(lp->ll_type, rettv) == FAIL)
                return;
            set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
        }
--- 1291,1297 ----
        else
        {
            if (lp->ll_type != NULL
!                          && check_typval_type(lp->ll_type, rettv, 0) == FAIL)
                return;
            set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
        }
*** ../vim-8.2.1550/src/testdir/test_vim9_func.vim      2020-08-30 
21:26:53.351220687 +0200
--- src/testdir/test_vim9_func.vim      2020-08-30 23:18:52.808178058 +0200
***************
*** 231,237 ****
      enddef
      Func([])
    END
!   call CheckScriptFailure(lines, 'E1012: type mismatch, expected string but 
got list<unknown>', 5)
  enddef
  
  " Default arg and varargs
--- 231,237 ----
      enddef
      Func([])
    END
!   call CheckScriptFailure(lines, 'E1013: argument 1: type mismatch, expected 
string but got list<unknown>', 5)
  enddef
  
  " Default arg and varargs
***************
*** 278,284 ****
        enddef
        Func(1, 2, 3)
    END
!   CheckScriptFailure(lines, 'E1012:')
  
    lines =<< trim END
        vim9script
--- 278,284 ----
        enddef
        Func(1, 2, 3)
    END
!   CheckScriptFailure(lines, 'E1013: argument 1: type mismatch')
  
    lines =<< trim END
        vim9script
***************
*** 287,293 ****
        enddef
        Func('a', 9)
    END
!   CheckScriptFailure(lines, 'E1012:')
  
    lines =<< trim END
        vim9script
--- 287,293 ----
        enddef
        Func('a', 9)
    END
!   CheckScriptFailure(lines, 'E1013: argument 2: type mismatch')
  
    lines =<< trim END
        vim9script
***************
*** 296,302 ****
        enddef
        Func(1, 'a')
    END
!   CheckScriptFailure(lines, 'E1012:')
  enddef
  
  def Test_call_call()
--- 296,302 ----
        enddef
        Func(1, 'a')
    END
!   CheckScriptFailure(lines, 'E1013: argument 1: type mismatch')
  enddef
  
  def Test_call_call()
***************
*** 691,697 ****
      enddef
      MyFunc(1234)
    END
!   CheckScriptFailure(lines, 'E1012: type mismatch, expected string but got 
number')
  enddef
  
  def Test_vim9script_call_fail_const()
--- 691,697 ----
      enddef
      MyFunc(1234)
    END
!   CheckScriptFailure(lines, 'E1013: argument 1: type mismatch, expected 
string but got number')
  enddef
  
  def Test_vim9script_call_fail_const()
*** ../vim-8.2.1550/src/version.c       2020-08-30 21:26:53.351220687 +0200
--- src/version.c       2020-08-30 23:19:21.364122448 +0200
***************
*** 756,757 ****
--- 756,759 ----
  {   /* Add new patch number below this line */
+ /**/
+     1551,
  /**/

-- 
Yah, well, we had to carve our electrons out of driftwood we'd
find.  In the winter.  Uphill.  Both ways.

 /// 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/202008302124.07ULOkpT2906097%40masaka.moolenaar.net.

Raspunde prin e-mail lui