Skip to content

Commit 52bf81c

Browse files
committed
patch 8.2.2002: Vim9: lambda argument shadowed by function name
Problem: Vim9: lambda argument shadowed by function name. Solution: Let function name be shadowed by lambda argument. (closes #7313)
1 parent 0ba48e8 commit 52bf81c

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

src/testdir/test_vim9_func.vim

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,15 @@ def Test_nested_lambda()
14561456
CheckScriptSuccess(lines)
14571457
enddef
14581458

1459+
def Shadowed(): list<number>
1460+
var FuncList: list<func: number> = [{ -> 42}]
1461+
return FuncList->map({_, Shadowed -> Shadowed()})
1462+
enddef
1463+
1464+
def Test_lambda_arg_shadows_func()
1465+
assert_equal([42], Shadowed())
1466+
enddef
1467+
14591468
def Line_continuation_in_def(dir: string = ''): string
14601469
var path: string = empty(dir)
14611470
\ ? 'empty'

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+
2002,
753755
/**/
754756
2001,
755757
/**/

src/vim9compile.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2712,13 +2712,19 @@ compile_call(
27122712
goto theend;
27132713
}
27142714

2715-
// If we can find the function by name generate the right call.
2716-
// Skip global functions here, a local funcref takes precedence.
2717-
ufunc = find_func(name, FALSE, cctx);
2718-
if (ufunc != NULL && !func_is_global(ufunc))
2719-
{
2720-
res = generate_CALL(cctx, ufunc, argcount);
2721-
goto theend;
2715+
// An argument or local variable can be a function reference, this
2716+
// overrules a function name.
2717+
if (lookup_local(namebuf, varlen, cctx) == NULL
2718+
&& arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
2719+
{
2720+
// If we can find the function by name generate the right call.
2721+
// Skip global functions here, a local funcref takes precedence.
2722+
ufunc = find_func(name, FALSE, cctx);
2723+
if (ufunc != NULL && !func_is_global(ufunc))
2724+
{
2725+
res = generate_CALL(cctx, ufunc, argcount);
2726+
goto theend;
2727+
}
27222728
}
27232729

27242730
// If the name is a variable, load it and use PCALL.

0 commit comments

Comments
 (0)