]> granicus.if.org Git - vim/commitdiff
patch 9.0.0444: trying to declare g:variable gives confusing error v9.0.0444
authorBram Moolenaar <Bram@vim.org>
Sun, 11 Sep 2022 14:14:05 +0000 (15:14 +0100)
committerBram Moolenaar <Bram@vim.org>
Sun, 11 Sep 2022 14:14:05 +0000 (15:14 +0100)
Problem:    Trying to declare g:variable gives confusing error.
Solution:   Give a better error message. (closes #11108)

src/errors.h
src/eval.c
src/evalvars.c
src/ex_docmd.c
src/proto/evalvars.pro
src/testdir/test_vim9_assign.vim
src/testdir/test_vim9_cmd.vim
src/testdir/test_vim9_script.vim
src/version.c

index d1cf4b457f9fbded0dfd9191f3b7c339ad2ceed6..5f0fdcb8a845492ccdae0fb1f932b005e3405e05 100644 (file)
@@ -3335,4 +3335,6 @@ EXTERN char e_script_variable_was_deleted[]
        INIT(= N_("E1302: Script variable was deleted"));
 EXTERN char e_custom_list_completion_function_does_not_return_list_but_str[]
        INIT(= N_("E1303: Custom list completion function does not return a List but a %s"));
+EXTERN char e_cannot_use_type_with_this_variable_str[]
+       INIT(= N_("E1304: Cannot use type with this variable: %s"));
 #endif
index f208ab100961555e8f9426cc0a76092d9beb5b28..5d93b7e50c1193c3e057890a4589e9665d6fed53 100644 (file)
@@ -1031,12 +1031,16 @@ get_lval(
            {
                char_u      *tp = skipwhite(p + 1);
 
+               if (is_scoped_variable(name))
+               {
+                   semsg(_(e_cannot_use_type_with_this_variable_str), name);
+                   return NULL;
+               }
                if (tp == p + 1 && !quiet)
                {
                    semsg(_(e_white_space_required_after_str_str), ":", p);
                    return NULL;
                }
-
                if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
                {
                    semsg(_(e_using_type_not_in_script_context_str), p);
index facafc7dd2a62a170a582152254dd32d2965ff01..7de785bc4b85d396cf3f89a44e86280f78e8250e 100644 (file)
@@ -602,6 +602,18 @@ list_script_vars(int *first)
                                                           "s:", FALSE, first);
 }
 
+/*
+ * Return TRUE if "name" starts with "g:", "w:", "t:" or "b:".
+ * But only when an identifier character follows.
+ */
+    int
+is_scoped_variable(char_u *name)
+{
+    return vim_strchr((char_u *)"gwbt", name[0]) != NULL
+       && name[1] == ':'
+       && eval_isnamec(name[2]);
+}
+
 /*
  * Evaluate one Vim expression {expr} in string "p" and append the
  * resulting string to "gap".  "p" points to the opening "{".
@@ -3679,8 +3691,7 @@ set_var_const(
        vim9_declare_error(name);
        goto failed;
     }
-    if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
-                             && vim_strchr((char_u *)"gwbt", name[0]) != NULL)
+    if ((flags & ASSIGN_FOR_LOOP) && is_scoped_variable(name))
        // Do not make g:var, w:var, b:var or t:var final.
        flags &= ~ASSIGN_FINAL;
 
index 0024c99c637826aaff8c491f637268f47ffe5a19..0e5e1db5ecd8793e3c7f9ad02e042ed7ac69012d 100644 (file)
@@ -3761,11 +3761,11 @@ find_ex_command(
                }
            }
 
-           // Recognize using a type for a w:, b:, t: or g: variable:
+           // Recognize trying to use a type for a w:, b:, t: or g: variable:
            // "w:varname: number = 123".
            if (eap->cmd[1] == ':' && *p == ':')
            {
-               eap->cmdidx = CMD_eval;
+               eap->cmdidx = CMD_var;
                return eap->cmd;
            }
        }
index 253af6de6c779fe6c0a3f555503b86a09d25889e..b8b0053422f3aebad57bfee648d263cd9eb15148 100644 (file)
@@ -13,6 +13,7 @@ list_T *eval_spell_expr(char_u *badword, char_u *expr);
 int get_spellword(list_T *list, char_u **pp);
 void prepare_vimvar(int idx, typval_T *save_tv);
 void restore_vimvar(int idx, typval_T *save_tv);
+int is_scoped_variable(char_u *name);
 char_u *eval_one_expr_in_str(char_u *p, garray_T *gap, int evaluate);
 list_T *heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile);
 void ex_var(exarg_T *eap);
index 58dde3f1fe0b74847937efc8615069da8d31da93..11d7425e6570287f4f1b611bd44e96e933413681 100644 (file)
@@ -1597,13 +1597,31 @@ def Test_assignment_failure()
   v9.CheckDefFailure(['var name: dict<number'], 'E1009:')
 
   v9.CheckDefFailure(['w:foo: number = 10'],
-                  'E488: Trailing characters: : number = 1')
+                  'E1016: Cannot declare a window variable: w:foo')
   v9.CheckDefFailure(['t:foo: bool = true'],
-                  'E488: Trailing characters: : bool = true')
+                  'E1016: Cannot declare a tab variable: t:foo')
   v9.CheckDefFailure(['b:foo: string = "x"'],
-                  'E488: Trailing characters: : string = "x"')
+                  'E1016: Cannot declare a buffer variable: b:foo')
   v9.CheckDefFailure(['g:foo: number = 123'],
-                  'E488: Trailing characters: : number = 123')
+                  'E1016: Cannot declare a global variable: g:foo')
+
+  v9.CheckScriptFailure(['vim9script', 'w:foo: number = 123'],
+                  'E1304: Cannot use type with this variable: w:foo:')
+  v9.CheckScriptFailure(['vim9script', 't:foo: number = 123'],
+                  'E1304: Cannot use type with this variable: t:foo:')
+  v9.CheckScriptFailure(['vim9script', 'b:foo: number = 123'],
+                  'E1304: Cannot use type with this variable: b:foo:')
+  v9.CheckScriptFailure(['vim9script', 'g:foo: number = 123'],
+                  'E1304: Cannot use type with this variable: g:foo:')
+
+  v9.CheckScriptFailure(['vim9script', 'const w:FOO: number = 123'],
+                  'E1304: Cannot use type with this variable: w:FOO:')
+  v9.CheckScriptFailure(['vim9script', 'const t:FOO: number = 123'],
+                  'E1304: Cannot use type with this variable: t:FOO:')
+  v9.CheckScriptFailure(['vim9script', 'const b:FOO: number = 123'],
+                  'E1304: Cannot use type with this variable: b:FOO:')
+  v9.CheckScriptFailure(['vim9script', 'const g:FOO: number = 123'],
+                  'E1304: Cannot use type with this variable: g:FOO:')
 enddef
 
 def Test_assign_list()
@@ -1959,8 +1977,6 @@ def Test_var_declaration()
     FLIST[0] = 11
     assert_equal([11], FLIST)
 
-    const g:FOO: number = 321
-    assert_equal(321, g:FOO)
     const g:FOOS = 'gfoos'
     assert_equal('gfoos', g:FOOS)
     final g:FLIST = [2]
@@ -1975,8 +1991,6 @@ def Test_var_declaration()
     assert_equal(123, g:globConst)
     assert_true(islocked('g:globConst'))
 
-    const w:FOO: number = 46
-    assert_equal(46, w:FOO)
     const w:FOOS = 'wfoos'
     assert_equal('wfoos', w:FOOS)
     final w:FLIST = [3]
@@ -2015,10 +2029,8 @@ def Test_var_declaration()
   unlet g:var_prefixed
   unlet g:other_var
   unlet g:globConst
-  unlet g:FOO
   unlet g:FOOS
   unlet g:FLIST
-  unlet w:FOO
   unlet w:FOOS
   unlet w:FLIST
 enddef
index 636769d3abe7b59c980a7cda7e22ca22b153b973..80b78205aef94cc557017acf03ec405b4681d454 100644 (file)
@@ -1936,7 +1936,7 @@ def Test_var_not_cmd()
   var lines =<< trim END
       g:notexist:cmd
   END
-  v9.CheckDefAndScriptFailure(lines, ['E488: Trailing characters: :cmd', 'E121: Undefined variable: g:notexist'], 1)
+  v9.CheckDefAndScriptFailure(lines, ['E1016: Cannot declare a global variable: g:notexist', "E1069: White space required after ':'"], 1)
 
   lines =<< trim END
       g-pat-cmd
@@ -1950,7 +1950,7 @@ def Test_var_not_cmd()
   lines =<< trim END
       s:notexist:repl
   END
-  v9.CheckDefAndScriptFailure(lines, ['E488: Trailing characters: :repl', 'E1268:'], 1)
+  v9.CheckDefAndScriptFailure(lines, ['E1101: Cannot declare a script variable in a function: s:notexist', "E1069: White space required after ':'"], 1)
 
   lines =<< trim END
       notexist:repl
index aeee14329720a077beb19533e196443b464092d0..f746ca17823181ad02a0ed3ee2521fc791c17ea9 100644 (file)
@@ -4241,13 +4241,12 @@ func Test_misplaced_type()
 endfunc
 
 def Run_Test_misplaced_type()
-  writefile(['let g:somevar = "asdf"'], 'XTest_misplaced_type')
+  writefile(['let g:somevar = "asdf"'], 'XTest_misplaced_type', 'D')
   var buf = g:RunVimInTerminal('-S XTest_misplaced_type', {'rows': 6})
-  term_sendkeys(buf, ":vim9cmd echo islocked('g:somevar: string')\<CR>")
+  term_sendkeys(buf, ":vim9cmd echo islocked('somevar: string')\<CR>")
   g:VerifyScreenDump(buf, 'Test_misplaced_type', {})
 
   g:StopVimInTerminal(buf)
-  delete('XTest_misplaced_type')
 enddef
 
 " Ensure echo doesn't crash when stringifying empty variables.
index 26c1697c33382dd356c2b004737335008ceaeccf..8fee9776eca033d394b08131097c5a9eef80b019 100644 (file)
@@ -703,6 +703,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    444,
 /**/
     443,
 /**/