]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 8.2.2159: Vim9: when declaring a list it is not allocated yet v8.2.2159
authorBram Moolenaar <Bram@vim.org>
Fri, 18 Dec 2020 16:23:14 +0000 (17:23 +0100)
committerBram Moolenaar <Bram@vim.org>
Fri, 18 Dec 2020 16:23:14 +0000 (17:23 +0100)
Problem:    Vim9: when declaring a list it is not allocated yet, causing a
            following extend() to fail.
Solution:   When fetching a variable value for a list or dict that is null
            allocate the list or dict, so it can be used. (closes #7491)

src/testdir/test_vim9_assign.vim
src/version.c
src/vim9execute.c

index ad7b85b5fcd292bd9cae661b67896d913d5f9e00..c9872d4e6335e80d1b09050f89cf0e9d08597b62 100644 (file)
@@ -391,6 +391,16 @@ def Test_extend_list()
       assert_equal(['a', 'b'], list)
   END
   CheckScriptSuccess(lines)
+  lines =<< trim END
+      vim9script
+      var list: list<string>
+      def Func()
+        extend(list, ['x', 'b'])
+      enddef
+      Func()
+      assert_equal(['x', 'b'], list)
+  END
+  CheckScriptSuccess(lines)
 
   lines =<< trim END
       vim9script
@@ -584,8 +594,9 @@ def Test_assignment_dict()
       return test
     enddef
     FillDict()
+    assert_equal({a: 43}, test)
   END
-  CheckScriptFailure(lines, 'E1103:')
+  CheckScriptSuccess(lines)
 
   # assignment to global dict
   lines =<< trim END
index 96aa9b0043f2fa6dcaddef18e62419ef3af3f38c..c9d8665334bf8cc9cb356e2a69d8bf8a86c35f51 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2159,
 /**/
     2158,
 /**/
index 351ea8b3b17d0d032483fa328d4d9a1868d3ce95..64b42cb05b81846ba80e1cd7629c6dc0869c8ec4 100644 (file)
@@ -791,6 +791,26 @@ store_var(char_u *name, typval_T *tv)
     restore_funccal();
 }
 
+/*
+ * When the value of "sv" is a null list of dict, allocate it.
+ */
+    static void
+allocate_if_null(typval_T *tv)
+{
+    switch (tv->v_type)
+    {
+       case VAR_LIST:
+           if (tv->vval.v_list == NULL)
+               rettv_list_alloc(tv);
+           break;
+       case VAR_DICT:
+           if (tv->vval.v_dict == NULL)
+               rettv_dict_alloc(tv);
+           break;
+       default:
+           break;
+    }
+}
 
 /*
  * Execute a function by "name".
@@ -1289,6 +1309,7 @@ call_def_function(
 
                    sv = ((svar_T *)si->sn_var_vals.ga_data)
                                             + iptr->isn_arg.script.script_idx;
+                   allocate_if_null(sv->sv_tv);
                    if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
                        goto failed;
                    copy_tv(sv->sv_tv, STACK_TV_BOT(0));