To: vim_dev@googlegroups.com Subject: Patch 8.2.0657 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.0657 Problem: Vim9: no check if called variable is a FuncRef. Solution: Add a type check. Files: src/vim9compile.c, src/testdir/test_vim9_script.vim, src/testdir/test_vim9_expr.vim *** ../vim-8.2.0656/src/vim9compile.c 2020-04-27 22:47:45.186176148 +0200 --- src/vim9compile.c 2020-04-28 21:25:07.282335257 +0200 *************** *** 1327,1341 **** /* * Generate an ISN_PCALL instruction. */ static int ! generate_PCALL(cctx_T *cctx, int argcount, int at_top) { isn_T *isn; garray_T *stack = &cctx->ctx_type_stack; RETURN_OK_IF_SKIP(cctx); if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) return FAIL; isn->isn_arg.pfunc.cpf_top = at_top; --- 1327,1358 ---- /* * Generate an ISN_PCALL instruction. + * "type" is the type of the FuncRef. */ static int ! 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,1350 **** 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; // If partial is above the arguments it must be cleared and replaced with // the return value. --- 1361,1367 ---- 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] = ret_type; // If partial is above the arguments it must be cleared and replaced with // the return value. *************** *** 2465,2476 **** if (STRNCMP(namebuf, "g:", 2) != 0 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK) { ! res = generate_PCALL(cctx, argcount, FALSE); goto theend; } // A global function may be defined only later. Need to figure out at ! // runtime. if (STRNCMP(namebuf, "g:", 2) == 0) res = generate_UCALL(cctx, name, argcount); else --- 2482,2497 ---- if (STRNCMP(namebuf, "g:", 2) != 0 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK) { ! 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. Also handles a FuncRef at runtime. if (STRNCMP(namebuf, "g:", 2) == 0) res = generate_UCALL(cctx, name, argcount); else *************** *** 3120,3132 **** { if (**arg == '(') { ! int argcount = 0; // funcref(arg) *arg = skipwhite(*arg + 1); if (compile_arguments(arg, cctx, &argcount) == FAIL) return FAIL; ! if (generate_PCALL(cctx, argcount, TRUE) == FAIL) return FAIL; } else if (**arg == '-' && (*arg)[1] == '>') --- 3141,3157 ---- { if (**arg == '(') { ! 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, end_leader, type, TRUE) == FAIL) return FAIL; } else if (**arg == '-' && (*arg)[1] == '>') *** ../vim-8.2.0656/src/testdir/test_vim9_script.vim 2020-04-27 23:39:26.416849722 +0200 --- src/testdir/test_vim9_script.vim 2020-04-28 21:18:29.515316414 +0200 *************** *** 345,350 **** --- 345,353 ---- 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() *** ../vim-8.2.0656/src/testdir/test_vim9_expr.vim 2020-04-27 22:47:45.186176148 +0200 --- src/testdir/test_vim9_expr.vim 2020-04-28 21:22:18.694750557 +0200 *************** *** 213,218 **** --- 213,224 ---- 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 *** ../vim-8.2.0656/src/version.c 2020-04-28 20:44:38.872258441 +0200 --- src/version.c 2020-04-28 21:29:08.225742426 +0200 *************** *** 748,749 **** --- 748,751 ---- { /* Add new patch number below this line */ + /**/ + 657, /**/ -- How To Keep A Healthy Level Of Insanity: 9. As often as possible, skip rather than walk. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///