diff src/evalfunc.c @ 27831:0a3ab3216b75 v8.2.4441

patch 8.2.4441: Vim9: function argument of filter() not checked like map() Commit: https://github.com/vim/vim/commit/16f6c8ac94d8412075060945aa90ba90be08656f Author: Bram Moolenaar <Bram@vim.org> Date: Tue Feb 22 15:12:14 2022 +0000 patch 8.2.4441: Vim9: function argument of filter() not checked like map() Problem: Vim9: function argument of filter() not checked like map(). Solution: Also check the function argument of filter().
author Bram Moolenaar <Bram@vim.org>
date Tue, 22 Feb 2022 16:15:03 +0100
parents f68947167523
children 780818e65dff
line wrap: on
line diff
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -486,6 +486,80 @@ arg_list_or_dict_or_blob_or_string(type_
 }
 
 /*
+ * Check second argument of map() or filter().
+ */
+    static int
+check_map_filter_arg2(type_T *type, argcontext_T *context, int is_map)
+{
+    type_T *expected_member = 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_member = context->arg_types[0].type_decl->tt_member;
+	else
+	    expected_member = context->arg_types[0].type_curr->tt_member;
+    }
+    else if (context->arg_types[0].type_curr->tt_type == VAR_STRING)
+	expected_member = &t_string;
+    else if (context->arg_types[0].type_curr->tt_type == VAR_BLOB)
+	expected_member = &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))))
+	{
+	    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_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_decl->tt_type == VAR_DICT)
+		args[0] = &t_string;
+	    if (args[0] != NULL)
+		args[1] = expected_member;
+	}
+    }
+
+    if ((type->tt_member != &t_any && type->tt_member != &t_unknown)
+	    || args[0] != NULL)
+    {
+	where_T where = WHERE_INIT;
+
+	if (is_map)
+	    t_func_exp.tt_member = expected_member == NULL
+				    || type->tt_member == &t_any
+				    || type->tt_member == &t_unknown
+				? &t_any : expected_member;
+	else
+	    t_func_exp.tt_member = &t_bool;
+	if (args[0] == NULL)
+	    args[0] = &t_unknown;
+
+	where.wt_index = 2;
+	return check_type(&t_func_exp, type, TRUE, where);
+    }
+    return OK;
+}
+
+/*
  * Check second argument of filter(): func must return a bool.
  */
     static int
@@ -498,22 +572,9 @@ arg_filter_func(type_T *type, type_T *de
 	return OK;
 
     if (type->tt_type == VAR_FUNC)
-    {
-	if (!(type->tt_member->tt_type == VAR_BOOL
-		|| type->tt_member->tt_type == VAR_NUMBER
-		|| type->tt_member->tt_type == VAR_UNKNOWN
-		|| type->tt_member->tt_type == VAR_ANY))
-	{
-	    arg_type_mismatch(&t_func_bool, type, context->arg_idx + 1);
-	    return FAIL;
-	}
-    }
-    else
-    {
-	semsg(_(e_string_or_function_required_for_argument_nr), 2);
-	return FAIL;
-    }
-    return OK;
+	return check_map_filter_arg2(type, context, FALSE);
+    semsg(_(e_string_or_function_required_for_argument_nr), 2);
+    return FAIL;
 }
 
 /*
@@ -529,76 +590,9 @@ arg_map_func(type_T *type, type_T *decl_
 	return OK;
 
     if (type->tt_type == VAR_FUNC)
-    {
-	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))))
-	    {
-		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_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_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;
-
-	    where.wt_index = 2;
-	    return check_type(&t_func_exp, type, TRUE, where);
-	}
-    }
-    else
-    {
-	semsg(_(e_string_or_function_required_for_argument_nr), 2);
-	return FAIL;
-    }
-    return OK;
+	return check_map_filter_arg2(type, context, TRUE);
+    semsg(_(e_string_or_function_required_for_argument_nr), 2);
+    return FAIL;
 }
 
 /*