]> granicus.if.org Git - vim/commitdiff
patch 8.2.1817: Vim9: wrong instruction when reusing a local variable spot v8.2.1817
authorBram Moolenaar <Bram@vim.org>
Fri, 9 Oct 2020 20:04:29 +0000 (22:04 +0200)
committerBram Moolenaar <Bram@vim.org>
Fri, 9 Oct 2020 20:04:29 +0000 (22:04 +0200)
Problem:    Vim9: wrong instruction when reusing a local variable spot.
Solution:   Clear a newly allocated local variable. (closes #7080)

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

index 28e503de78d50652c67fbab4c66001d5aeec7877..60894029044092fe324b72994045a7bc8bd81a20 100644 (file)
@@ -2032,5 +2032,23 @@ def Test_callstack_def()
   endtry
 enddef
 
+" Re-using spot for variable used in block
+def Test_block_scoped_var()
+  var lines =<< trim END
+      vim9script
+      def Func()
+        var x = ['a', 'b', 'c']
+        if 1
+          var y = 'x'
+          map(x, {-> y})
+        endif
+        var z = x
+        assert_equal(['x', 'x', 'x'], z)
+      enddef
+      Func()
+  END
+  CheckScriptSuccess(lines)
+enddef
+
 
 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
index 598222ae6a74d84868d9fd9703e65d24214aa7a1..5adfd967868e3614f06482422b8f19264af30548 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1817,
 /**/
     1816,
 /**/
index a6bdcc3f3df56c78a3c1988221045c6823540cbe..74be262a521bce15a4d7eb2a809397e85c7ed653 100644 (file)
@@ -1690,7 +1690,12 @@ generate_EXECCONCAT(cctx_T *cctx, int count)
  * Return the variable or NULL if it failed.
  */
     static lvar_T *
-reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
+reserve_local(
+       cctx_T  *cctx,
+       char_u  *name,
+       size_t  len,
+       int     isConst,
+       type_T  *type)
 {
     lvar_T  *lvar;
 
@@ -1703,6 +1708,7 @@ reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
     if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
        return NULL;
     lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
+    CLEAR_POINTER(lvar);
 
     // Every local variable uses the next entry on the stack.  We could re-use
     // the last ones when leaving a scope, but then variables used in a closure
@@ -4438,7 +4444,6 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx)
     char_u     *name_start = eap->arg;
     char_u     *name_end = to_name_end(eap->arg, TRUE);
     char_u     *lambda_name;
-    lvar_T     *lvar;
     ufunc_T    *ufunc;
     int                r;
 
@@ -4487,8 +4492,9 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx)
     else
     {
        // Define a local variable for the function reference.
-       lvar = reserve_local(cctx, name_start, name_end - name_start,
+       lvar_T  *lvar = reserve_local(cctx, name_start, name_end - name_start,
                                                    TRUE, ufunc->uf_func_type);
+
        if (lvar == NULL)
            return NULL;
        if (generate_FUNCREF(cctx, ufunc) == FAIL)