Patch 8.2.3782
Problem:    Vim9: no error if a function shadows a script variable.
Solution:   Check the function doesn't shadow a variable. (closes #9310)
Files:      src/userfunc.c, src/evalvars.c, src/vim.h,
            src/testdir/test_vim9_script.vim


*** ../vim-8.2.3781/src/userfunc.c      2021-12-07 12:23:53.991565068 +0000
--- src/userfunc.c      2021-12-11 13:42:38.612610959 +0000
***************
*** 4134,4152 ****
                                     || (fudi.fd_di->di_tv.v_type != VAR_FUNC
                                 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
        {
            if (*arg == K_SPECIAL)
!               j = 3;
!           else
!               j = 0;
!           while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
!                                                     : eval_isnamec(arg[j])))
!               ++j;
!           if (arg[j] != NUL)
                emsg_funcname((char *)e_invarg2, arg);
        }
        // Disallow using the g: dict.
        if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
            emsg(_("E862: Cannot use g: here"));
      }
  
      // This may get more lines and make the pointers into the first line
--- 4134,4174 ----
                                     || (fudi.fd_di->di_tv.v_type != VAR_FUNC
                                 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
        {
+           char_u  *name_base = arg;
+           int     i;
+ 
            if (*arg == K_SPECIAL)
!           {
!               name_base = vim_strchr(arg, '_');
!               if (name_base == NULL)
!                   name_base = arg + 3;
!               else
!                   ++name_base;
!           }
!           for (i = 0; name_base[i] != NUL && (i == 0
!                                       ? eval_isnamec1(name_base[i])
!                                       : eval_isnamec(name_base[i])); ++i)
!               ;
!           if (name_base[i] != NUL)
                emsg_funcname((char *)e_invarg2, arg);
+ 
+           // In Vim9 script a function cannot have the same name as a
+           // variable.
+           if (vim9script && *arg == K_SPECIAL
+                    && eval_variable(name_base, STRLEN(name_base), NULL, NULL,
+                        EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
+                                                    + EVAL_VAR_NO_FUNC) == OK)
+           {
+               semsg(_(e_redefining_script_item_str), name_base);
+               goto ret_free;
+           }
        }
        // Disallow using the g: dict.
        if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
+       {
            emsg(_("E862: Cannot use g: here"));
+           goto ret_free;
+       }
      }
  
      // This may get more lines and make the pointers into the first line
*** ../vim-8.2.3781/src/evalvars.c      2021-12-10 10:37:30.855830802 +0000
--- src/evalvars.c      2021-12-11 13:38:56.793056153 +0000
***************
*** 2712,2718 ****
                type = sv->sv_type;
            }
        }
!       else if (in_vim9script())
        {
            ufunc_T *ufunc = find_func(name, FALSE, NULL);
  
--- 2712,2718 ----
                type = sv->sv_type;
            }
        }
!       else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
        {
            ufunc_T *ufunc = find_func(name, FALSE, NULL);
  
*** ../vim-8.2.3781/src/vim.h   2021-11-29 17:37:38.063265208 +0000
--- src/vim.h   2021-12-11 13:39:33.720980555 +0000
***************
*** 2782,2787 ****
--- 2782,2788 ----
  #define EVAL_VAR_VERBOSE      1   // may give error message
  #define EVAL_VAR_NOAUTOLOAD   2   // do not use script autoloading
  #define EVAL_VAR_IMPORT               4   // may return special variable for 
import
+ #define EVAL_VAR_NO_FUNC      8   // do not look for a function
  
  // Maximum number of characters that can be fuzzy matched
  #define MAX_FUZZY_MATCHES     256
*** ../vim-8.2.3781/src/testdir/test_vim9_script.vim    2021-12-10 
21:05:50.205426652 +0000
--- src/testdir/test_vim9_script.vim    2021-12-11 13:45:27.024284502 +0000
***************
*** 1896,1901 ****
--- 1896,1912 ----
    CheckScriptFailure(lines, 'E1041:', 5)
  enddef
  
+ def Test_function_shadows_script_var()
+   var lines =<< trim END
+       vim9script
+       var Func = 1
+       def Func(): number
+         return 123
+       enddef
+   END
+   CheckScriptFailure(lines, 'E1041:', 3)
+ enddef
+ 
  def Test_script_var_shadows_command()
    var lines =<< trim END
        var undo = 1
***************
*** 2198,2204 ****
        echo 'local to function'
      enddef
    END
!   CheckScriptFailure(lines, 'E1073:')
  
    lines =<< trim END
      vim9script
--- 2209,2215 ----
        echo 'local to function'
      enddef
    END
!   CheckScriptFailure(lines, 'E1041:')
  
    lines =<< trim END
      vim9script
***************
*** 2231,2237 ****
      vim9script
      def Foo(): string
        return 'foo'
!       enddef
      def Func()
        var  Foo = {-> 'lambda'}
      enddef
--- 2242,2248 ----
      vim9script
      def Foo(): string
        return 'foo'
!     enddef
      def Func()
        var  Foo = {-> 'lambda'}
      enddef
*** ../vim-8.2.3781/src/version.c       2021-12-11 12:33:22.127457639 +0000
--- src/version.c       2021-12-11 13:26:08.370466489 +0000
***************
*** 755,756 ****
--- 755,758 ----
  {   /* Add new patch number below this line */
+ /**/
+     3782,
  /**/

-- 
How To Keep A Healthy Level Of Insanity:
6. In the memo field of all your checks, write "for sexual favors".

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/20211211135515.794C21C0AE8%40moolenaar.net.

Raspunde prin e-mail lui