]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 8.2.4287: cannot assign empty list with type to variable with list type v8.2.4287
authorBram Moolenaar <Bram@vim.org>
Thu, 3 Feb 2022 12:34:05 +0000 (12:34 +0000)
committerBram Moolenaar <Bram@vim.org>
Thu, 3 Feb 2022 12:34:05 +0000 (12:34 +0000)
Problem:    Cannot assign empty list with any list type to variable with
            specific list type.
Solution:   Use unknown list type for empty list if the specified type is any.

src/testdir/test_vim9_assign.vim
src/testdir/test_vim9_func.vim
src/version.c
src/vim9type.c

index 73b331070f6f3a2f76c24f4136bbbf6dabade400..744fc5e715594385f816793d3d699127be0af39d 100644 (file)
@@ -1249,6 +1249,15 @@ def Test_assignment_var_list()
   v9.CheckScriptSuccess(lines)
 enddef
 
+def Test_assignment_empty_list()
+  var lines =<< trim END
+      var l2: list<any> = []
+      var l: list<string>
+      l = l2
+  END
+  v9.CheckDefAndScriptSuccess(lines)
+enddef
+
 def Test_assignment_vim9script()
   var lines =<< trim END
     vim9script
index 434d49bf97cb148eb4fa8731a1663ef98a9d3c72..55297cb652f40cb718aad043cf59553e41849b69 100644 (file)
@@ -3461,11 +3461,11 @@ def Test_list_any_type_checked()
       enddef
       Foo()
   END
+  # "any" could be "dict<any>", thus OK
   lines[2] = 'var l: list<any>'
-  v9.CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
-
+  v9.CheckScriptSuccess(lines)
   lines[2] = 'var l: list<any> = []'
-  v9.CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
+  v9.CheckScriptSuccess(lines)
 
   lines[2] = 'var l: list<any> = [11]'
   v9.CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<number>', 2)
index e6edb17be2a3354470553adc32c18f9982920a82..b53b4eb3dfaa19932ec04155ee4017fe6d3ebdcf 100644 (file)
@@ -746,6 +746,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    4287,
 /**/
     4286,
 /**/
index f184171c1131ad83785f8c19a5670cc7f5c90e3f..f72698cb9d50576d53cb78f434758f248834d33f 100644 (file)
@@ -344,7 +344,11 @@ typval2type_int(typval_T *tv, int copyID, garray_T *type_gap, int flags)
        list_T      *l = tv->vval.v_list;
        listitem_T  *li;
 
-       if (l == NULL || (l->lv_first == NULL && l->lv_type == NULL))
+       // An empty list has type list<unknown>, unless the type was specified
+       // and is not list<any>.  This matters when assigning to a variable
+       // with a specific list type.
+       if (l == NULL || (l->lv_first == NULL
+                  && (l->lv_type == NULL || l->lv_type->tt_member == &t_any)))
            return &t_list_empty;
        if ((flags & TVTT_DO_MEMBER) == 0)
            return &t_list_any;