diff options
author | Bram Moolenaar <Bram@vim.org> | 2022-03-05 11:37:48 +0000 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2022-03-05 11:37:48 +0000 |
commit | e1d1211799bc37c063666e97437cf4e9af4782b0 (patch) | |
tree | 1a43103320c16459551d6ab64c4d02afd1947452 | |
parent | 330a388e18ad6b64c3574c712db675258b0e9878 (diff) | |
download | vim-git-e1d1211799bc37c063666e97437cf4e9af4782b0.tar.gz |
patch 8.2.4509: Vim9: can declare a variable with ":va"v8.2.4509
Problem: Vim9: can declare a variable with ":va".
Solution: Disallow using ":va", require using ":var".
-rw-r--r-- | src/errors.h | 3 | ||||
-rw-r--r-- | src/evalvars.c | 9 | ||||
-rw-r--r-- | src/testdir/test_vim9_assign.vim | 5 | ||||
-rw-r--r-- | src/version.c | 2 | ||||
-rw-r--r-- | src/vim9compile.c | 9 |
5 files changed, 25 insertions, 3 deletions
diff --git a/src/errors.h b/src/errors.h index 6f25af6af..82708d3bf 100644 --- a/src/errors.h +++ b/src/errors.h @@ -2762,7 +2762,8 @@ EXTERN char e_type_mismatch_for_v_variable[] 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[] diff --git a/src/evalvars.c b/src/evalvars.c index 40e643b9f..56ac3daa7 100644 --- a/src/evalvars.c +++ b/src/evalvars.c @@ -755,17 +755,24 @@ heredoc_get(exarg_T *eap, char_u *cmd, int script_get) 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); } diff --git a/src/testdir/test_vim9_assign.vim b/src/testdir/test_vim9_assign.vim index ccf2a01a0..6f6433493 100644 --- 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() diff --git a/src/version.c b/src/version.c index 5bbd307c7..999f8ac43 100644 --- 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, diff --git a/src/vim9compile.c b/src/vim9compile.c index 898712396..52c60fb9c 100644 --- a/src/vim9compile.c +++ b/src/vim9compile.c @@ -1883,7 +1883,14 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) 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; |