Skip to content

Commit a915fa0

Browse files
committed
patch 8.2.4612: Vim9: cannot use a recursive call in a nested function
Problem: Vim9: cannot use a recursive call in a nested function. (Sergey Vlasov) Solution: Define the funcref before compiling the function. (closes #9989)
1 parent 81b573d commit a915fa0

File tree

6 files changed

+52
-21
lines changed

6 files changed

+52
-21
lines changed

src/proto/vim9instr.pro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ int generate_OLDSCRIPT(cctx_T *cctx, isntype_T isn_type, char_u *name, int sid,
3838
int generate_VIM9SCRIPT(cctx_T *cctx, isntype_T isn_type, int sid, int idx, type_T *type);
3939
int generate_NEWLIST(cctx_T *cctx, int count);
4040
int generate_NEWDICT(cctx_T *cctx, int count);
41-
int generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc);
41+
int generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc, isn_T **isnp);
4242
int generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name);
4343
int generate_DEF(cctx_T *cctx, char_u *name, size_t len);
4444
int generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where);

src/testdir/test_vim9_func.vim

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,25 @@ def Test_nested_function()
876876
END
877877
v9.CheckScriptSuccess(lines)
878878

879+
# nested function with recursive call
880+
lines =<< trim END
881+
vim9script
882+
883+
def MyFunc(): number
884+
def Fib(n: number): number
885+
if n < 2
886+
return 1
887+
endif
888+
return Fib(n - 2) + Fib(n - 1)
889+
enddef
890+
891+
return Fib(5)
892+
enddef
893+
894+
assert_equal(8, MyFunc())
895+
END
896+
v9.CheckScriptSuccess(lines)
897+
879898
lines =<< trim END
880899
vim9script
881900
def Outer()

src/version.c

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

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
4612,
753755
/**/
754756
4611,
755757
/**/

src/vim9compile.c

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,7 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx, garray_T *lines_to_free)
818818
ufunc_T *ufunc;
819819
int r = FAIL;
820820
compiletype_T compile_type;
821+
isn_T *funcref_isn = NULL;
821822

822823
if (eap->forceit)
823824
{
@@ -913,6 +914,27 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx, garray_T *lines_to_free)
913914
}
914915
}
915916

917+
// Define the funcref before compiling, so that it is found by any
918+
// recursive call.
919+
if (is_global)
920+
{
921+
r = generate_NEWFUNC(cctx, lambda_name, func_name);
922+
func_name = NULL;
923+
lambda_name = NULL;
924+
}
925+
else
926+
{
927+
// Define a local variable for the function reference.
928+
lvar_T *lvar = reserve_local(cctx, func_name, name_end - name_start,
929+
TRUE, ufunc->uf_func_type);
930+
931+
if (lvar == NULL)
932+
goto theend;
933+
if (generate_FUNCREF(cctx, ufunc, &funcref_isn) == FAIL)
934+
goto theend;
935+
r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
936+
}
937+
916938
compile_type = get_compile_type(ufunc);
917939
#ifdef FEAT_PROFILE
918940
// If the outer function is profiled, also compile the nested function for
@@ -934,24 +956,9 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx, garray_T *lines_to_free)
934956
compile_def_function(ufunc, FALSE, CT_NONE, cctx);
935957
#endif
936958

937-
if (is_global)
938-
{
939-
r = generate_NEWFUNC(cctx, lambda_name, func_name);
940-
func_name = NULL;
941-
lambda_name = NULL;
942-
}
943-
else
944-
{
945-
// Define a local variable for the function reference.
946-
lvar_T *lvar = reserve_local(cctx, func_name, name_end - name_start,
947-
TRUE, ufunc->uf_func_type);
948-
949-
if (lvar == NULL)
950-
goto theend;
951-
if (generate_FUNCREF(cctx, ufunc) == FAIL)
952-
goto theend;
953-
r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
954-
}
959+
// If a FUNCREF instruction was generated, set the index after compiling.
960+
if (funcref_isn != NULL && ufunc->uf_def_status == UF_COMPILED)
961+
funcref_isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
955962

956963
theend:
957964
vim_free(lambda_name);

src/vim9expr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ compile_lambda(char_u **arg, cctx_T *cctx)
10401040
// The function reference count will be 1. When the ISN_FUNCREF
10411041
// instruction is deleted the reference count is decremented and the
10421042
// function is freed.
1043-
return generate_FUNCREF(cctx, ufunc);
1043+
return generate_FUNCREF(cctx, ufunc, NULL);
10441044
}
10451045

10461046
func_ptr_unref(ufunc);

src/vim9instr.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1172,16 +1172,19 @@ generate_NEWDICT(cctx_T *cctx, int count)
11721172

11731173
/*
11741174
* Generate an ISN_FUNCREF instruction.
1175+
* "isnp" is set to the instruction, so that fr_dfunc_idx can be set later.
11751176
*/
11761177
int
1177-
generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
1178+
generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc, isn_T **isnp)
11781179
{
11791180
isn_T *isn;
11801181
type_T *type;
11811182

11821183
RETURN_OK_IF_SKIP(cctx);
11831184
if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
11841185
return FAIL;
1186+
if (isnp != NULL)
1187+
*isnp = isn;
11851188
if (ufunc->uf_def_status == UF_NOT_COMPILED)
11861189
isn->isn_arg.funcref.fr_func_name = vim_strsave(ufunc->uf_name);
11871190
else

0 commit comments

Comments
 (0)