diff options
author | Bram Moolenaar <Bram@vim.org> | 2020-04-28 21:29:34 +0200 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2020-04-28 21:29:34 +0200 |
commit | a0a9f43ab24928a0b01b6d91d084bf50a6dbefc2 (patch) | |
tree | 23f7f1885c3c3d3531be9ee598d9ef8e88a1d57e | |
parent | 7ed8f59ae09278c666f1b43488351f9450097372 (diff) | |
download | vim-git-a0a9f43ab24928a0b01b6d91d084bf50a6dbefc2.tar.gz |
patch 8.2.0657: Vim9: no check if called variable is a FuncRefv8.2.0657
Problem: Vim9: no check if called variable is a FuncRef.
Solution: Add a type check.
-rw-r--r-- | src/testdir/test_vim9_expr.vim | 6 | ||||
-rw-r--r-- | src/testdir/test_vim9_script.vim | 3 | ||||
-rw-r--r-- | src/version.c | 2 | ||||
-rw-r--r-- | src/vim9compile.c | 37 |
4 files changed, 42 insertions, 6 deletions
diff --git a/src/testdir/test_vim9_expr.vim b/src/testdir/test_vim9_expr.vim index 463227257..ace3d2f25 100644 --- a/src/testdir/test_vim9_expr.vim +++ b/src/testdir/test_vim9_expr.vim @@ -213,6 +213,12 @@ def Test_expr4_equal() assert_equal(true, function('g:Test_expr4_equal', [123]) == function('g:Test_expr4_equal', [123])) assert_equal(false, function('g:Test_expr4_equal', [123]) == function('g:Test_expr4_is', [123])) assert_equal(false, function('g:Test_expr4_equal', [123]) == function('g:Test_expr4_equal', [999])) + + let OneFunc: func + let TwoFunc: func + OneFunc = function('len') + TwoFunc = function('len') + assert_equal(true, OneFunc('abc') == TwoFunc('123')) enddef " test != comperator diff --git a/src/testdir/test_vim9_script.vim b/src/testdir/test_vim9_script.vim index 4c821ec51..bc1f87377 100644 --- a/src/testdir/test_vim9_script.vim +++ b/src/testdir/test_vim9_script.vim @@ -345,6 +345,9 @@ func Test_wrong_type() call CheckDefFailure(['let var: pam'], 'E1010:') call CheckDefFailure(['let var: sam'], 'E1010:') call CheckDefFailure(['let var: vim'], 'E1010:') + + call CheckDefFailure(['let Ref: number', 'Ref()'], 'E1085:') + call CheckDefFailure(['let Ref: string', 'let res = Ref()'], 'E1085:') endfunc func Test_const() diff --git a/src/version.c b/src/version.c index ebe266eda..eac5c9adf 100644 --- a/src/version.c +++ b/src/version.c @@ -747,6 +747,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 657, +/**/ 656, /**/ 655, diff --git a/src/vim9compile.c b/src/vim9compile.c index 89a86779a..aa3cd07d3 100644 --- a/src/vim9compile.c +++ b/src/vim9compile.c @@ -1327,15 +1327,32 @@ generate_UCALL(cctx_T *cctx, char_u *name, int argcount) /* * Generate an ISN_PCALL instruction. + * "type" is the type of the FuncRef. */ static int -generate_PCALL(cctx_T *cctx, int argcount, int at_top) +generate_PCALL( + cctx_T *cctx, + int argcount, + char_u *name, + type_T *type, + int at_top) { isn_T *isn; garray_T *stack = &cctx->ctx_type_stack; + type_T *ret_type; RETURN_OK_IF_SKIP(cctx); + if (type->tt_type == VAR_ANY) + ret_type = &t_any; + else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) + ret_type = type->tt_member; + else + { + semsg(_("E1085: Not a callable type: %s"), name); + return FAIL; + } + if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) return FAIL; isn->isn_arg.pfunc.cpf_top = at_top; @@ -1344,7 +1361,7 @@ generate_PCALL(cctx_T *cctx, int argcount, int at_top) stack->ga_len -= argcount; // drop the arguments // drop the funcref/partial, get back the return value - ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any; + ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; // If partial is above the arguments it must be cleared and replaced with // the return value. @@ -2465,12 +2482,16 @@ compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init) if (STRNCMP(namebuf, "g:", 2) != 0 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK) { - res = generate_PCALL(cctx, argcount, FALSE); + garray_T *stack = &cctx->ctx_type_stack; + type_T *type; + + type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; + res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); goto theend; } // A global function may be defined only later. Need to figure out at - // runtime. + // runtime. Also handles a FuncRef at runtime. if (STRNCMP(namebuf, "g:", 2) == 0) res = generate_UCALL(cctx, name, argcount); else @@ -3120,13 +3141,17 @@ compile_subscript( { if (**arg == '(') { - int argcount = 0; + garray_T *stack = &cctx->ctx_type_stack; + type_T *type; + int argcount = 0; // funcref(arg) + type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; + *arg = skipwhite(*arg + 1); if (compile_arguments(arg, cctx, &argcount) == FAIL) return FAIL; - if (generate_PCALL(cctx, argcount, TRUE) == FAIL) + if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL) return FAIL; } else if (**arg == '-' && (*arg)[1] == '>') |