changeset 22910:f78057703df9 v8.2.2002

patch 8.2.2002: Vim9: lambda argument shadowed by function name Commit: https://github.com/vim/vim/commit/52bf81c2d5f0d57443a29525b68b88707f5ad87c Author: Bram Moolenaar <Bram@vim.org> Date: Tue Nov 17 18:50:44 2020 +0100 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 https://github.com/vim/vim/issues/7313)
author Bram Moolenaar <Bram@vim.org>
date Tue, 17 Nov 2020 19:00:04 +0100
parents 3bd8aeb73f94
children 3b89427262e6
files src/testdir/test_vim9_func.vim src/version.c src/vim9compile.c
diffstat 3 files changed, 24 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/testdir/test_vim9_func.vim
+++ b/src/testdir/test_vim9_func.vim
@@ -1456,6 +1456,15 @@ def Test_nested_lambda()
   CheckScriptSuccess(lines)
 enddef
 
+def Shadowed(): list<number>
+  var FuncList: list<func: number> = [{ -> 42}]
+  return FuncList->map({_, Shadowed -> Shadowed()})
+enddef
+
+def Test_lambda_arg_shadows_func()
+  assert_equal([42], Shadowed())
+enddef
+
 def Line_continuation_in_def(dir: string = ''): string
   var path: string = empty(dir)
           \ ? 'empty'
--- a/src/version.c
+++ b/src/version.c
@@ -751,6 +751,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    2002,
+/**/
     2001,
 /**/
     2000,
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -2712,13 +2712,19 @@ compile_call(
 	goto theend;
     }
 
-    // If we can find the function by name generate the right call.
-    // Skip global functions here, a local funcref takes precedence.
-    ufunc = find_func(name, FALSE, cctx);
-    if (ufunc != NULL && !func_is_global(ufunc))
-    {
-	res = generate_CALL(cctx, ufunc, argcount);
-	goto theend;
+    // An argument or local variable can be a function reference, this
+    // overrules a function name.
+    if (lookup_local(namebuf, varlen, cctx) == NULL
+	    && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
+    {
+	// If we can find the function by name generate the right call.
+	// Skip global functions here, a local funcref takes precedence.
+	ufunc = find_func(name, FALSE, cctx);
+	if (ufunc != NULL && !func_is_global(ufunc))
+	{
+	    res = generate_CALL(cctx, ufunc, argcount);
+	    goto theend;
+	}
     }
 
     // If the name is a variable, load it and use PCALL.