]> granicus.if.org Git - vim/commitdiff
patch 8.2.3994: Vim9: extend() complains about type when it was not declared v8.2.3994
authorBram Moolenaar <Bram@vim.org>
Mon, 3 Jan 2022 16:52:28 +0000 (16:52 +0000)
committerBram Moolenaar <Bram@vim.org>
Mon, 3 Jan 2022 16:52:28 +0000 (16:52 +0000)
Problem:    Vim9: extend() complains about the type even when it was not
            declared.
Solution:   Only check the list or dict type when it was declared.

src/list.c
src/testdir/test_vim9_builtin.vim
src/version.c

index 6b275789d502134531b6019d14c2a8c1c4505f0f..aadc7233d0359c0bf908298edcb65c55bea16ef9 100644 (file)
@@ -2758,25 +2758,26 @@ list_extend_func(
 extend(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg, int is_new)
 {
     type_T     *type = NULL;
-    garray_T   type_list;
     char       *func_name = is_new ? "extendnew()" : "extend()";
 
-    if (!is_new && in_vim9script())
-    {
-       // Check that extend() does not change the type of the dict.
-       ga_init2(&type_list, sizeof(type_T *), 10);
-       type = typval2type(argvars, get_copyID(), &type_list, TVTT_DO_MEMBER);
-    }
-
     if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
+    {
+       // Check that extend() does not change the type of the list if it was
+       // declared.
+       if (!is_new && in_vim9script() && argvars[0].vval.v_list != NULL)
+           type = argvars[0].vval.v_list->lv_type;
        list_extend_func(argvars, type, func_name, arg_errmsg, is_new, rettv);
+    }
     else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
+    {
+       // Check that extend() does not change the type of the list if it was
+       // declared.
+       if (!is_new && in_vim9script() && argvars[0].vval.v_dict != NULL)
+           type = argvars[0].vval.v_dict->dv_type;
        dict_extend_func(argvars, type, func_name, arg_errmsg, is_new, rettv);
+    }
     else
        semsg(_(e_argument_of_str_must_be_list_or_dictionary), func_name);
-
-    if (type != NULL)
-       clear_type_list(&type_list);
 }
 
 /*
index df27dba6f2eddc86329d2357e1a36101f381fbf8..7b835100c2c9a665b29b93d96a0cba850f54a1fb 100644 (file)
@@ -974,15 +974,25 @@ def Test_extend_arg_types()
       assert_equal({a: 1, b: 2}, extend({a: 1, b: 2}, {b: 4}, 'keep'))
       assert_equal({a: 1, b: 2}, extend({a: 1, b: 2}, {b: 4}, g:string_keep))
 
+      # mix of types is OK without a declaration
+
       var res: list<dict<any>>
       extend(res, mapnew([1, 2], (_, v) => ({})))
       assert_equal([{}, {}], res)
   END
   CheckDefAndScriptSuccess(lines)
 
+  # FIXME: this should not fail when compiled
+  lines =<< trim END
+      vim9script
+      assert_equal([1, 2, "x"], extend([1, 2], ["x"]))
+      assert_equal([1, "b", 1], extend([1], ["b", 1]))
+  END
+  CheckScriptSuccess(lines)
+
   CheckDefAndScriptFailure(['extend("a", 1)'], ['E1013: Argument 1: type mismatch, expected list<any> but got string', 'E712: Argument of extend() must be a List or Dictionary'])
   CheckDefAndScriptFailure(['extend([1, 2], 3)'], ['E1013: Argument 2: type mismatch, expected list<number> but got number', 'E712: Argument of extend() must be a List or Dictionary'])
-  CheckDefAndScriptFailure(['extend([1, 2], ["x"])'], ['E1013: Argument 2: type mismatch, expected list<number> but got list<string>', 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>'])
+  CheckDefAndScriptFailure(['var ll = [1, 2]', 'extend(ll, ["x"])'], ['E1013: Argument 2: type mismatch, expected list<number> but got list<string>', 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>'])
   CheckDefFailure(['extend([1, 2], [3], "x")'], 'E1013: Argument 3: type mismatch, expected number but got string')
 
   CheckDefFailure(['extend({a: 1}, 42)'], 'E1013: Argument 2: type mismatch, expected dict<number> but got number')
@@ -992,7 +1002,7 @@ def Test_extend_arg_types()
   CheckDefFailure(['extend([1], ["b"])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>')
   CheckDefExecFailure(['extend([1], ["b", 1])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<any>')
 
-  CheckScriptFailure(['vim9script', 'extend([1], ["b", 1])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<any> in extend()')
+  CheckScriptFailure(['vim9script', 'var l = [1]', 'extend(l, ["b", 1])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<any> in extend()')
 enddef
 
 func g:ExtendDict(d)
index b7bc986912fa197c79b7049381901f55bb4d5280..04f06f93151db4814aa0f0ead63be19ad40c3f32 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    3994,
 /**/
     3993,
 /**/