]> granicus.if.org Git - vim/commitdiff
patch 8.2.2687: Vim9: cannot use "const" for global variable in :def function v8.2.2687
authorBram Moolenaar <Bram@vim.org>
Thu, 1 Apr 2021 17:42:48 +0000 (19:42 +0200)
committerBram Moolenaar <Bram@vim.org>
Thu, 1 Apr 2021 17:42:48 +0000 (19:42 +0200)
Problem:    Vim9: cannot use "const" for global variable in :def function.
Solution:   Do allow using :const for a global variable. (closes #8030)

src/testdir/test_vim9_assign.vim
src/version.c
src/vim9compile.c
src/vim9execute.c

index 05e65e1ecfd4859fb9fbae2845562bde1d51d079..41ec7c7bec0f03e9be7e12109de9f51d6357073f 100644 (file)
@@ -1277,6 +1277,13 @@ def Test_var_declaration()
     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'
@@ -1339,6 +1346,17 @@ def Test_var_declaration_fails()
   CheckScriptFailure(lines, 'E741:')
   unlet g:constvar
 
+  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> = {}
index 68949e0e3cb5360e865c4050a48bf29f132d1b90..0fb8bc352386d24fac6714f5d7b594260809a192 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2687,
 /**/
     2686,
 /**/
index fa73de23da04f413245f3f2eec1eaeed47e5d566..ca1c89cc15815719276ab641d5397bf695509ab0 100644 (file)
@@ -5752,7 +5752,8 @@ compile_lhs(
                                      &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 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
        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);
index f15b93d867b515a14732d07ee05b9ea3b6b20278..9b7f87b6c13aade84b98fee9e25fb248eeb83ed9 100644 (file)
@@ -963,9 +963,12 @@ check_for_number(typval_T *tv)
 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();
 }