comparison src/vim9compile.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 bff0506a0911
children ee039a6049ff
comparison
equal deleted inserted replaced
30575:3b314751334b 30576:72e6073a2822
461 return need_type_where(actual, expected, offset, where, 461 return need_type_where(actual, expected, offset, where,
462 cctx, silent, actual_is_const); 462 cctx, silent, actual_is_const);
463 } 463 }
464 464
465 /* 465 /*
466 * Set type of variable "lvar" to "type". If the variable is a constant then
467 * the type gets TTFLAG_CONST.
468 */
469 static void
470 set_var_type(lvar_T *lvar, type_T *type_arg, cctx_T *cctx)
471 {
472 type_T *type = type_arg;
473
474 if (lvar->lv_const && (type->tt_flags & TTFLAG_CONST) == 0)
475 {
476 if (type->tt_flags & TTFLAG_STATIC)
477 // entry in static_types[] is followed by const type
478 type = type + 1;
479 else
480 {
481 type = copy_type(type, cctx->ctx_type_list);
482 type->tt_flags |= TTFLAG_CONST;
483 }
484 }
485 lvar->lv_type = type;
486 }
487
488 /*
466 * Reserve space for a local variable. 489 * Reserve space for a local variable.
467 * Return the variable or NULL if it failed. 490 * Return the variable or NULL if it failed.
468 */ 491 */
469 lvar_T * 492 lvar_T *
470 reserve_local( 493 reserve_local(
495 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx; 518 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
496 lvar->lv_idx = dfunc->df_var_names.ga_len; 519 lvar->lv_idx = dfunc->df_var_names.ga_len;
497 520
498 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); 521 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
499 lvar->lv_const = isConst; 522 lvar->lv_const = isConst;
500 lvar->lv_type = type; 523 if (type == &t_unknown || type == &t_any)
524 // type not known yet, may be inferred from RHS
525 lvar->lv_type = type;
526 else
527 // may use TTFLAG_CONST
528 set_var_type(lvar, type, cctx);
501 529
502 // Remember the name for debugging. 530 // Remember the name for debugging.
503 if (GA_GROW_FAILS(&dfunc->df_var_names, 1)) 531 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
504 return NULL; 532 return NULL;
505 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] = 533 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2302 emsg(_(e_cannot_use_void_value)); 2330 emsg(_(e_cannot_use_void_value));
2303 goto theend; 2331 goto theend;
2304 } 2332 }
2305 else 2333 else
2306 { 2334 {
2335 type_T *type;
2336
2307 // An empty list or dict has a &t_unknown member, 2337 // An empty list or dict has a &t_unknown member,
2308 // for a variable that implies &t_any. 2338 // for a variable that implies &t_any.
2309 if (rhs_type == &t_list_empty) 2339 if (rhs_type == &t_list_empty)
2310 lhs.lhs_lvar->lv_type = &t_list_any; 2340 type = &t_list_any;
2311 else if (rhs_type == &t_dict_empty) 2341 else if (rhs_type == &t_dict_empty)
2312 lhs.lhs_lvar->lv_type = &t_dict_any; 2342 type = &t_dict_any;
2313 else if (rhs_type == &t_unknown) 2343 else if (rhs_type == &t_unknown)
2314 lhs.lhs_lvar->lv_type = &t_any; 2344 type = &t_any;
2315 else 2345 else
2316 { 2346 {
2317 lhs.lhs_lvar->lv_type = rhs_type; 2347 type = rhs_type;
2318 inferred_type = rhs_type; 2348 inferred_type = rhs_type;
2319 } 2349 }
2350 set_var_type(lhs.lhs_lvar, type, cctx);
2320 } 2351 }
2321 } 2352 }
2322 else if (*op == '=') 2353 else if (*op == '=')
2323 { 2354 {
2324 type_T *use_type = lhs.lhs_lvar->lv_type; 2355 type_T *use_type = lhs.lhs_lvar->lv_type;