To: vim_dev@googlegroups.com Subject: Patch 9.0.0627 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 9.0.0627 Problem: "const" and "final" both make the type a constant. (Daniel Steinberg) Solution: Only have "const" make the type a constant. Files: src/vim.h, src/vim9cmds.c, src/vim9compile.c, src/vim9.h, src/testdir/test_vim9_builtin.vim *** ../vim-9.0.0626/src/vim.h 2022-09-14 00:30:47.081316534 +0100 --- src/vim.h 2022-09-30 10:51:31.282523010 +0100 *************** *** 2250,2255 **** --- 2250,2256 ---- } estack_arg_T; // Flags for assignment functions. + #define ASSIGN_VAR 0 // ":var" (nothing special) #define ASSIGN_FINAL 0x01 // ":final" #define ASSIGN_CONST 0x02 // ":const" #define ASSIGN_NO_DECL 0x04 // "name = expr" without ":let"/":const"/":final" *** ../vim-9.0.0626/src/vim9cmds.c 2022-09-19 15:54:29.543117874 +0100 --- src/vim9cmds.c 2022-09-30 10:52:33.394427149 +0100 *************** *** 881,887 **** // Reserve a variable to store the loop iteration counter and initialize it // to -1. ! loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); if (loop_lvar == NULL) { drop_scope(cctx); --- 881,887 ---- // Reserve a variable to store the loop iteration counter and initialize it // to -1. ! loop_lvar = reserve_local(cctx, (char_u *)"", 0, ASSIGN_VAR, &t_number); if (loop_lvar == NULL) { drop_scope(cctx); *************** *** 894,900 **** // Reserve a variable to store ec_funcrefs.ga_len, used in ISN_ENDLOOP. // The variable index is always the loop var index plus one. // It is not used when no closures are encountered, we don't know yet. ! funcref_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); if (funcref_lvar == NULL) { drop_scope(cctx); --- 894,900 ---- // Reserve a variable to store ec_funcrefs.ga_len, used in ISN_ENDLOOP. // The variable index is always the loop var index plus one. // It is not used when no closures are encountered, we don't know yet. ! funcref_lvar = reserve_local(cctx, (char_u *)"", 0, ASSIGN_VAR, &t_number); if (funcref_lvar == NULL) { drop_scope(cctx); *************** *** 1050,1056 **** && need_type_where(item_type, lhs_type, -1, where, cctx, FALSE, FALSE) == FAIL) goto failed; ! var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type); if (var_lvar == NULL) // out of memory or used as an argument goto failed; --- 1050,1057 ---- && need_type_where(item_type, lhs_type, -1, where, cctx, FALSE, FALSE) == FAIL) goto failed; ! var_lvar = reserve_local(cctx, arg, varlen, ASSIGN_CONST, ! lhs_type); if (var_lvar == NULL) // out of memory or used as an argument goto failed; *************** *** 1182,1188 **** // Reserve a variable to store ec_funcrefs.ga_len, used in ISN_ENDLOOP. // It is not used when no closures are encountered, we don't know yet. ! funcref_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); if (funcref_lvar == NULL) { drop_scope(cctx); --- 1183,1189 ---- // Reserve a variable to store ec_funcrefs.ga_len, used in ISN_ENDLOOP. // It is not used when no closures are encountered, we don't know yet. ! funcref_lvar = reserve_local(cctx, (char_u *)"", 0, ASSIGN_VAR, &t_number); if (funcref_lvar == NULL) { drop_scope(cctx); *** ../vim-9.0.0626/src/vim9compile.c 2022-09-29 19:14:37.675876694 +0100 --- src/vim9compile.c 2022-09-30 10:56:41.478052493 +0100 *************** *** 471,477 **** { type_T *type = type_arg; ! if (lvar->lv_const && (type->tt_flags & TTFLAG_CONST) == 0) { if (type->tt_flags & TTFLAG_STATIC) // entry in static_types[] is followed by const type --- 471,477 ---- { type_T *type = type_arg; ! if (lvar->lv_const == ASSIGN_CONST && (type->tt_flags & TTFLAG_CONST) == 0) { if (type->tt_flags & TTFLAG_STATIC) // entry in static_types[] is followed by const type *************** *** 487,492 **** --- 487,494 ---- /* * Reserve space for a local variable. + * "assign" can be ASSIGN_VAR for :var, ASSIGN_CONST for :const and + * ASSIGN_FINAL for :final. * Return the variable or NULL if it failed. */ lvar_T * *************** *** 494,500 **** cctx_T *cctx, char_u *name, size_t len, ! int isConst, type_T *type) { lvar_T *lvar; --- 496,502 ---- cctx_T *cctx, char_u *name, size_t len, ! int assign, type_T *type) { lvar_T *lvar; *************** *** 519,525 **** lvar->lv_idx = dfunc->df_var_names.ga_len; lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); ! lvar->lv_const = isConst; if (type == &t_unknown || type == &t_any) // type not known yet, may be inferred from RHS lvar->lv_type = type; --- 521,527 ---- lvar->lv_idx = dfunc->df_var_names.ga_len; lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); ! lvar->lv_const = assign; if (type == &t_unknown || type == &t_any) // type not known yet, may be inferred from RHS lvar->lv_type = type; *************** *** 993,999 **** { // Define a local variable for the function reference. lvar = reserve_local(cctx, func_name, name_end - name_start, ! TRUE, ufunc->uf_func_type); if (lvar == NULL) goto theend; if (generate_FUNCREF(cctx, ufunc, &funcref_isn) == FAIL) --- 995,1001 ---- { // Define a local variable for the function reference. lvar = reserve_local(cctx, func_name, name_end - name_start, ! ASSIGN_CONST, ufunc->uf_func_type); if (lvar == NULL) goto theend; if (generate_FUNCREF(cctx, ufunc, &funcref_isn) == FAIL) *************** *** 1691,1698 **** return FAIL; // New local variable. lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen, ! cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type); if (lhs->lhs_lvar == NULL) return FAIL; lhs->lhs_new_local = TRUE; --- 1693,1702 ---- return FAIL; // New local variable. + int assign = cmdidx == CMD_final ? ASSIGN_FINAL + : cmdidx == CMD_const ? ASSIGN_CONST : ASSIGN_VAR; lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen, ! assign, lhs->lhs_type); if (lhs->lhs_lvar == NULL) return FAIL; lhs->lhs_new_local = TRUE; *************** *** 1769,1775 **** return FAIL; } if (!is_decl && lhs->lhs_lvar != NULL ! && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index) { semsg(_(e_cannot_assign_to_constant), lhs->lhs_name); return FAIL; --- 1773,1780 ---- return FAIL; } if (!is_decl && lhs->lhs_lvar != NULL ! && lhs->lhs_lvar->lv_const != ASSIGN_VAR ! && !lhs->lhs_has_index) { semsg(_(e_cannot_assign_to_constant), lhs->lhs_name); return FAIL; *** ../vim-9.0.0626/src/vim9.h 2022-09-19 15:54:29.539117880 +0100 --- src/vim9.h 2022-09-30 10:57:38.557967751 +0100 *************** *** 697,703 **** int lv_loop_depth; // depth for variable inside a loop or -1 int lv_loop_idx; // index of first variable inside a loop or -1 int lv_from_outer; // nesting level, using ctx_outer scope ! int lv_const; // when TRUE cannot be assigned to int lv_arg; // when TRUE this is an argument } lvar_T; --- 697,705 ---- int lv_loop_depth; // depth for variable inside a loop or -1 int lv_loop_idx; // index of first variable inside a loop or -1 int lv_from_outer; // nesting level, using ctx_outer scope ! int lv_const; // ASSIGN_VAR (can be assigned to), ! // ASSIGN_FINAL (no assignment) or ASSIGN_CONST ! // (value cannot be changed) int lv_arg; // when TRUE this is an argument } lvar_T; *** ../vim-9.0.0626/src/testdir/test_vim9_builtin.vim 2022-09-29 19:14:37.679876726 +0100 --- src/testdir/test_vim9_builtin.vim 2022-09-30 11:02:51.337511197 +0100 *************** *** 192,197 **** --- 192,204 ---- v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const list') lines =<< trim END + final l = [1, 2] + add(l, 3) + assert_equal([1, 2, 3], l) + END + v9.CheckDefSuccess(lines) + + lines =<< trim END const b = 0z0102 add(b, 0z03) END *************** *** 1209,1214 **** --- 1216,1228 ---- END v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const dict') + lines =<< trim END + final d = {a: 1, b: 2} + extend(d, {c: 3}) + assert_equal({a: 1, b: 2, c: 3}, d) + END + v9.CheckDefSuccess(lines) + # item in a for loop is const lines =<< trim END var l: list> = [{n: 1}] *** ../vim-9.0.0626/src/version.c 2022-09-29 21:37:19.321641591 +0100 --- src/version.c 2022-09-30 11:03:13.409479374 +0100 *************** *** 701,702 **** --- 701,704 ---- { /* Add new patch number below this line */ + /**/ + 627, /**/ -- hundred-and-one symptoms of being an internet addict: 226. You sit down at the computer right after dinner and your spouse says "See you in the morning." /// 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 ///