changeset 27519:7898c7847293 v8.2.4287

patch 8.2.4287: cannot assign empty list with type to variable with list type Commit: https://github.com/vim/vim/commit/2d3ac2e03093c4b0ae5d18c5f2f51ae0c2a9ec72 Author: Bram Moolenaar <Bram@vim.org> Date: Thu Feb 3 12:34:05 2022 +0000 patch 8.2.4287: cannot assign empty list with type to variable with list type 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.
author Bram Moolenaar <Bram@vim.org>
date Thu, 03 Feb 2022 13:45:03 +0100
parents 67c5f3cebf28
children 22f899e99b3f
files src/testdir/test_vim9_assign.vim src/testdir/test_vim9_func.vim src/version.c src/vim9type.c
diffstat 4 files changed, 19 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/testdir/test_vim9_assign.vim
+++ b/src/testdir/test_vim9_assign.vim
@@ -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
--- a/src/testdir/test_vim9_func.vim
+++ b/src/testdir/test_vim9_func.vim
@@ -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)
--- a/src/version.c
+++ b/src/version.c
@@ -747,6 +747,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    4287,
+/**/
     4286,
 /**/
     4285,
--- a/src/vim9type.c
+++ b/src/vim9type.c
@@ -344,7 +344,11 @@ typval2type_int(typval_T *tv, int copyID
 	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;