patch 8.2.1996: Vim9: invalid error for argument of extend()

Problem:    Vim9: invalid error for argument of extend().
Solution:   Check if the type could match. (closes #7299)
diff --git a/src/evalfunc.c b/src/evalfunc.c
index 8c090e3..d40f89f 100644
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -295,7 +295,7 @@
     static int
 arg_number(type_T *type, argcontext_T *context)
 {
-    return check_type(&t_number, type, TRUE, context->arg_idx + 1);
+    return check_arg_type(&t_number, type, context->arg_idx + 1);
 }
 
 /*
@@ -304,7 +304,7 @@
     static int
 arg_string(type_T *type, argcontext_T *context)
 {
-    return check_type(&t_string, type, TRUE, context->arg_idx + 1);
+    return check_arg_type(&t_string, type, context->arg_idx + 1);
 }
 
 /*
@@ -342,7 +342,7 @@
 {
     type_T *prev_type = context->arg_types[context->arg_idx - 1];
 
-    return check_type(prev_type, type, TRUE, context->arg_idx + 1);
+    return check_arg_type(prev_type, type, context->arg_idx + 1);
 }
 
 /*
@@ -363,7 +363,7 @@
 	// probably VAR_ANY, can't check
 	return OK;
 
-    return check_type(expected, type, TRUE, context->arg_idx + 1);
+    return check_arg_type(expected, type, context->arg_idx + 1);
 }
 
 /*
diff --git a/src/proto/vim9compile.pro b/src/proto/vim9compile.pro
index 49213c8..fbee55c 100644
--- a/src/proto/vim9compile.pro
+++ b/src/proto/vim9compile.pro
@@ -1,6 +1,7 @@
 /* vim9compile.c */
 int check_defined(char_u *p, size_t len, cctx_T *cctx);
 int check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2);
+int use_typecheck(type_T *actual, type_T *expected);
 int get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx);
 imported_T *find_imported(char_u *name, size_t len, cctx_T *cctx);
 imported_T *find_imported_in_script(char_u *name, size_t len, int sid);
diff --git a/src/proto/vim9type.pro b/src/proto/vim9type.pro
index e80f303..90f2345 100644
--- a/src/proto/vim9type.pro
+++ b/src/proto/vim9type.pro
@@ -15,6 +15,7 @@
 void type_mismatch(type_T *expected, type_T *actual);
 void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
 int check_type(type_T *expected, type_T *actual, int give_msg, int argidx);
+int check_arg_type(type_T *expected, type_T *actual, int argidx);
 char_u *skip_type(char_u *start, int optional);
 type_T *parse_type(char_u **arg, garray_T *type_gap);
 void common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap);
diff --git a/src/testdir/test_vim9_builtin.vim b/src/testdir/test_vim9_builtin.vim
index bbb7e85..cf12267 100644
--- a/src/testdir/test_vim9_builtin.vim
+++ b/src/testdir/test_vim9_builtin.vim
@@ -195,10 +195,16 @@
   assert_equal([1, 2, 3], extend([1, 2], [3]))
   assert_equal([3, 1, 2], extend([1, 2], [3], 0))
   assert_equal([1, 3, 2], extend([1, 2], [3], 1))
+  assert_equal([1, 3, 2], extend([1, 2], [3], s:number_one))
 
   assert_equal(#{a: 1, b: 2, c: 3}, extend(#{a: 1, b: 2}, #{c: 3}))
   assert_equal(#{a: 1, b: 4}, extend(#{a: 1, b: 2}, #{b: 4}))
   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}, s:string_keep))
+
+  var res: list<dict<any>>
+  extend(res, map([1, 2], {_, v -> {}}))
+  assert_equal([{}, {}], res)
 
   CheckDefFailure(['extend([1, 2], 3)'], 'E1013: Argument 2: type mismatch, expected list<number> but got number')
   CheckDefFailure(['extend([1, 2], ["x"])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>')
@@ -338,6 +344,10 @@
   index(['a', 'b', 'a', 'B'], 'b', 2, true)->assert_equal(3)
 enddef
 
+let s:number_one = 1
+let s:number_two = 2
+let s:string_keep = 'keep'
+
 def Test_insert()
   var l = insert([2, 1], 3)
   var res = 0
@@ -347,9 +357,12 @@
   res->assert_equal(6)
 
   assert_equal([1, 2, 3], insert([2, 3], 1))
+  assert_equal([1, 2, 3], insert([2, 3], s:number_one))
   assert_equal([1, 2, 3], insert([1, 2], 3, 2))
+  assert_equal([1, 2, 3], insert([1, 2], 3, s:number_two))
   assert_equal(['a', 'b', 'c'], insert(['b', 'c'], 'a'))
   assert_equal(0z1234, insert(0z34, 0x12))
+
   CheckDefFailure(['insert([2, 3], "a")'], 'E1013: Argument 2: type mismatch, expected number but got string', 1)
   CheckDefFailure(['insert([2, 3], 1, "x")'], 'E1013: Argument 3: type mismatch, expected number but got string', 1)
 enddef
diff --git a/src/version.c b/src/version.c
index 8de5308..7aadd45 100644
--- a/src/version.c
+++ b/src/version.c
@@ -751,6 +751,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1996,
+/**/
     1995,
 /**/
     1994,
diff --git a/src/vim9compile.c b/src/vim9compile.c
index 9a38f05..8dc9744 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -820,7 +820,7 @@
  * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
  * used.  Return FALSE if the types will never match.
  */
-    static int
+    int
 use_typecheck(type_T *actual, type_T *expected)
 {
     if (actual->tt_type == VAR_ANY
diff --git a/src/vim9type.c b/src/vim9type.c
index 27daf83..d655a63 100644
--- a/src/vim9type.c
+++ b/src/vim9type.c
@@ -517,6 +517,20 @@
 }
 
 /*
+ * Like check_type() but also allow for a runtime type check. E.g. "any" can be
+ * used for "number".
+ */
+    int
+check_arg_type(type_T *expected, type_T *actual, int argidx)
+{
+    if (check_type(expected, actual, FALSE, 0) == OK
+					    || use_typecheck(actual, expected))
+	return OK;
+    // TODO: should generate a TYPECHECK instruction.
+    return check_type(expected, actual, TRUE, argidx);
+}
+
+/*
  * Skip over a type definition and return a pointer to just after it.
  * When "optional" is TRUE then a leading "?" is accepted.
  */