summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2022-09-30 11:04:50 +0100
committerBram Moolenaar <Bram@vim.org>2022-09-30 11:04:50 +0100
commit6586a015144f15a979d573a79d91e700e4b3009f (patch)
treee276ff392ab6f428c169b2fb0b8646643109bd44
parentb152b6a40f729ed81a25d2fa541a4e73e201bec4 (diff)
patch 9.0.0627: "const" and "final" both make the type a constantv9.0.0627
Problem: "const" and "final" both make the type a constant. (Daniel Steinberg) Solution: Only have "const" make the type a constant.
-rw-r--r--src/testdir/test_vim9_builtin.vim14
-rw-r--r--src/version.c2
-rw-r--r--src/vim.h1
-rw-r--r--src/vim9.h4
-rw-r--r--src/vim9cmds.c9
-rw-r--r--src/vim9compile.c17
6 files changed, 36 insertions, 11 deletions
diff --git a/src/testdir/test_vim9_builtin.vim b/src/testdir/test_vim9_builtin.vim
index 4ebf65a1de..151ffedbfe 100644
--- a/src/testdir/test_vim9_builtin.vim
+++ b/src/testdir/test_vim9_builtin.vim
@@ -192,6 +192,13 @@ def Test_add_const()
v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const list<number>')
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,6 +1216,13 @@ def Test_extend_const()
END
v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const dict<number>')
+ 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<dict<any>> = [{n: 1}]
diff --git a/src/version.c b/src/version.c
index 40dad238d0..a2e66b2d4f 100644
--- 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 */
/**/
+ 627,
+/**/
626,
/**/
625,
diff --git a/src/vim.h b/src/vim.h
index 8f1bf3335b..14f67f7b24 100644
--- a/src/vim.h
+++ b/src/vim.h
@@ -2250,6 +2250,7 @@ typedef enum {
} 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"
diff --git a/src/vim9.h b/src/vim9.h
index 176fc642a4..fe193b1d61 100644
--- a/src/vim9.h
+++ b/src/vim9.h
@@ -697,7 +697,9 @@ typedef struct {
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_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;
diff --git a/src/vim9cmds.c b/src/vim9cmds.c
index c9c222c470..3c561d1ef5 100644
--- a/src/vim9cmds.c
+++ b/src/vim9cmds.c
@@ -881,7 +881,7 @@ compile_for(char_u *arg_start, cctx_T *cctx)
// 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);
+ loop_lvar = reserve_local(cctx, (char_u *)"", 0, ASSIGN_VAR, &t_number);
if (loop_lvar == NULL)
{
drop_scope(cctx);
@@ -894,7 +894,7 @@ compile_for(char_u *arg_start, cctx_T *cctx)
// 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);
+ funcref_lvar = reserve_local(cctx, (char_u *)"", 0, ASSIGN_VAR, &t_number);
if (funcref_lvar == NULL)
{
drop_scope(cctx);
@@ -1050,7 +1050,8 @@ compile_for(char_u *arg_start, cctx_T *cctx)
&& 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);
+ 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,7 +1183,7 @@ compile_while(char_u *arg, cctx_T *cctx)
// 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);
+ funcref_lvar = reserve_local(cctx, (char_u *)"", 0, ASSIGN_VAR, &t_number);
if (funcref_lvar == NULL)
{
drop_scope(cctx);
diff --git a/src/vim9compile.c b/src/vim9compile.c
index 5225345976..53c8e5a7ff 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -471,7 +471,7 @@ set_var_type(lvar_T *lvar, type_T *type_arg, cctx_T *cctx)
{
type_T *type = type_arg;
- if (lvar->lv_const && (type->tt_flags & TTFLAG_CONST) == 0)
+ 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,6 +487,8 @@ set_var_type(lvar_T *lvar, type_T *type_arg, cctx_T *cctx)
/*
* 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,7 +496,7 @@ reserve_local(
cctx_T *cctx,
char_u *name,
size_t len,
- int isConst,
+ int assign,
type_T *type)
{
lvar_T *lvar;
@@ -519,7 +521,7 @@ reserve_local(
lvar->lv_idx = dfunc->df_var_names.ga_len;
lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
- lvar->lv_const = isConst;
+ 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,7 +995,7 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx, garray_T *lines_to_free)
{
// Define a local variable for the function reference.
lvar = reserve_local(cctx, func_name, name_end - name_start,
- TRUE, ufunc->uf_func_type);
+ ASSIGN_CONST, ufunc->uf_func_type);
if (lvar == NULL)
goto theend;
if (generate_FUNCREF(cctx, ufunc, &funcref_isn) == FAIL)
@@ -1691,8 +1693,10 @@ compile_lhs(
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,
- cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
+ assign, lhs->lhs_type);
if (lhs->lhs_lvar == NULL)
return FAIL;
lhs->lhs_new_local = TRUE;
@@ -1769,7 +1773,8 @@ compile_assign_lhs(
return FAIL;
}
if (!is_decl && lhs->lhs_lvar != NULL
- && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
+ && lhs->lhs_lvar->lv_const != ASSIGN_VAR
+ && !lhs->lhs_has_index)
{
semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
return FAIL;