diff options
author | Bram Moolenaar <Bram@vim.org> | 2020-11-16 20:08:35 +0100 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2020-11-16 20:08:35 +0100 |
commit | 193f6201b4d5c76f8dc1faa1dcf369575d93fe3a (patch) | |
tree | 6e4225c94d9d04cb33244ac4d87c4c6f9f53b787 | |
parent | 714cbe5b212abbecb578b90424d89f47142e8f25 (diff) | |
download | vim-git-193f6201b4d5c76f8dc1faa1dcf369575d93fe3a.tar.gz |
patch 8.2.1996: Vim9: invalid error for argument of extend()v8.2.1996
Problem: Vim9: invalid error for argument of extend().
Solution: Check if the type could match. (closes #7299)
-rw-r--r-- | src/evalfunc.c | 8 | ||||
-rw-r--r-- | src/proto/vim9compile.pro | 1 | ||||
-rw-r--r-- | src/proto/vim9type.pro | 1 | ||||
-rw-r--r-- | src/testdir/test_vim9_builtin.vim | 13 | ||||
-rw-r--r-- | src/version.c | 2 | ||||
-rw-r--r-- | src/vim9compile.c | 2 | ||||
-rw-r--r-- | src/vim9type.c | 14 |
7 files changed, 36 insertions, 5 deletions
diff --git a/src/evalfunc.c b/src/evalfunc.c index 8c090e361..d40f89f08 100644 --- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -295,7 +295,7 @@ arg_float_or_nr(type_T *type, argcontext_T *context) 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 *context) 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, argcontext_T *context) { 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, argcontext_T *context) // 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 49213c839..fbee55c95 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 e80f303a4..90f2345b8 100644 --- a/src/proto/vim9type.pro +++ b/src/proto/vim9type.pro @@ -15,6 +15,7 @@ int check_typval_type(type_T *expected, typval_T *actual_tv, int argidx); 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 bbb7e85d0..cf122673c 100644 --- 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<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 @@ 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 index 8de530879..7aadd454f 100644 --- 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 index 9a38f056a..8dc974422 100644 --- 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 index 27daf8366..d655a63db 100644 --- a/src/vim9type.c +++ b/src/vim9type.c @@ -517,6 +517,20 @@ check_type(type_T *expected, type_T *actual, int give_msg, int argidx) } /* + * 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. */ |