changeset 23996:c192411b27e8 v8.2.2540

patch 8.2.2540: Vim9: no error for using script var name for argument Commit: https://github.com/vim/vim/commit/b4893b84507570f24af32597716ffb67fb0ef24e Author: Bram Moolenaar <Bram@vim.org> Date: Sun Feb 21 22:20:24 2021 +0100 patch 8.2.2540: Vim9: no error for using script var name for argument Problem: Vim9: no error for using script var name for argument. Solution: Check for this error. (closes https://github.com/vim/vim/issues/7868)
author Bram Moolenaar <Bram@vim.org>
date Sun, 21 Feb 2021 22:30:05 +0100
parents ac050d89a914
children 74ed52d0f507
files src/proto/vim9compile.pro src/testdir/test_vim9_func.vim src/userfunc.c src/version.c src/vim9compile.c
diffstat 5 files changed, 21 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/proto/vim9compile.pro
+++ b/src/proto/vim9compile.pro
@@ -1,4 +1,5 @@
 /* vim9compile.c */
+int script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx);
 int check_defined(char_u *p, size_t len, cctx_T *cctx);
 int check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2);
 int use_typecheck(type_T *actual, type_T *expected);
--- a/src/testdir/test_vim9_func.vim
+++ b/src/testdir/test_vim9_func.vim
@@ -591,6 +591,15 @@ def Test_call_wrong_args()
 
   lines =<< trim END
     vim9script
+    var name = 'piet'
+    def FuncOne(name: string)
+      echo nr
+    enddef
+  END
+  CheckScriptFailure(lines, 'E1054:')
+
+  lines =<< trim END
+    vim9script
     def FuncOne(nr: number)
       echo nr
     enddef
--- a/src/userfunc.c
+++ b/src/userfunc.c
@@ -80,6 +80,14 @@ one_function_arg(
 	    semsg(_("E125: Illegal argument: %s"), arg);
 	return arg;
     }
+
+    // Vim9 script: cannot use script var name for argument.
+    if (argtypes != NULL && script_var_exists(arg, p - arg, FALSE, NULL) == OK)
+    {
+	semsg(_(e_variable_already_declared_in_script), arg);
+	return arg;
+    }
+
     if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
 	return arg;
     if (newargs != NULL)
--- 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 */
 /**/
+    2540,
+/**/
     2539,
 /**/
     2538,
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -337,7 +337,7 @@ script_is_vim9()
  * "cctx" is NULL at the script level.
  * Returns OK or FAIL.
  */
-    static int
+    int
 script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx)
 {
     int		    is_vim9_script;