To: vim_dev@googlegroups.com Subject: Patch 8.2.4449 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.4449 Problem: vim9: function argument of sort() not checked at compile time. Solution: Add a compile time check. Files: src/evalfunc.c, src/testdir/test_vim9_builtin.vim *** ../vim-8.2.4448/src/evalfunc.c 2022-02-22 15:12:10.667151754 +0000 --- src/evalfunc.c 2022-02-22 21:37:10.722335225 +0000 *************** *** 596,601 **** --- 596,638 ---- } /* + * Check second argument of sort() and uniq(), the "how" argument. + */ + static int + arg_sort_how(type_T *type, type_T *decl_type UNUSED, argcontext_T *context) + { + if (type->tt_type == VAR_STRING + || type->tt_type == VAR_PARTIAL + || type == &t_unknown + || type == &t_any) + return OK; + + if (type->tt_type == VAR_FUNC) + { + type_T *(args[2]); + type_T t_func_exp = {VAR_FUNC, 2, 0, 0, &t_number, args}; + + if (context->arg_types[0].type_curr->tt_type == VAR_LIST) + args[0] = context->arg_types[0].type_curr->tt_member; + else + args[0] = &t_unknown; + if ((type->tt_member != &t_any && type->tt_member != &t_unknown) + || args[0] != &t_unknown) + { + where_T where = WHERE_INIT; + + args[1] = args[0]; + where.wt_index = 2; + return check_type(&t_func_exp, type, TRUE, where); + } + + return OK; + } + semsg(_(e_string_or_function_required_for_argument_nr), 2); + return FAIL; + } + + /* * Check an expression argument, can be a string, funcref or partial. * Also accept a bool, a constant resulting from compiling a string argument. * Also accept a number, one and zero are accepted. *************** *** 1006,1012 **** static argcheck_T arg02_sign_getplaced[] = {arg_buffer, arg_dict_any}; static argcheck_T arg45_sign_place[] = {arg_number, arg_string, arg_string, arg_buffer, arg_dict_any}; static argcheck_T arg23_slice[] = {arg_slice1, arg_number, arg_number}; ! static argcheck_T arg13_sortuniq[] = {arg_list_any, NULL, arg_dict_any}; static argcheck_T arg24_strpart[] = {arg_string, arg_number, arg_number, arg_bool}; static argcheck_T arg12_system[] = {arg_string, arg_str_or_nr_or_list}; static argcheck_T arg23_win_execute[] = {arg_number, arg_string_or_list_string, arg_string}; --- 1043,1049 ---- static argcheck_T arg02_sign_getplaced[] = {arg_buffer, arg_dict_any}; static argcheck_T arg45_sign_place[] = {arg_number, arg_string, arg_string, arg_buffer, arg_dict_any}; static argcheck_T arg23_slice[] = {arg_slice1, arg_number, arg_number}; ! static argcheck_T arg13_sortuniq[] = {arg_list_any, arg_sort_how, arg_dict_any}; static argcheck_T arg24_strpart[] = {arg_string, arg_number, arg_number, arg_bool}; static argcheck_T arg12_system[] = {arg_string, arg_str_or_nr_or_list}; static argcheck_T arg23_win_execute[] = {arg_number, arg_string_or_list_string, arg_string}; *** ../vim-8.2.4448/src/testdir/test_vim9_builtin.vim 2022-02-22 19:39:07.590366896 +0000 --- src/testdir/test_vim9_builtin.vim 2022-02-22 21:51:37.703208950 +0000 *************** *** 3796,3814 **** assert_equal([1, 2, 3, 4, 5, 6, 7, 8], l) END v9.CheckDefAndScriptSuccess(lines) ! v9.CheckDefAndScriptFailure(['sort("a")'], ['E1013: Argument 1: type mismatch, expected list but got string', 'E1211: List required for argument 1']) ! v9.CheckDefAndScriptFailure(['sort([1], "", [1])'], ['E1013: Argument 3: type mismatch, expected dict but got list', 'E1206: Dictionary required for argument 3']) enddef def Test_sort_compare_func_fails() var lines =<< trim END vim9script echo ['a', 'b', 'c']->sort((a: number, b: number) => 0) END writefile(lines, 'Xbadsort') assert_fails('source Xbadsort', ['E1013:', 'E702:']) - delete('Xbadsort') enddef def Test_spellbadword() --- 3796,3836 ---- assert_equal([1, 2, 3, 4, 5, 6, 7, 8], l) END v9.CheckDefAndScriptSuccess(lines) ! ! lines =<< trim END ! sort([1, 2, 3], (a: any, b: any) => 1) ! END ! v9.CheckDefAndScriptSuccess(lines) enddef def Test_sort_compare_func_fails() + v9.CheckDefAndScriptFailure(['sort("a")'], ['E1013: Argument 1: type mismatch, expected list but got string', 'E1211: List required for argument 1']) + v9.CheckDefAndScriptFailure(['sort([1], "", [1])'], ['E1013: Argument 3: type mismatch, expected dict but got list', 'E1206: Dictionary required for argument 3']) + var lines =<< trim END vim9script echo ['a', 'b', 'c']->sort((a: number, b: number) => 0) END writefile(lines, 'Xbadsort') assert_fails('source Xbadsort', ['E1013:', 'E702:']) delete('Xbadsort') + + lines =<< trim END + var l = [1, 2, 3] + sort(l, (a: string, b: number) => 1) + END + v9.CheckDefAndScriptFailure(lines, ['E1013: Argument 2: type mismatch, expected func(?number, ?number): number but got func(string, number): number', 'E1013: Argument 1: type mismatch, expected string but got number']) + + lines =<< trim END + var l = ['a', 'b', 'c'] + sort(l, (a: string, b: number) => 1) + END + v9.CheckDefAndScriptFailure(lines, ['E1013: Argument 2: type mismatch, expected func(?string, ?string): number but got func(string, number): number', 'E1013: Argument 2: type mismatch, expected number but got string']) + + lines =<< trim END + sort([1, 2, 3], (a: number, b: number) => true) + END + v9.CheckDefAndScriptFailure(lines, ['E1013: Argument 2: type mismatch, expected func(?number, ?number): number but got func(number, number): bool', 'E1138: Using a Bool as a Number']) enddef def Test_spellbadword() *** ../vim-8.2.4448/src/version.c 2022-02-22 21:17:36.675692322 +0000 --- src/version.c 2022-02-22 21:38:11.574504270 +0000 *************** *** 752,753 **** --- 752,755 ---- { /* Add new patch number below this line */ + /**/ + 4449, /**/ -- Do not trust atoms, they make up everything. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// \\\ \\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///