]> granicus.if.org Git - vim/commitdiff
patch 8.2.3954: Vim9: no error for shadowing if script var is declared later v8.2.3954
authorBram Moolenaar <Bram@vim.org>
Fri, 31 Dec 2021 14:06:45 +0000 (14:06 +0000)
committerBram Moolenaar <Bram@vim.org>
Fri, 31 Dec 2021 14:06:45 +0000 (14:06 +0000)
Problem:    Vim9: no error for shadowing if script var is declared later.
Solution:   Check argument names when compiling a function.

src/testdir/test_vim9_assign.vim
src/testdir/test_vim9_func.vim
src/version.c
src/vim9compile.c

index f02da7fe4a610ce9e5c26c759f05a26fdced7a19..683180995ba9df42917f39f6ec6a26f83074a75b 100644 (file)
@@ -853,8 +853,8 @@ def Test_assignment_partial()
 
       var nres: any
       var sres: any
-      def Func(n: number, s = '')
-        nres = n
+      def Func(nr: number, s = '')
+        nres = nr
         sres = s
       enddef
 
@@ -869,7 +869,7 @@ def Test_assignment_partial()
   lines =<< trim END
       vim9script
 
-      def Func(n: number, s = '')
+      def Func(nr: number, s = '')
       enddef
 
       var n: number
index 7b2f5016c3fac979eae3b9e81c1312c9fa515155..1cdf7d480820b2950f3a2c0b0c9db37edf8f8cee 100644 (file)
@@ -951,6 +951,7 @@ def Test_call_wrong_args()
   END
   CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got list<unknown>', 5)
 
+  # argument name declared earlier is found when declaring a function
   lines =<< trim END
     vim9script
     var name = 'piet'
@@ -960,6 +961,17 @@ def Test_call_wrong_args()
   END
   CheckScriptFailure(lines, 'E1168:')
 
+  # argument name declared later is only found when compiling
+  lines =<< trim END
+    vim9script
+    def FuncOne(name: string)
+      echo nr
+    enddef
+    var name = 'piet'
+  END
+  CheckScriptSuccess(lines)
+  CheckScriptFailure(lines + ['defcompile'], 'E1168:')
+
   lines =<< trim END
     vim9script
     def FuncOne(nr: number)
index 1e955c41d7f891c136685527b0b7966e17855018..9c1725240a21f4caf354b9eae30bd1d9b0f44190 100644 (file)
@@ -749,6 +749,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    3954,
 /**/
     3953,
 /**/
index 272368e76503a9f2a17955a2ee451968cd5840fd..041837830550846865aa228e07e92acf72efc58c 100644 (file)
@@ -2393,6 +2393,34 @@ may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
     return NOTDONE;
 }
 
+/*
+ * Check if arguments of "ufunc" shadow variables in "cctx".
+ * Return OK or FAIL.
+ */
+    static int
+check_args_shadowing(ufunc_T *ufunc, cctx_T *cctx)
+{
+    int            i;
+    char_u  *arg;
+    int            r = OK;
+
+    // Make sure arguments are not found when compiling a second time.
+    ufunc->uf_args_visible = 0;
+
+    // Check for arguments shadowing variables from the context.
+    for (i = 0; i < ufunc->uf_args.ga_len; ++i)
+    {
+       arg = ((char_u **)(ufunc->uf_args.ga_data))[i];
+       if (check_defined(arg, STRLEN(arg), cctx, TRUE) == FAIL)
+       {
+           r = FAIL;
+           break;
+       }
+    }
+    ufunc->uf_args_visible = ufunc->uf_args.ga_len;
+    return r;
+}
+
 
 /*
  * Add a function to the list of :def functions.
@@ -2525,6 +2553,9 @@ compile_def_function(
        estack_push_ufunc(ufunc, 1);
     estack_compiling = TRUE;
 
+    if (check_args_shadowing(ufunc, &cctx) == FAIL)
+       goto erret;
+
     if (ufunc->uf_def_args.ga_len > 0)
     {
        int     count = ufunc->uf_def_args.ga_len;