]> granicus.if.org Git - vim/commitdiff
patch 8.2.4425: map() function does not check function arguments v8.2.4425
authorBram Moolenaar <Bram@vim.org>
Sun, 20 Feb 2022 15:52:28 +0000 (15:52 +0000)
committerBram Moolenaar <Bram@vim.org>
Sun, 20 Feb 2022 15:52:28 +0000 (15:52 +0000)
Problem:    map() function does not check function arguments at compile time.
Solution:   Give an error if the arguments of a map() function are wrong.

src/evalfunc.c
src/testdir/test_vim9_builtin.vim
src/testdir/test_vim9_func.vim
src/version.c

index 7c313f5d1313fb1f1d0bdfef98cee41576232339..86e5d2f4029369ece261a090af57130e914bf32c 100644 (file)
@@ -517,7 +517,7 @@ arg_filter_func(type_T *type, type_T *decl_type UNUSED, argcontext_T *context)
 }
 
 /*
- * Check second argument of map().
+ * Check second argument of map(), the function.
  */
     static int
 arg_map_func(type_T *type, type_T *decl_type UNUSED, argcontext_T *context)
@@ -530,35 +530,67 @@ arg_map_func(type_T *type, type_T *decl_type UNUSED, argcontext_T *context)
 
     if (type->tt_type == VAR_FUNC)
     {
-       if (type->tt_member != &t_any && type->tt_member != &t_unknown)
-       {
-           type_T *expected = NULL;
+       type_T *expected_ret = NULL;
+       type_T *(args[2]);
+       type_T t_func_exp = {VAR_FUNC, 2, 0, 0, NULL, args};
 
-           if (context->arg_types[0].type_curr->tt_type == VAR_LIST
-                   || context->arg_types[0].type_curr->tt_type == VAR_DICT)
+       if (context->arg_types[0].type_curr->tt_type == VAR_LIST
+               || context->arg_types[0].type_curr->tt_type == VAR_DICT)
+       {
+           // Use the declared type if possible, so that an error is given if
+           // a declared list changes type, but not if a constant list changes
+           // type.
+           if (context->arg_types[0].type_decl->tt_type == VAR_LIST
+                   || context->arg_types[0].type_decl->tt_type == VAR_DICT)
+               expected_ret = context->arg_types[0].type_decl->tt_member;
+           else
+               expected_ret = context->arg_types[0].type_curr->tt_member;
+       }
+       else if (context->arg_types[0].type_curr->tt_type == VAR_STRING)
+           expected_ret = &t_string;
+       else if (context->arg_types[0].type_curr->tt_type == VAR_BLOB)
+           expected_ret = &t_number;
+
+       args[0] = NULL;
+       args[1] = &t_unknown;
+       if (type->tt_argcount != -1)
+       {
+           if (!(type->tt_argcount == 2 || (type->tt_argcount == 1
+                                       && (type->tt_flags & TTFLAG_VARARGS))))
            {
-               // Use the declared type, so that an error is given if a
-               // declared list changes type, but not if a constant list
-               // changes type.
-               if (context->arg_types[0].type_decl->tt_type == VAR_LIST
-                       || context->arg_types[0].type_decl->tt_type == VAR_DICT)
-                   expected = context->arg_types[0].type_decl->tt_member;
-               else
-                   expected = context->arg_types[0].type_curr->tt_member;
+               emsg(_(e_invalid_number_of_arguments));
+               return FAIL;
            }
-           else if (context->arg_types[0].type_curr->tt_type == VAR_STRING)
-               expected = &t_string;
-           else if (context->arg_types[0].type_curr->tt_type == VAR_BLOB)
-               expected = &t_number;
-           if (expected != NULL)
+           if (type->tt_flags & TTFLAG_VARARGS)
+               // check the argument types at runtime
+               t_func_exp.tt_argcount = -1;
+           else
            {
-               type_T t_func_exp = {VAR_FUNC, -1, 0, TTFLAG_STATIC,
-                                                                  NULL, NULL};
-
-               t_func_exp.tt_member = expected;
-               return check_arg_type(&t_func_exp, type, context);
+               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;
            }
        }
+
+       if ((type->tt_member != &t_any && type->tt_member != &t_unknown)
+               || args[0] != NULL)
+       {
+           where_T where = WHERE_INIT;
+
+           t_func_exp.tt_member = expected_ret == NULL
+                                       || type->tt_member == &t_any
+                                       || type->tt_member == &t_unknown
+                                   ? &t_any : expected_ret;
+           if (args[0] == NULL)
+               args[0] = &t_unknown;
+           return check_arg_type(&t_func_exp, type, context);
+
+           where.wt_index = 2;
+           return check_type(&t_func_exp, type, TRUE, where);
+       }
     }
     else
     {
index 09cfd707d960f60a00fafca8b57e817876bb25c4..882c1811a3ae261752c200d99b2150926959069c 100644 (file)
@@ -2289,11 +2289,11 @@ def Test_map_function_arg()
   lines =<< trim END
       range(3)->map((a, b, c) => a + b + c)
   END
-  v9.CheckDefExecAndScriptFailure(lines, 'E1190: One argument too few')
+  v9.CheckDefAndScriptFailure(lines, ['E176:', 'E1190: One argument too few'])
   lines =<< trim END
       range(3)->map((a, b, c, d) => a + b + c + d)
   END
-  v9.CheckDefExecAndScriptFailure(lines, 'E1190: 2 arguments too few')
+  v9.CheckDefAndScriptFailure(lines, ['E176:', 'E1190: 2 arguments too few'])
 
   # declared list cannot change type
   lines =<< trim END
@@ -2303,7 +2303,7 @@ def Test_map_function_arg()
     var ll: list<number> = [1, 2, 3]
     echo map(ll, Map)
   END
-  v9.CheckDefAndScriptFailure(lines, ['E1013: Argument 2: type mismatch, expected func(...): number but got func(number, number): string', 'E1012: Type mismatch; expected number but got string in map()'])
+  v9.CheckDefAndScriptFailure(lines, ['E1013: Argument 2: type mismatch, expected func(?number, ?number): number but got func(number, number): string', 'E1012: Type mismatch; expected number but got string in map()'])
 
   # not declared list can change type
   echo [1, 2, 3]->map((..._) => 'x')
@@ -2321,19 +2321,19 @@ def Test_map_item_type()
     var l: list<number> = [0]
     echo map(l, (_, v) => [])
   END
-  v9.CheckDefAndScriptFailure(lines, ['E1013: Argument 2: type mismatch, expected func(...): number but got func(any, any): list<unknown>', 'E1012: Type mismatch; expected number but got list<unknown> in map()'], 2)
+  v9.CheckDefAndScriptFailure(lines, ['E1013: Argument 2: type mismatch, expected func(?number, ?number): number but got func(any, any): list<unknown>', 'E1012: Type mismatch; expected number but got list<unknown> in map()'], 2)
 
   lines =<< trim END
     var l: list<number> = range(2)
     echo map(l, (_, v) => [])
   END
-  v9.CheckDefAndScriptFailure(lines, ['E1013: Argument 2: type mismatch, expected func(...): number but got func(any, any): list<unknown>', 'E1012: Type mismatch; expected number but got list<unknown> in map()'], 2)
+  v9.CheckDefAndScriptFailure(lines, ['E1013: Argument 2: type mismatch, expected func(?number, ?number): number but got func(any, any): list<unknown>', 'E1012: Type mismatch; expected number but got list<unknown> in map()'], 2)
 
   lines =<< trim END
     var d: dict<number> = {key: 0}
     echo map(d, (_, v) => [])
   END
-  v9.CheckDefAndScriptFailure(lines, ['E1013: Argument 2: type mismatch, expected func(...): number but got func(any, any): list<unknown>', 'E1012: Type mismatch; expected number but got list<unknown> in map()'], 2)
+  v9.CheckDefAndScriptFailure(lines, ['E1013: Argument 2: type mismatch, expected func(?string, ?number): number but got func(any, any): list<unknown>', 'E1012: Type mismatch; expected number but got list<unknown> in map()'], 2)
 enddef
 
 def Test_maparg()
@@ -2359,6 +2359,22 @@ def Test_maparg()
   v9.CheckDefAndScriptFailure(['maparg("a", "b", 2)'], ['E1013: Argument 3: type mismatch, expected bool but got number', 'E1212: Bool required for argument 3'])
   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')
+
+  lines =<< trim END
+    {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')
 enddef
 
 def Test_maparg_mapset()
index a7b6187d9796e961c88a7fc8b020e9b479aa03e7..81a67d97fbacb1449fa41d32b74bcc1e5e9ec355 100644 (file)
@@ -3732,12 +3732,12 @@ def Test_too_many_arguments()
   var lines =<< trim END
     echo [0, 1, 2]->map(() => 123)
   END
-  v9.CheckDefExecAndScriptFailure(lines, 'E1106: 2 arguments too many', 1)
+  v9.CheckDefAndScriptFailure(lines, ['E176:', 'E1106: 2 arguments too many'], 1)
 
   lines =<< trim END
     echo [0, 1, 2]->map((_) => 123)
   END
-  v9.CheckDefExecAndScriptFailure(lines, 'E1106: One argument too many', 1)
+  v9.CheckDefAndScriptFailure(lines, ['E176', 'E1106: One argument too many'], 1)
 enddef
 
 def Test_closing_brace_at_start_of_line()
index f15153131cf5f7f20f4c2c7055fa38ba6b543702..1fdf4e47d3c4c348ad12c541642302800786500b 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    4425,
 /**/
     4424,
 /**/