diff src/evalfunc.c @ 27798:324d394e314e v8.2.4425

patch 8.2.4425: map() function does not check function arguments Commit: https://github.com/vim/vim/commit/eddd4fc4f6d9d626374a73861c891f95ef999133 Author: Bram Moolenaar <Bram@vim.org> Date: Sun Feb 20 15:52:28 2022 +0000 patch 8.2.4425: map() function does not check function arguments Problem: map() function does not check function arguments at compile time. Solution: Give an error if the arguments of a map() function are wrong.
author Bram Moolenaar <Bram@vim.org>
date Sun, 20 Feb 2022 17:00:03 +0100
parents c1d1639b52dd
children 6e5d378919c4
line wrap: on
line diff
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -517,7 +517,7 @@ arg_filter_func(type_T *type, type_T *de
 }
 
 /*
- * 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,34 +530,66 @@ arg_map_func(type_T *type, type_T *decl_
 
     if (type->tt_type == VAR_FUNC)
     {
-	if (type->tt_member != &t_any && type->tt_member != &t_unknown)
-	{
-	    type_T *expected = NULL;
-
-	    if (context->arg_types[0].type_curr->tt_type == VAR_LIST
-		    || context->arg_types[0].type_curr->tt_type == VAR_DICT)
+	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)
+	{
+	    // 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;
+	    }
+	    if (type->tt_flags & TTFLAG_VARARGS)
+		// check the argument types at runtime
+		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;
 	    }
-	    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)
-	    {
-		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 ((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