changeset 27968:40c6e5f849dc v8.2.4509

patch 8.2.4509: Vim9: can declare a variable with ":va" Commit: https://github.com/vim/vim/commit/e1d1211799bc37c063666e97437cf4e9af4782b0 Author: Bram Moolenaar <Bram@vim.org> Date: Sat Mar 5 11:37:48 2022 +0000 patch 8.2.4509: Vim9: can declare a variable with ":va" Problem: Vim9: can declare a variable with ":va". Solution: Disallow using ":va", require using ":var".
author Bram Moolenaar <Bram@vim.org>
date Sat, 05 Mar 2022 12:45:03 +0100
parents c91fca0baf52
children 0e58183b329e
files src/errors.h src/evalvars.c src/testdir/test_vim9_assign.vim src/version.c src/vim9compile.c
diffstat 5 files changed, 25 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/errors.h
+++ b/src/errors.h
@@ -2762,7 +2762,8 @@ EXTERN char e_type_mismatch_for_v_variab
 EXTERN char e_yank_register_changed_while_using_it[]
 	INIT(= N_("E1064: Yank register changed while using it"));
 #ifdef FEAT_EVAL
-// E1065 unused
+EXTERN char e_must_use_var_instead_of_va[]
+	INIT(= N_("E1065: Must use :var instead of :va"));
 EXTERN char e_cannot_declare_a_register_str[]
 	INIT(= N_("E1066: Cannot declare a register: %s"));
 EXTERN char e_separator_mismatch_str[]
--- a/src/evalvars.c
+++ b/src/evalvars.c
@@ -755,17 +755,24 @@ heredoc_get(exarg_T *eap, char_u *cmd, i
 ex_var(exarg_T *eap)
 {
     char_u *p = eap->cmd;
+    int	    has_var;
 
     if (!in_vim9script())
     {
 	semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
 	return;
     }
-    if (current_sctx.sc_sid == 0 && checkforcmd_noparen(&p, "var", 3))
+    has_var = checkforcmd_noparen(&p, "var", 3);
+    if (current_sctx.sc_sid == 0 && has_var)
     {
 	emsg(_(e_cannot_declare_variable_on_command_line));
 	return;
     }
+    if (eap->arg > eap->cmd && !has_var)
+    {
+	emsg(_(e_must_use_var_instead_of_va));
+	return;
+    }
     ex_let(eap);
 }
 
--- a/src/testdir/test_vim9_assign.vim
+++ b/src/testdir/test_vim9_assign.vim
@@ -1965,6 +1965,11 @@ def Test_var_declaration_fails()
   v9.CheckDefFailure(['var foo.bar = 2'], 'E1087:')
   v9.CheckDefFailure(['var foo[3] = 2'], 'E1087:')
   v9.CheckDefFailure(['const foo: number'], 'E1021:')
+
+  lines =<< trim END
+      va foo = 123
+  END
+  v9.CheckDefAndScriptFailure(lines, 'E1065:', 1)
 enddef
 
 def Test_var_declaration_inferred()
--- a/src/version.c
+++ b/src/version.c
@@ -755,6 +755,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    4509,
+/**/
     4508,
 /**/
     4507,
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -1883,7 +1883,14 @@ compile_assignment(char_u *arg, exarg_T 
     lhs_T	lhs;
     long	start_lnum = SOURCING_LNUM;
 
-    // Skip over the "var" or "[var, var]" to get to any "=".
+    p = eap->cmd;
+    if (eap->cmdidx == CMD_var && arg > p && !checkforcmd_noparen(&p, "var", 3))
+    {
+	emsg(_(e_must_use_var_instead_of_va));
+	return NULL;
+    }
+
+    // Skip over the "varname" or "[varname, varname]" to get to any "=".
     p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
     if (p == NULL)
 	return *arg == '[' ? arg : NULL;