]> granicus.if.org Git - vim/commitdiff
patch 8.2.0483: Vim9: "let x = x + 1" does not give an error v8.2.0483
authorBram Moolenaar <Bram@vim.org>
Mon, 30 Mar 2020 19:05:45 +0000 (21:05 +0200)
committerBram Moolenaar <Bram@vim.org>
Mon, 30 Mar 2020 19:05:45 +0000 (21:05 +0200)
Problem:    Vim9: "let x = x + 1" does not give an error.
Solution:   Hide the variable when compiling the expression.

src/testdir/test_vim9_expr.vim
src/version.c
src/vim9compile.c

index f61ff536ad28bef4ffd19c2e79c0cb06ecf5e809..770890ec35fb200c9b72f9de2c503a07e39e2e54 100644 (file)
@@ -738,6 +738,8 @@ def Test_expr7_dict()
   call CheckDefFailure("let x = {'a': xxx}", 'E1001:')
   call CheckDefFailure("let x = {xxx: 8}", 'E1001:')
   call CheckDefFailure("let x = #{a: 1, a: 2}", 'E721:')
+  call CheckDefFailure("let x += 1", 'E1020:')
+  call CheckDefFailure("let x = x + 1", 'E1001:')
   call CheckDefExecFailure("let x = g:anint.member", 'E715:')
   call CheckDefExecFailure("let x = g:dict_empty.member", 'E716:')
 enddef
index 2e8420c5eaffeeb34b5c04e852f14e18de579cb4..79d50ee42dc74662e8bfc05a3f06d063e0eb2541 100644 (file)
@@ -738,6 +738,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    483,
 /**/
     482,
 /**/
index 9d66e5ed15bf213230f9bb9be546b39a93dfe4fe..1e8cd03d02455e3c7eb346716e9b1084f0055938 100644 (file)
@@ -3685,6 +3685,8 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
     }
     else if (oplen > 0)
     {
+       int r;
+
        // for "+=", "*=", "..=" etc. first load the current value
        if (*op != '=')
        {
@@ -3717,10 +3719,16 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
            }
        }
 
-       // compile the expression
+       // Compile the expression.  Temporarily hide the new local variable
+       // here, it is not available to this expression.
+       if (idx >= 0)
+           --cctx->ctx_locals.ga_len;
        instr_count = instr->ga_len;
        p = skipwhite(p + oplen);
-       if (compile_expr1(&p, cctx) == FAIL)
+       r = compile_expr1(&p, cctx);
+       if (idx >= 0)
+           ++cctx->ctx_locals.ga_len;
+       if (r == FAIL)
            goto theend;
 
        if (idx >= 0 && (is_decl || !has_type))