# HG changeset patch # User Bram Moolenaar # Date 1605554104 -3600 # Node ID a8bccb0634bc15f85e3ec0a0375a89410a88b8d7 # Parent 09b160140ca324184d53139a59e46039f7c665c5 patch 8.2.1996: Vim9: invalid error for argument of extend() Commit: https://github.com/vim/vim/commit/193f6201b4d5c76f8dc1faa1dcf369575d93fe3a Author: Bram Moolenaar Date: Mon Nov 16 20:08:35 2020 +0100 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 https://github.com/vim/vim/issues/7299) diff --git a/src/evalfunc.c b/src/evalfunc.c --- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -295,7 +295,7 @@ arg_float_or_nr(type_T *type, argcontext 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 @@ arg_number(type_T *type, argcontext_T *c 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 @@ arg_same_as_prev(type_T *type, argcontex { 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 @@ arg_item_of_prev(type_T *type, argcontex // 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 --- 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 --- a/src/proto/vim9type.pro +++ b/src/proto/vim9type.pro @@ -15,6 +15,7 @@ int check_typval_type(type_T *expected, 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 --- a/src/testdir/test_vim9_builtin.vim +++ b/src/testdir/test_vim9_builtin.vim @@ -195,10 +195,16 @@ def Test_extend_arg_types() 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> + extend(res, map([1, 2], {_, v -> {}})) + assert_equal([{}, {}], res) CheckDefFailure(['extend([1, 2], 3)'], 'E1013: Argument 2: type mismatch, expected list but got number') CheckDefFailure(['extend([1, 2], ["x"])'], 'E1013: Argument 2: type mismatch, expected list but got list') @@ -338,6 +344,10 @@ def Test_index() 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 @@ def Test_insert() 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 --- a/src/version.c +++ b/src/version.c @@ -751,6 +751,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 1996, +/**/ 1995, /**/ 1994, diff --git a/src/vim9compile.c b/src/vim9compile.c --- a/src/vim9compile.c +++ b/src/vim9compile.c @@ -820,7 +820,7 @@ generate_TYPECHECK( * 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 --- a/src/vim9type.c +++ b/src/vim9type.c @@ -517,6 +517,20 @@ check_type(type_T *expected, type_T *act } /* + * 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. */