diff options
author | Bram Moolenaar <Bram@vim.org> | 2022-09-30 17:57:47 +0100 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2022-09-30 17:57:47 +0100 |
commit | a5d1a67bee5672786c4b172f66ae4391e7b0cc57 (patch) | |
tree | b1fdba033a543ad4e0179ba96fe5be0aed5d81da /src/evalfunc.c | |
parent | d0121c63cfb0922a7048ece3efe994df6f444bed (diff) |
patch 9.0.0629: get an error for using const only when executingv9.0.0629
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().
Diffstat (limited to 'src/evalfunc.c')
-rw-r--r-- | src/evalfunc.c | 65 |
1 files changed, 55 insertions, 10 deletions
diff --git a/src/evalfunc.c b/src/evalfunc.c index 211e12ce4b..5c6591c42b 100644 --- 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_type UNUSED, argcontext_T *context) } /* + * 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_T *type, type_T *decl_type UNUSED, argco } /* + * 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[] = {arg_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_list_or_blob, arg_item_of_prev, arg_numb 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[] = {arg_number, arg_dict_any, arg_string}; 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}, |