diff options
author | Bram Moolenaar <Bram@vim.org> | 2022-02-20 18:26:46 +0000 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2022-02-20 18:26:46 +0000 |
commit | c1e6c7bafec1115b690c745a28e1a6338750b137 (patch) | |
tree | 2299cd11918b10569a201ef2f151afbf4ce4602f /src | |
parent | eddd4fc4f6d9d626374a73861c891f95ef999133 (diff) | |
download | vim-git-c1e6c7bafec1115b690c745a28e1a6338750b137.tar.gz |
patch 8.2.4426: map() function on string and blob does not check typesv8.2.4426
Problem: map() function on string and blob does not check argument types at
compile time.
Solution: Check string and blob argument types. Support "0z1234->func()".
Diffstat (limited to 'src')
-rw-r--r-- | src/evalfunc.c | 6 | ||||
-rw-r--r-- | src/ex_docmd.c | 5 | ||||
-rw-r--r-- | src/testdir/test_vim9_builtin.vim | 34 | ||||
-rw-r--r-- | src/version.c | 2 | ||||
-rw-r--r-- | src/vim9compile.c | 7 |
5 files changed, 47 insertions, 7 deletions
diff --git a/src/evalfunc.c b/src/evalfunc.c index 86e5d2f40..ce51fc017 100644 --- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -566,9 +566,11 @@ arg_map_func(type_T *type, type_T *decl_type UNUSED, argcontext_T *context) t_func_exp.tt_argcount = -1; else { - if (context->arg_types[0].type_decl->tt_type == VAR_LIST) + if (context->arg_types[0].type_curr->tt_type == VAR_STRING + || context->arg_types[0].type_curr->tt_type == VAR_BLOB + || context->arg_types[0].type_curr->tt_type == VAR_LIST) args[0] = &t_number; - else if (context->arg_types[0].type_decl->tt_type == VAR_DICT) + else if (context->arg_types[0].type_curr->tt_type == VAR_DICT) args[0] = &t_string; if (args[0] != NULL) args[1] = expected_ret; diff --git a/src/ex_docmd.c b/src/ex_docmd.c index f967e6e33..36a7386eb 100644 --- a/src/ex_docmd.c +++ b/src/ex_docmd.c @@ -3457,7 +3457,8 @@ find_ex_command( char_u *pskip = skip_option_env_lead(eap->cmd); if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL - || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)) + || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL) + || (p[0] == '0' && p[1] == 'z')) { int oplen; int heredoc; @@ -3503,6 +3504,8 @@ find_ex_command( // "'string'->func()" is an expression. || *eap->cmd == '\'' // '"string"->func()' is an expression. + || (eap->cmd[0] == '0' && eap->cmd[1] == 'z') + // '"string"->func()' is an expression. || *eap->cmd == '"' // "g:varname" is an expression. || eap->cmd[1] == ':' diff --git a/src/testdir/test_vim9_builtin.vim b/src/testdir/test_vim9_builtin.vim index 882c1811a..41060e1a2 100644 --- a/src/testdir/test_vim9_builtin.vim +++ b/src/testdir/test_vim9_builtin.vim @@ -2360,13 +2360,33 @@ def Test_maparg() v9.CheckDefAndScriptFailure(['maparg("a", "b", true, 2)'], ['E1013: Argument 4: type mismatch, expected bool but got number', 'E1212: Bool required for argument 4']) maparg('')->assert_equal('') + # value argument type is checked at compile time var lines =<< trim END var l = [123] - l->map((_, v: string) => 0) + l->map((i: number, v: string) => 0) END - v9.CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected func(?number, ?number): number but got func(any, string): number') + v9.CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected func(?number, ?number): number but got func(number, string): number') lines =<< trim END + var d = {a: 123} + d->map((i: string, v: string) => 0) + END + v9.CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected func(?string, ?number): number but got func(string, string): number') + + lines =<< trim END + var s = 'abc' + s->map((i: number, v: number) => 'x') + END + v9.CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected func(?number, ?string): string but got func(number, number): string') + + lines =<< trim END + var s = 0z1122 + s->map((i: number, v: string) => 0) + END + v9.CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected func(?number, ?number): number but got func(number, string): number') + + # index argument type is checked at compile time + lines =<< trim END ['x']->map((i: string, v: string) => 'y') END v9.CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected func(?number, ?any): any but got func(string, string): string') @@ -2375,6 +2395,16 @@ def Test_maparg() {a: 1}->map((i: number, v: number) => 0) END v9.CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected func(?string, ?any): any but got func(number, number): number') + + lines =<< trim END + 'abc'->map((i: string, v: string) => 'x') + END + v9.CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected func(?number, ?string): string but got func(string, string): string') + + lines =<< trim END + 0z1122->map((i: string, v: number) => 0) + END + v9.CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected func(?number, ?number): number but got func(string, number): number') enddef def Test_maparg_mapset() diff --git a/src/version.c b/src/version.c index 1fdf4e47d..6c5ca2bdd 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 */ /**/ + 4426, +/**/ 4425, /**/ 4424, diff --git a/src/vim9compile.c b/src/vim9compile.c index 74dc5e0b0..803f19c08 100644 --- a/src/vim9compile.c +++ b/src/vim9compile.c @@ -2818,13 +2818,16 @@ compile_def_function( /* * COMMAND after range * 'text'->func() should not be confused with 'a mark + * 0z1234->func() should not be confused with a zero line number * "++nr" and "--nr" are eval commands * in "$ENV->func()" the "$" is not a range */ cmd = ea.cmd; if ((*cmd != '$' || starts_with_colon) - && (starts_with_colon || !(*cmd == '\'' - || (cmd[0] != NUL && cmd[0] == cmd[1] + && (starts_with_colon + || !(*cmd == '\'' + || (cmd[0] == '0' && cmd[1] == 'z') + || (cmd[0] != NUL && cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-'))))) { ea.cmd = skip_range(ea.cmd, TRUE, NULL); |