Patch 8.2.4426
Problem:    map() function on string and blob does not check argument types at
            compile time.
Solution:   Check string and blob argument types.  Support "0z1234->func()".
Files:      src/vim9compile.c, src/evalfunc.c, src/ex_docmd.c,
            src/testdir/test_vim9_builtin.vim


*** ../vim-8.2.4425/src/vim9compile.c   2022-02-15 21:17:53.148747988 +0000
--- src/vim9compile.c   2022-02-20 18:02:03.114187998 +0000
***************
*** 2818,2830 ****
        /*
         * COMMAND after range
         * 'text'->func() should not be confused with 'a mark
         * "++nr" and "--nr" are eval commands
         * in "$ENV->func()" the "$" is not a range
         */
        cmd = ea.cmd;
        if ((*cmd != '$' || starts_with_colon)
!               && (starts_with_colon || !(*cmd == '\''
!                      || (cmd[0] != NUL && cmd[0] == cmd[1]
                                            && (*cmd == '+' || *cmd == '-')))))
        {
            ea.cmd = skip_range(ea.cmd, TRUE, NULL);
--- 2818,2833 ----
        /*
         * COMMAND after range
         * 'text'->func() should not be confused with 'a mark
+        * 0z1234->func() should not be confused with a zero line number
         * "++nr" and "--nr" are eval commands
         * in "$ENV->func()" the "$" is not a range
         */
        cmd = ea.cmd;
        if ((*cmd != '$' || starts_with_colon)
!               && (starts_with_colon
!                   || !(*cmd == '\''
!                       || (cmd[0] == '0' && cmd[1] == 'z')
!                       || (cmd[0] != NUL && cmd[0] == cmd[1]
                                            && (*cmd == '+' || *cmd == '-')))))
        {
            ea.cmd = skip_range(ea.cmd, TRUE, NULL);
*** ../vim-8.2.4425/src/evalfunc.c      2022-02-20 15:52:24.388395078 +0000
--- src/evalfunc.c      2022-02-20 17:38:38.825315602 +0000
***************
*** 566,574 ****
                t_func_exp.tt_argcount = -1;
            else
            {
!               if (context->arg_types[0].type_decl->tt_type == VAR_LIST)
                    args[0] = &t_number;
!               else if (context->arg_types[0].type_decl->tt_type == VAR_DICT)
                    args[0] = &t_string;
                if (args[0] != NULL)
                    args[1] = expected_ret;
--- 566,576 ----
                t_func_exp.tt_argcount = -1;
            else
            {
!               if (context->arg_types[0].type_curr->tt_type == VAR_STRING
!                       || context->arg_types[0].type_curr->tt_type == VAR_BLOB
!                       || context->arg_types[0].type_curr->tt_type == VAR_LIST)
                    args[0] = &t_number;
!               else if (context->arg_types[0].type_curr->tt_type == VAR_DICT)
                    args[0] = &t_string;
                if (args[0] != NULL)
                    args[1] = expected_ret;
*** ../vim-8.2.4425/src/ex_docmd.c      2022-02-14 21:51:42.503756078 +0000
--- src/ex_docmd.c      2022-02-20 18:19:08.652075716 +0000
***************
*** 3457,3463 ****
        char_u *pskip = skip_option_env_lead(eap->cmd);
  
        if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
!              || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
        {
            int     oplen;
            int     heredoc;
--- 3457,3464 ----
        char_u *pskip = skip_option_env_lead(eap->cmd);
  
        if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
!              || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
!              || (p[0] == '0' && p[1] == 'z'))
        {
            int     oplen;
            int     heredoc;
***************
*** 3503,3508 ****
--- 3504,3511 ----
                            // "'string'->func()" is an expression.
                         || *eap->cmd == '\''
                            // '"string"->func()' is an expression.
+                        || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
+                           // '"string"->func()' is an expression.
                         || *eap->cmd == '"'
                            // "g:varname" is an expression.
                         || eap->cmd[1] == ':'
*** ../vim-8.2.4425/src/testdir/test_vim9_builtin.vim   2022-02-20 
15:52:24.388395078 +0000
--- src/testdir/test_vim9_builtin.vim   2022-02-20 17:44:15.820751607 +0000
***************
*** 2360,2372 ****
    v9.CheckDefAndScriptFailure(['maparg("a", "b", true, 2)'], ['E1013: 
Argument 4: type mismatch, expected bool but got number', 'E1212: Bool required 
for argument 4'])
    maparg('')->assert_equal('')
  
    var lines =<< trim END
        var l = [123]
!       l->map((_, v: string) => 0)
    END
!   v9.CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected 
func(?number, ?number): number but got func(any, string): number')
  
    lines =<< trim END
        ['x']->map((i: string, v: string) => 'y')
    END
    v9.CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected 
func(?number, ?any): any but got func(string, string): string')
--- 2360,2392 ----
    v9.CheckDefAndScriptFailure(['maparg("a", "b", true, 2)'], ['E1013: 
Argument 4: type mismatch, expected bool but got number', 'E1212: Bool required 
for argument 4'])
    maparg('')->assert_equal('')
  
+   # value argument type is checked at compile time
    var lines =<< trim END
        var l = [123]
!       l->map((i: number, v: string) => 0)
    END
!   v9.CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected 
func(?number, ?number): number but got func(number, string): number')
  
    lines =<< trim END
+       var d = {a: 123}
+       d->map((i: string, v: string) => 0)
+   END
+   v9.CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected 
func(?string, ?number): number but got func(string, string): number')
+ 
+   lines =<< trim END
+     var s = 'abc'
+     s->map((i: number, v: number) => 'x')
+   END
+   v9.CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected 
func(?number, ?string): string but got func(number, number): string')
+ 
+   lines =<< trim END
+     var s = 0z1122
+     s->map((i: number, v: string) => 0)
+   END
+   v9.CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected 
func(?number, ?number): number but got func(number, string): number')
+ 
+   # index argument type is checked at compile time
+   lines =<< trim END
        ['x']->map((i: string, v: string) => 'y')
    END
    v9.CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected 
func(?number, ?any): any but got func(string, string): string')
***************
*** 2375,2380 ****
--- 2395,2410 ----
      {a: 1}->map((i: number, v: number) => 0)
    END
    v9.CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected 
func(?string, ?any): any but got func(number, number): number')
+ 
+   lines =<< trim END
+     'abc'->map((i: string, v: string) => 'x')
+   END
+   v9.CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected 
func(?number, ?string): string but got func(string, string): string')
+ 
+   lines =<< trim END
+     0z1122->map((i: string, v: number) => 0)
+   END
+   v9.CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected 
func(?number, ?number): number but got func(string, number): number')
  enddef
  
  def Test_maparg_mapset()
*** ../vim-8.2.4425/src/version.c       2022-02-20 15:52:24.388395078 +0000
--- src/version.c       2022-02-20 18:03:35.253987199 +0000
***************
*** 752,753 ****
--- 752,755 ----
  {   /* Add new patch number below this line */
+ /**/
+     4426,
  /**/

-- 
hundred-and-one symptoms of being an internet addict:
86. E-mail Deficiency Depression (EDD) forces you to e-mail yourself.

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            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/20220220184649.984491C0FE0%40moolenaar.net.

Raspunde prin e-mail lui