Patch 8.2.2983

3 views
Skip to first unread message

Bram Moolenaar

unread,
Jun 12, 2021, 9:59:04 AM6/12/21
to vim...@googlegroups.com

Patch 8.2.2983
Problem: Vim9: an inline function requires specifying the return type.
Solution: Make the return type optional.
Files: src/eval.c, src/vim9compile.c, src/userfunc.c,
src/testdir/test_vim9_func.vim


*** ../vim-8.2.2982/src/eval.c 2021-06-12 12:33:46.415692289 +0200
--- src/eval.c 2021-06-12 15:30:44.651128631 +0200
***************
*** 3530,3538 ****
{
ufunc_T *ufunc = rettv->vval.v_partial->pt_func;

! // compile it here to get the return type
if (compile_def_function(ufunc,
! TRUE, PROFILING(ufunc), NULL) == FAIL)
{
clear_tv(rettv);
ret = FAIL;
--- 3530,3542 ----
{
ufunc_T *ufunc = rettv->vval.v_partial->pt_func;

! // Compile it here to get the return type. The return
! // type is optional, when it's missing use t_unknown.
! // This is recognized in compile_return().
! if (ufunc->uf_ret_type->tt_type == VAR_VOID)
! ufunc->uf_ret_type = &t_unknown;
if (compile_def_function(ufunc,
! FALSE, PROFILING(ufunc), NULL) == FAIL)
{
clear_tv(rettv);
ret = FAIL;
*** ../vim-8.2.2982/src/vim9compile.c 2021-06-11 22:05:43.490401734 +0200
--- src/vim9compile.c 2021-06-12 15:38:53.717803532 +0200
***************
*** 3565,3572 ****
++ufunc->uf_refcount;
clear_tv(&rettv);

! // Compile the function into instructions.
! compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);

// evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
// points into it. Point to the original line to avoid a dangling pointer.
--- 3565,3576 ----
++ufunc->uf_refcount;
clear_tv(&rettv);

! // Compile it here to get the return type. The return type is optional,
! // when it's missing use t_unknown. This is recognized in
! // compile_return().
! if (ufunc->uf_ret_type->tt_type == VAR_VOID)
! ufunc->uf_ret_type = &t_unknown;
! compile_def_function(ufunc, FALSE, PROFILING(ufunc), cctx);

// evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
// points into it. Point to the original line to avoid a dangling pointer.
***************
*** 5398,5407 ****

if (cctx->ctx_skip != SKIP_YES)
{
stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
! if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
|| cctx->ctx_ufunc->uf_ret_type == &t_unknown
|| cctx->ctx_ufunc->uf_ret_type == &t_any))
{
cctx->ctx_ufunc->uf_ret_type = stack_type;
}
--- 5402,5416 ----

if (cctx->ctx_skip != SKIP_YES)
{
+ // "check_return_type" with uf_ret_type set to &t_unknown is used
+ // for an inline function without a specified return type. Set the
+ // return type here.
stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
! if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
|| cctx->ctx_ufunc->uf_ret_type == &t_unknown
|| cctx->ctx_ufunc->uf_ret_type == &t_any))
+ || (!check_return_type
+ && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
{
cctx->ctx_ufunc->uf_ret_type = stack_type;
}
*** ../vim-8.2.2982/src/userfunc.c 2021-06-06 17:02:49.753789485 +0200
--- src/userfunc.c 2021-06-12 15:50:23.463941062 +0200
***************
*** 1377,1383 ****
goto errret;
}
else
! fp->uf_ret_type = &t_any;
}

fp->uf_lines = newlines;
--- 1377,1383 ----
goto errret;
}
else
! fp->uf_ret_type = &t_unknown;
}

fp->uf_lines = newlines;
*** ../vim-8.2.2982/src/testdir/test_vim9_func.vim 2021-06-11 22:05:43.490401734 +0200
--- src/testdir/test_vim9_func.vim 2021-06-12 15:52:33.419590414 +0200
***************
*** 948,953 ****
--- 948,973 ----
echo FilterWithCond('foo', (v) => v .. '^b')
END
CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected func(string): bool but got func(any): string', 1)
+
+ lines =<< trim END
+ var Lambda1 = (x) => {
+ return x
+ }
+ assert_equal('asdf', Lambda1('asdf'))
+ var Lambda2 = (x): string => {
+ return x
+ }
+ assert_equal('foo', Lambda2('foo'))
+ END
+ CheckDefAndScriptSuccess(lines)
+
+ lines =<< trim END
+ var Lambda = (x): string => {
+ return x
+ }
+ echo Lambda(['foo'])
+ END
+ CheckDefExecAndScriptFailure(lines, 'E1012:')
enddef

def Test_lambda_uses_assigned_var()
*** ../vim-8.2.2982/src/version.c 2021-06-12 14:52:35.953230564 +0200
--- src/version.c 2021-06-12 15:23:20.088340817 +0200
***************
*** 752,753 ****
--- 752,755 ----
{ /* Add new patch number below this line */
+ /**/
+ 2983,
/**/

--
BLACK KNIGHT: I'm invincible!
ARTHUR: You're a looney.
"Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
Reply all
Reply to author
Forward
0 new messages