patch 8.2.2687: Vim9: cannot use "const" for global variable in :def function
Problem: Vim9: cannot use "const" for global variable in :def function.
Solution: Do allow using :const for a global variable. (closes #8030)
diff --git a/src/testdir/test_vim9_assign.vim b/src/testdir/test_vim9_assign.vim
index 05e65e1..41ec7c7 100644
--- a/src/testdir/test_vim9_assign.vim
+++ b/src/testdir/test_vim9_assign.vim
@@ -1277,6 +1277,13 @@
g:FLIST[0] = 22
assert_equal([22], g:FLIST)
+ def SetGlobalConst()
+ const g:globConst = 123
+ enddef
+ SetGlobalConst()
+ assert_equal(123, g:globConst)
+ assert_true(islocked('g:globConst'))
+
const w:FOO: number = 46
assert_equal(46, w:FOO)
const w:FOOS = 'wfoos'
@@ -1341,6 +1348,17 @@
lines =<< trim END
vim9script
+ def SetGlobalConst()
+ const g:globConst = 123
+ enddef
+ SetGlobalConst()
+ g:globConst = 234
+ END
+ CheckScriptFailure(lines, 'E741: Value is locked: globConst')
+ unlet g:globConst
+
+ lines =<< trim END
+ vim9script
const cdict: dict<string> = {}
def Change()
cdict.foo = 'foo'
diff --git a/src/version.c b/src/version.c
index 68949e0..0fb8bc3 100644
--- a/src/version.c
+++ b/src/version.c
@@ -751,6 +751,8 @@
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 2687,
+/**/
2686,
/**/
2685,
diff --git a/src/vim9compile.c b/src/vim9compile.c
index fa73de2..ca1c89c 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -5752,7 +5752,8 @@
&lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
&lhs->lhs_type, cctx) == FAIL)
return FAIL;
- if (lhs->lhs_dest != dest_local)
+ if (lhs->lhs_dest != dest_local
+ && cmdidx != CMD_const && cmdidx != CMD_final)
{
// Specific kind of variable recognized.
declare_error = is_decl;
@@ -6519,6 +6520,7 @@
else
{
if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
+ || lhs.lhs_dest == dest_global
|| lhs.lhs_dest == dest_local))
// ":const var": lock the value, but not referenced variables
generate_LOCKCONST(cctx);
diff --git a/src/vim9execute.c b/src/vim9execute.c
index f15b93d..9b7f87b 100644
--- a/src/vim9execute.c
+++ b/src/vim9execute.c
@@ -963,9 +963,12 @@
store_var(char_u *name, typval_T *tv)
{
funccal_entry_T entry;
+ int flags = ASSIGN_DECL;
+ if (tv->v_lock)
+ flags |= ASSIGN_CONST;
save_funccal(&entry);
- set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL, 0);
+ set_var_const(name, NULL, tv, FALSE, flags, 0);
restore_funccal();
}