changeset 21905:2474c6b0c5de v8.2.1502

patch 8.2.1502: Vim9: can use += with a :let command at script level Commit: https://github.com/vim/vim/commit/122616d9c1b57f1e1f29f1151f8e26d24df9092a Author: Bram Moolenaar <Bram@vim.org> Date: Fri Aug 21 21:32:50 2020 +0200 patch 8.2.1502: Vim9: can use += with a :let command at script level Problem: Vim9: can use += with a :let command at script level. Solution: Give an error.
author Bram Moolenaar <Bram@vim.org>
date Fri, 21 Aug 2020 21:45:04 +0200
parents a0088959f40a
children b81818d441f7
files src/evalvars.c src/testdir/test_vim9_expr.vim src/testdir/test_vim9_script.vim src/version.c
diffstat 4 files changed, 13 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/evalvars.c
+++ b/src/evalvars.c
@@ -786,7 +786,13 @@ ex_let(exarg_T *eap)
 	    op[1] = NUL;
 	    if (*expr != '=')
 	    {
-		if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
+		if (vim9script && (flags & LET_NO_COMMAND) == 0)
+		{
+		    // +=, /=, etc. require an existing variable
+		    semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
+		    i = FAIL;
+		}
+		else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
 		{
 		    op[0] = *expr;   // +=, -=, *=, /=, %= or .=
 		    ++len;
--- a/src/testdir/test_vim9_expr.vim
+++ b/src/testdir/test_vim9_expr.vim
@@ -1682,8 +1682,6 @@ def Test_expr7_dict()
   call CheckDefFailure(["let x = {xxx: 8}"], 'E1001:', 1)
   call CheckDefFailure(["let x = #{a: 1, a: 2}"], 'E721:', 1)
   call CheckDefFailure(["let x = #"], 'E1015:', 1)
-  call CheckDefFailure(["let x += 1"], 'E1020:', 1)
-  call CheckDefFailure(["let x = x + 1"], 'E1001:', 1)
   call CheckDefExecFailure(["let x = g:anint.member"], 'E715:', 1)
   call CheckDefExecFailure(["let x = g:dict_empty.member"], 'E716:', 1)
 
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -569,7 +569,10 @@ def Test_assignment_failure()
   call CheckDefFailure(['let t:var = 5'], 'E1016: Cannot declare a tab variable:')
 
   call CheckDefFailure(['let anr = 4', 'anr ..= "text"'], 'E1019:')
-  call CheckDefFailure(['let xnr += 4'], 'E1020:')
+  call CheckDefFailure(['let xnr += 4'], 'E1020:', 1)
+  call CheckScriptFailure(['vim9script', 'let xnr += 4'], 'E1020:')
+  call CheckDefFailure(["let xnr = xnr + 1"], 'E1001:', 1)
+  call CheckScriptFailure(['vim9script', 'let xnr = xnr + 4'], 'E121:')
 
   call CheckScriptFailure(['vim9script', 'def Func()', 'let dummy = s:notfound', 'enddef', 'defcompile'], 'E1108:')
 
--- 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 */
 /**/
+    1502,
+/**/
     1501,
 /**/
     1500,