# HG changeset patch # User Bram Moolenaar # Date 1664557205 -7200 # Node ID 56fabd53c7b805efe26d43d0ea3e6cf1bd5535a5 # Parent 8b4040d04c328212dfa36fad81da863dad96124d patch 9.0.0629: get an error for using const only when executing Commit: https://github.com/vim/vim/commit/a5d1a67bee5672786c4b172f66ae4391e7b0cc57 Author: Bram Moolenaar Date: Fri Sep 30 17:57:47 2022 +0100 patch 9.0.0629: get an error for using const only when executing Problem: Get an error for using const only when executing. Solution: Check for const at compile time for filter(), map(), remove(), reverse(), sort() and uniq(). diff --git a/src/evalfunc.c b/src/evalfunc.c --- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -214,14 +214,28 @@ check_arg_type( type_T *actual, argcontext_T *context) { - // TODO: would be useful to know if "actual" is a constant and pass it to - // need_type() to get a compile time error if possible. return need_type(actual, expected, context->arg_idx - context->arg_count, context->arg_idx + 1, context->arg_cctx, FALSE, FALSE); } /* + * Call need_type() to check an argument type and that it is modifiable + */ + static int +check_arg_type_mod( + type_T *expected, + type_T *actual, + argcontext_T *context) +{ + if (need_type(actual, expected, + context->arg_idx - context->arg_count, context->arg_idx + 1, + context->arg_cctx, FALSE, FALSE) == FAIL) + return FAIL; + return arg_type_modifiable(actual, context->arg_idx + 1); +} + +/* * Give an error if "type" is a constant. */ int @@ -280,6 +294,18 @@ arg_list_any(type_T *type, type_T *decl_ } /* + * Check "type" is a list of 'any' and modifiable + */ + static int +arg_list_any_mod( + type_T *type, + type_T *decl_type UNUSED, + argcontext_T *context) +{ + return check_arg_type_mod(&t_list_any, type, context); +} + +/* * Check "type" is a list of numbers. */ static int @@ -513,16 +539,20 @@ arg_list_or_dict_mod( /* * Check "type" is a list of 'any' or a dict of 'any' or a blob. + * Also check if "type" is modifiable. */ static int -arg_list_or_dict_or_blob(type_T *type, type_T *decl_type UNUSED, argcontext_T *context) +arg_list_or_dict_or_blob_mod( + type_T *type, + type_T *decl_type UNUSED, + argcontext_T *context) { if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN || type->tt_type == VAR_LIST || type->tt_type == VAR_DICT || type->tt_type == VAR_BLOB) - return OK; + return arg_type_modifiable(type, context->arg_idx + 1); arg_type_mismatch(&t_list_any, type, context->arg_idx + 1); return FAIL; } @@ -545,6 +575,21 @@ arg_list_or_dict_or_blob_or_string(type_ } /* + * Check "type" is a list of 'any' or a dict of 'any' or a blob or a string. + * Also check the value is modifiable. + */ + static int +arg_list_or_dict_or_blob_or_string_mod( + type_T *type, + type_T *decl_type, + argcontext_T *context) +{ + if (arg_list_or_dict_or_blob_or_string(type, decl_type, context) == FAIL) + return FAIL; + return arg_type_modifiable(type, context->arg_idx + 1); +} + +/* * Check second argument of map() or filter(). */ static int @@ -994,7 +1039,7 @@ static argcheck_T arg1_float_or_nr[] = { static argcheck_T arg1_job[] = {arg_job}; static argcheck_T arg1_list_any[] = {arg_list_any}; static argcheck_T arg1_list_number[] = {arg_list_number}; -static argcheck_T arg1_list_or_blob[] = {arg_list_or_blob}; +static argcheck_T arg1_list_or_blob_mod[] = {arg_list_or_blob_mod}; static argcheck_T arg1_list_or_dict[] = {arg_list_or_dict}; static argcheck_T arg1_list_string[] = {arg_list_string}; static argcheck_T arg1_string_or_list_or_dict[] = {arg_string_or_list_or_dict}; @@ -1091,15 +1136,15 @@ static argcheck_T arg23_insert[] = {arg_ static argcheck_T arg1_len[] = {arg_len1}; static argcheck_T arg3_libcall[] = {arg_string, arg_string, arg_string_or_nr}; static argcheck_T arg14_maparg[] = {arg_string, arg_string, arg_bool, arg_bool}; -static argcheck_T arg2_filter[] = {arg_list_or_dict_or_blob_or_string, arg_filter_func}; -static argcheck_T arg2_map[] = {arg_list_or_dict_or_blob_or_string, arg_map_func}; +static argcheck_T arg2_filter[] = {arg_list_or_dict_or_blob_or_string_mod, arg_filter_func}; +static argcheck_T arg2_map[] = {arg_list_or_dict_or_blob_or_string_mod, arg_map_func}; static argcheck_T arg2_mapnew[] = {arg_list_or_dict_or_blob_or_string, NULL}; static argcheck_T arg25_matchadd[] = {arg_string, arg_string, arg_number, arg_number, arg_dict_any}; static argcheck_T arg25_matchaddpos[] = {arg_string, arg_list_any, arg_number, arg_number, arg_dict_any}; static argcheck_T arg119_printf[] = {arg_string_or_nr, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; static argcheck_T arg23_reduce[] = {arg_string_list_or_blob, NULL, NULL}; static argcheck_T arg24_remote_expr[] = {arg_string, arg_string, arg_string, arg_number}; -static argcheck_T arg23_remove[] = {arg_list_or_dict_or_blob, arg_remove2, arg_number}; +static argcheck_T arg23_remove[] = {arg_list_or_dict_or_blob_mod, arg_remove2, arg_number}; static argcheck_T arg2_repeat[] = {arg_repeat1, arg_number}; static argcheck_T arg15_search[] = {arg_string, arg_string, arg_number, arg_number, arg_string_or_func}; static argcheck_T arg37_searchpair[] = {arg_string, arg_string, arg_string, arg_string, arg_string_or_func, arg_number, arg_number}; @@ -1111,7 +1156,7 @@ static argcheck_T arg23_settagstack[] = 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 arg13_sortuniq[] = {arg_list_any_mod, 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}; @@ -2362,7 +2407,7 @@ static funcentry_T global_functions[] = ret_repeat, f_repeat}, {"resolve", 1, 1, FEARG_1, arg1_string, ret_string, f_resolve}, - {"reverse", 1, 1, FEARG_1, arg1_list_or_blob, + {"reverse", 1, 1, FEARG_1, arg1_list_or_blob_mod, ret_first_arg, f_reverse}, {"round", 1, 1, FEARG_1, arg1_float_or_nr, ret_float, f_round}, diff --git a/src/testdir/test_vim9_builtin.vim b/src/testdir/test_vim9_builtin.vim --- a/src/testdir/test_vim9_builtin.vim +++ b/src/testdir/test_vim9_builtin.vim @@ -1528,6 +1528,20 @@ def Test_filter_missing_argument() res->assert_equal({aa: [1], ac: [3]}) enddef +def Test_filter_const() + var lines =<< trim END + const l = [1, 2, 3] + filter(l, 'v:val == 2') + END + v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const list') + + lines =<< trim END + const d = {a: 1, b: 2} + filter(d, 'v:val == 2') + END + v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const dict') +enddef + def Test_foldclosed() v9.CheckDefAndScriptFailure(['foldclosed(function("min"))'], ['E1013: Argument 1: type mismatch, expected string but got func(...): unknown', 'E1220: String or Number required for argument 1']) v9.CheckDefExecAndScriptFailure(['foldclosed("")'], 'E1209: Invalid value for a line number') @@ -2547,6 +2561,20 @@ def Test_map_failure() v9.CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got bool') enddef +def Test_map_const() + var lines =<< trim END + const l = [1, 2, 3] + map(l, 'SomeFunc') + END + v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const list') + + lines =<< trim END + const d = {a: 1, b: 2} + map(d, 'SomeFunc') + END + v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const dict') +enddef + def Test_map_function_arg() var lines =<< trim END def MapOne(i: number, v: string): string @@ -3334,12 +3362,32 @@ def Test_remote_startserver() v9.CheckDefAndScriptFailure(['remote_startserver({})'], ['E1013: Argument 1: type mismatch, expected string but got dict', 'E1174: String required for argument 1']) enddef -def Test_remove_const_list() +def Test_remove_literal_list() var l: list = [1, 2, 3, 4] assert_equal([1, 2], remove(l, 0, 1)) assert_equal([3, 4], l) enddef +def Test_remove_const() + var lines =<< trim END + const l = [1, 2, 3, 4] + remove(l, 1) + END + v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const list') + + lines =<< trim END + const d = {a: 1, b: 2} + remove(d, 'a') + END + v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const dict') + + lines =<< trim END + const b = 0z010203 + remove(b, 1) + END + v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const blob') +enddef + def Test_remove() v9.CheckDefAndScriptFailure(['remove("a", 1)'], ['E1013: Argument 1: type mismatch, expected list but got string', 'E1228: List, Dictionary or Blob required for argument 1']) v9.CheckDefAndScriptFailure(['remove([], "b")'], ['E1013: Argument 2: type mismatch, expected number but got string', 'E1210: Number required for argument 2']) @@ -3419,6 +3467,20 @@ def Test_reverse_return_type() res->assert_equal(6) enddef +def Test_reverse_const() + var lines =<< trim END + const l = [1, 2, 3, 4] + reverse(l) + END + v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const list') + + lines =<< trim END + const b = 0z010203 + reverse(b) + END + v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const blob') +enddef + def Test_rubyeval() if !has('ruby') CheckFeature ruby @@ -4053,6 +4115,14 @@ def Test_sort_argument() v9.CheckScriptSuccess(lines) enddef +def Test_sort_const() + var lines =<< trim END + const l = [1, 2, 3, 4] + sort(l) + END + v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const list') +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']) @@ -4654,6 +4724,14 @@ def Test_uniq() v9.CheckDefFailure(['var l: list = uniq(["a", "b"])'], 'E1012: Type mismatch; expected list but got list') enddef +def Test_uniq_const() + var lines =<< trim END + const l = [1, 2, 3, 4] + uniq(l) + END + v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const list') +enddef + def Test_values() v9.CheckDefAndScriptFailure(['values([])'], ['E1013: Argument 1: type mismatch, expected dict but got list', 'E1206: Dictionary required for argument 1']) assert_equal([], {}->values()) diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -700,6 +700,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 629, +/**/ 628, /**/ 627,