comparison src/evalfunc.c @ 30576:72e6073a2822 v9.0.0623

patch 9.0.0623: error for modifying a const is not detected at compile time Commit: https://github.com/vim/vim/commit/fa1039760e8c1a0c7a2a722160bd3d71a4736e61 Author: Bram Moolenaar <Bram@vim.org> Date: Thu Sep 29 19:14:42 2022 +0100 patch 9.0.0623: error for modifying a const is not detected at compile time Problem: Error for modifying a const is not detected at compile time. Solution: Add TTFLAG_CONST and check for it in add() and extend().
author Bram Moolenaar <Bram@vim.org>
date Thu, 29 Sep 2022 20:15:05 +0200
parents b3de17181c19
children 56fabd53c7b8
comparison
equal deleted inserted replaced
30575:3b314751334b 30576:72e6073a2822
220 context->arg_idx - context->arg_count, context->arg_idx + 1, 220 context->arg_idx - context->arg_count, context->arg_idx + 1,
221 context->arg_cctx, FALSE, FALSE); 221 context->arg_cctx, FALSE, FALSE);
222 } 222 }
223 223
224 /* 224 /*
225 * Give an error if "type" is a constant.
226 */
227 int
228 arg_type_modifiable(type_T *type, int arg_idx)
229 {
230 char *tofree;
231
232 if ((type->tt_flags & TTFLAG_CONST) == 0)
233 return OK;
234 semsg(_(e_argument_nr_trying_to_modify_const_str),
235 arg_idx, type_name(type, &tofree));
236 vim_free(tofree);
237 return FAIL;
238 }
239
240 /*
225 * Check "type" is a float or a number. 241 * Check "type" is a float or a number.
226 */ 242 */
227 static int 243 static int
228 arg_float_or_nr(type_T *type, type_T *decl_type UNUSED, argcontext_T *context) 244 arg_float_or_nr(type_T *type, type_T *decl_type UNUSED, argcontext_T *context)
229 { 245 {
319 || type->tt_type == VAR_LIST 335 || type->tt_type == VAR_LIST
320 || type->tt_type == VAR_BLOB) 336 || type->tt_type == VAR_BLOB)
321 return OK; 337 return OK;
322 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1); 338 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
323 return FAIL; 339 return FAIL;
340 }
341
342 /*
343 * Check "type" is a modifiable list of 'any' or a blob.
344 */
345 static int
346 arg_list_or_blob_mod(
347 type_T *type,
348 type_T *decl_type,
349 argcontext_T *context)
350 {
351 if (arg_list_or_blob(type, decl_type, context) == FAIL)
352 return FAIL;
353 return arg_type_modifiable(type, context->arg_idx + 1);
324 } 354 }
325 355
326 /* 356 /*
327 * Check "type" is a string or a number 357 * Check "type" is a string or a number
328 */ 358 */
463 || type->tt_type == VAR_LIST 493 || type->tt_type == VAR_LIST
464 || type->tt_type == VAR_DICT) 494 || type->tt_type == VAR_DICT)
465 return OK; 495 return OK;
466 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1); 496 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
467 return FAIL; 497 return FAIL;
498 }
499
500 /*
501 * Check "type" is a list of 'any' or a dict of 'any'. And modifiable.
502 */
503 static int
504 arg_list_or_dict_mod(
505 type_T *type,
506 type_T *decl_type,
507 argcontext_T *context)
508 {
509 if (arg_list_or_dict(type, decl_type, context) == FAIL)
510 return FAIL;
511 return arg_type_modifiable(type, context->arg_idx + 1);
468 } 512 }
469 513
470 /* 514 /*
471 * Check "type" is a list of 'any' or a dict of 'any' or a blob. 515 * Check "type" is a list of 'any' or a dict of 'any' or a blob.
472 */ 516 */
977 static argcheck_T arg2_job_string_or_number[] = {arg_job, arg_string_or_nr}; 1021 static argcheck_T arg2_job_string_or_number[] = {arg_job, arg_string_or_nr};
978 static argcheck_T arg2_list_any_number[] = {arg_list_any, arg_number}; 1022 static argcheck_T arg2_list_any_number[] = {arg_list_any, arg_number};
979 static argcheck_T arg2_list_any_string[] = {arg_list_any, arg_string}; 1023 static argcheck_T arg2_list_any_string[] = {arg_list_any, arg_string};
980 static argcheck_T arg2_list_number[] = {arg_list_number, arg_list_number}; 1024 static argcheck_T arg2_list_number[] = {arg_list_number, arg_list_number};
981 static argcheck_T arg2_list_number_bool[] = {arg_list_number, arg_bool}; 1025 static argcheck_T arg2_list_number_bool[] = {arg_list_number, arg_bool};
982 static argcheck_T arg2_listblob_item[] = {arg_list_or_blob, arg_item_of_prev}; 1026 static argcheck_T arg2_listblobmod_item[] = {arg_list_or_blob_mod, arg_item_of_prev};
983 static argcheck_T arg2_lnum[] = {arg_lnum, arg_lnum}; 1027 static argcheck_T arg2_lnum[] = {arg_lnum, arg_lnum};
984 static argcheck_T arg2_lnum_number[] = {arg_lnum, arg_number}; 1028 static argcheck_T arg2_lnum_number[] = {arg_lnum, arg_number};
985 static argcheck_T arg2_number[] = {arg_number, arg_number}; 1029 static argcheck_T arg2_number[] = {arg_number, arg_number};
986 static argcheck_T arg2_number_any[] = {arg_number, NULL}; 1030 static argcheck_T arg2_number_any[] = {arg_number, NULL};
987 static argcheck_T arg2_number_bool[] = {arg_number, arg_bool}; 1031 static argcheck_T arg2_number_bool[] = {arg_number, arg_bool};
1034 static argcheck_T arg23_chanraw[] = {arg_chan_or_job, arg_string_or_blob, arg_dict_any}; 1078 static argcheck_T arg23_chanraw[] = {arg_chan_or_job, arg_string_or_blob, arg_dict_any};
1035 static argcheck_T arg24_count[] = {arg_string_or_list_or_dict, NULL, arg_bool, arg_number}; 1079 static argcheck_T arg24_count[] = {arg_string_or_list_or_dict, NULL, arg_bool, arg_number};
1036 static argcheck_T arg13_cursor[] = {arg_cursor1, arg_number, arg_number}; 1080 static argcheck_T arg13_cursor[] = {arg_cursor1, arg_number, arg_number};
1037 static argcheck_T arg12_deepcopy[] = {NULL, arg_bool}; 1081 static argcheck_T arg12_deepcopy[] = {NULL, arg_bool};
1038 static argcheck_T arg12_execute[] = {arg_string_or_list_string, arg_string}; 1082 static argcheck_T arg12_execute[] = {arg_string_or_list_string, arg_string};
1039 static argcheck_T arg23_extend[] = {arg_list_or_dict, arg_same_as_prev, arg_extend3}; 1083 static argcheck_T arg23_extend[] = {arg_list_or_dict_mod, arg_same_as_prev, arg_extend3};
1040 static argcheck_T arg23_extendnew[] = {arg_list_or_dict, arg_same_struct_as_prev, arg_extend3}; 1084 static argcheck_T arg23_extendnew[] = {arg_list_or_dict, arg_same_struct_as_prev, arg_extend3};
1041 static argcheck_T arg23_get[] = {arg_get1, arg_string_or_nr, NULL}; 1085 static argcheck_T arg23_get[] = {arg_get1, arg_string_or_nr, NULL};
1042 static argcheck_T arg14_glob[] = {arg_string, arg_bool, arg_bool, arg_bool}; 1086 static argcheck_T arg14_glob[] = {arg_string, arg_bool, arg_bool, arg_bool};
1043 static argcheck_T arg25_globpath[] = {arg_string, arg_string, arg_bool, arg_bool, arg_bool}; 1087 static argcheck_T arg25_globpath[] = {arg_string, arg_string, arg_bool, arg_bool, arg_bool};
1044 static argcheck_T arg24_index[] = {arg_list_or_blob, arg_item_of_prev, arg_number, arg_bool}; 1088 static argcheck_T arg24_index[] = {arg_list_or_blob, arg_item_of_prev, arg_number, arg_bool};
1552 { 1596 {
1553 {"abs", 1, 1, FEARG_1, arg1_float_or_nr, 1597 {"abs", 1, 1, FEARG_1, arg1_float_or_nr,
1554 ret_any, f_abs}, 1598 ret_any, f_abs},
1555 {"acos", 1, 1, FEARG_1, arg1_float_or_nr, 1599 {"acos", 1, 1, FEARG_1, arg1_float_or_nr,
1556 ret_float, f_acos}, 1600 ret_float, f_acos},
1557 {"add", 2, 2, FEARG_1, arg2_listblob_item, 1601 {"add", 2, 2, FEARG_1, arg2_listblobmod_item,
1558 ret_first_arg, f_add}, 1602 ret_first_arg, f_add},
1559 {"and", 2, 2, FEARG_1, arg2_number, 1603 {"and", 2, 2, FEARG_1, arg2_number,
1560 ret_number, f_and}, 1604 ret_number, f_and},
1561 {"append", 2, 2, FEARG_2, arg2_setline, 1605 {"append", 2, 2, FEARG_2, arg2_setline,
1562 ret_number_bool, f_append}, 1606 ret_number_bool, f_append},