Skip to content

Commit 5a849da

Browse files
committed
patch 8.2.1396: Vim9: no error for unexpectedly returning a value
Problem: Vim9: no error for unexpectedly returning a value. Solution: Only set the return type for lambda's. Make using function type in a function reference work.
1 parent 98b4f14 commit 5a849da

3 files changed

Lines changed: 57 additions & 15 deletions

File tree

src/testdir/test_vim9_func.vim

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -503,16 +503,48 @@ def Test_error_in_nested_function()
503503
enddef
504504

505505
def Test_return_type_wrong()
506-
CheckScriptFailure(['def Func(): number', 'return "a"', 'enddef', 'defcompile'], 'expected number but got string')
507-
CheckScriptFailure(['def Func(): string', 'return 1', 'enddef', 'defcompile'], 'expected string but got number')
508-
CheckScriptFailure(['def Func(): void', 'return "a"', 'enddef', 'defcompile'], 'E1096: Returning a value in a function without a return type')
509-
CheckScriptFailure(['def Func()', 'return "a"', 'enddef', 'defcompile'], 'E1096: Returning a value in a function without a return type')
510-
511-
CheckScriptFailure(['def Func(): number', 'return', 'enddef', 'defcompile'], 'E1003:')
506+
CheckScriptFailure([
507+
'def Func(): number',
508+
'return "a"',
509+
'enddef',
510+
'defcompile'], 'expected number but got string')
511+
CheckScriptFailure([
512+
'def Func(): string',
513+
'return 1',
514+
'enddef',
515+
'defcompile'], 'expected string but got number')
516+
CheckScriptFailure([
517+
'def Func(): void',
518+
'return "a"',
519+
'enddef',
520+
'defcompile'],
521+
'E1096: Returning a value in a function without a return type')
522+
CheckScriptFailure([
523+
'def Func()',
524+
'return "a"',
525+
'enddef',
526+
'defcompile'],
527+
'E1096: Returning a value in a function without a return type')
528+
529+
CheckScriptFailure([
530+
'def Func(): number',
531+
'return',
532+
'enddef',
533+
'defcompile'], 'E1003:')
512534

513535
CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:')
514536
CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:')
515537
CheckScriptFailure(['def Func()', 'return 1'], 'E1057:')
538+
539+
CheckScriptFailure([
540+
'vim9script',
541+
'def FuncB()',
542+
' return 123',
543+
'enddef',
544+
'def FuncA()',
545+
' FuncB()',
546+
'enddef',
547+
'defcompile'], 'E1096:')
516548
enddef
517549

518550
def Test_arg_type_wrong()

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,8 @@ static char *(features[]) =
754754

755755
static int included_patches[] =
756756
{ /* Add new patch number below this line */
757+
/**/
758+
1396,
757759
/**/
758760
1395,
759761
/**/

src/vim9compile.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,10 @@ check_type(type_T *expected, type_T *actual, int give_msg)
662662
int i;
663663

664664
for (i = 0; i < expected->tt_argcount; ++i)
665-
if (check_type(expected->tt_args[i], actual->tt_args[i],
666-
FALSE) == FAIL)
665+
// Allow for using "any" argument type, lambda's have them.
666+
if (actual->tt_args[i] != &t_any && check_type(
667+
expected->tt_args[i], actual->tt_args[i], FALSE)
668+
== FAIL)
667669
{
668670
ret = FAIL;
669671
break;
@@ -1537,21 +1539,21 @@ generate_NEWDICT(cctx_T *cctx, int count)
15371539
* Generate an ISN_FUNCREF instruction.
15381540
*/
15391541
static int
1540-
generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1542+
generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
15411543
{
15421544
isn_T *isn;
15431545
garray_T *stack = &cctx->ctx_type_stack;
15441546

15451547
RETURN_OK_IF_SKIP(cctx);
15461548
if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
15471549
return FAIL;
1548-
isn->isn_arg.funcref.fr_func = dfunc_idx;
1550+
isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
15491551
isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
15501552

15511553
if (ga_grow(stack, 1) == FAIL)
15521554
return FAIL;
1553-
((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
1554-
// TODO: argument and return types
1555+
((type_T **)stack->ga_data)[stack->ga_len] =
1556+
ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
15551557
++stack->ga_len;
15561558

15571559
return OK;
@@ -1713,7 +1715,8 @@ generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
17131715
}
17141716
}
17151717
if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
1716-
if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
1718+
if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1719+
== FAIL)
17171720
return FAIL;
17181721
}
17191722

@@ -3338,7 +3341,12 @@ compile_lambda(char_u **arg, cctx_T *cctx)
33383341
clear_evalarg(&evalarg, NULL);
33393342

33403343
if (ufunc->uf_def_status == UF_COMPILED)
3341-
return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
3344+
{
3345+
// The return type will now be known.
3346+
set_function_type(ufunc);
3347+
3348+
return generate_FUNCREF(cctx, ufunc);
3349+
}
33423350

33433351
func_ptr_unref(ufunc);
33443352
return FAIL;
@@ -4982,7 +4990,7 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx)
49824990
TRUE, ufunc->uf_func_type);
49834991
if (lvar == NULL)
49844992
return NULL;
4985-
if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL)
4993+
if (generate_FUNCREF(cctx, ufunc) == FAIL)
49864994
return NULL;
49874995
r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
49884996
}

0 commit comments

Comments
 (0)