Patch 8.2.4162
Problem:    Vim9: no error for redefining function with export.
Solution:   Check for existing function with/without prefix. (closes #9577)
Files:      src/userfunc.c, src/scriptfile.c, src/testdir/test_vim9_import.vim


*** ../vim-8.2.4161/src/userfunc.c      2022-01-19 17:21:24.846755312 +0000
--- src/userfunc.c      2022-01-20 19:07:35.106223364 +0000
***************
*** 1911,1916 ****
--- 1911,1922 ----
      {
        size_t  len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
        char_u  *auto_name;
+       char_u  *namep;
+ 
+       // skip a "<SNR>99_" prefix
+       namep = untrans_function_name(name);
+       if (namep == NULL)
+           namep = name;
  
        // An exported function in an autoload script is stored as
        // "dir#path#name".
***************
*** 1921,1927 ****
        if (auto_name != NULL)
        {
            vim_snprintf((char *)auto_name, len, "%s%s",
!                                                si->sn_autoload_prefix, name);
            hi = hash_find(&func_hashtab, auto_name);
            if (auto_name != buffer)
                vim_free(auto_name);
--- 1927,1933 ----
        if (auto_name != NULL)
        {
            vim_snprintf((char *)auto_name, len, "%s%s",
!                                               si->sn_autoload_prefix, namep);
            hi = hash_find(&func_hashtab, auto_name);
            if (auto_name != buffer)
                vim_free(auto_name);
***************
*** 4175,4181 ****
        // is stored with the legacy autoload name "dir#script#FuncName" so
        // that it can also be found in legacy script.
        if (is_export && name != NULL)
!           name = may_prefix_autoload(name);
      }
  
      // An error in a function call during evaluation of an expression in magic
--- 4181,4195 ----
        // is stored with the legacy autoload name "dir#script#FuncName" so
        // that it can also be found in legacy script.
        if (is_export && name != NULL)
!       {
!           char_u *prefixed = may_prefix_autoload(name);
! 
!           if (prefixed != NULL && prefixed != name)
!           {
!               vim_free(name);
!               name = prefixed;
!           }
!       }
      }
  
      // An error in a function call during evaluation of an expression in magic
***************
*** 4447,4466 ****
      if (fudi.fd_dict == NULL)
      {
        hashtab_T       *ht;
  
        v = find_var(name, &ht, TRUE);
!       if (v != NULL && v->di_tv.v_type == VAR_FUNC)
        {
            emsg_funcname(e_function_name_conflicts_with_variable_str, name);
            goto erret;
        }
  
!       fp = find_func_even_dead(name, is_global);
        if (vim9script)
        {
            char_u *uname = untrans_function_name(name);
  
!           import = find_imported(uname == NULL ? name : uname, 0, FALSE, 
NULL);
        }
  
        if (fp != NULL || import != NULL)
--- 4461,4513 ----
      if (fudi.fd_dict == NULL)
      {
        hashtab_T       *ht;
+       char_u          *find_name = name;
+       int             var_conflict = FALSE;
  
        v = find_var(name, &ht, TRUE);
!       if (v != NULL)
!           var_conflict = TRUE;
! 
!       if (SCRIPT_ID_VALID(current_sctx.sc_sid))
!       {
!           scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
! 
!           if (si->sn_autoload_prefix != NULL)
!           {
!               if (is_export)
!               {
!                   find_name = name + STRLEN(si->sn_autoload_prefix);
!                   v = find_var(find_name, &ht, TRUE);
!                   if (v != NULL)
!                       var_conflict = TRUE;
!               }
!               else
!               {
!                   char_u *prefixed = may_prefix_autoload(name);
! 
!                   if (prefixed != NULL)
!                   {
!                       v = find_var(prefixed, &ht, TRUE);
!                       if (v != NULL)
!                           var_conflict = TRUE;
!                       vim_free(prefixed);
!                   }
!               }
!           }
!       }
!       if (var_conflict)
        {
            emsg_funcname(e_function_name_conflicts_with_variable_str, name);
            goto erret;
        }
  
!       fp = find_func_even_dead(find_name, is_global);
        if (vim9script)
        {
            char_u *uname = untrans_function_name(name);
  
!           import = find_imported(uname == NULL ? name : uname, 0,
!                                                                 FALSE, NULL);
        }
  
        if (fp != NULL || import != NULL)
*** ../vim-8.2.4161/src/scriptfile.c    2022-01-18 14:16:55.144654083 +0000
--- src/scriptfile.c    2022-01-20 19:04:41.929558501 +0000
***************
*** 2190,2196 ****
  
  /*
   * If in a Vim9 autoload script return "name" with the autoload prefix for the
!  * script.  If successful "name" is freed, the returned name is allocated.
   * Otherwise it returns "name" unmodified.
   */
      char_u *
--- 2190,2196 ----
  
  /*
   * If in a Vim9 autoload script return "name" with the autoload prefix for the
!  * script.  If successful the returned name is allocated.
   * Otherwise it returns "name" unmodified.
   */
      char_u *
***************
*** 2221,2227 ****
            {
                vim_snprintf((char *)res, len, "%s%s",
                                             si->sn_autoload_prefix, basename);
-               vim_free(name);
                return res;
            }
        }
--- 2221,2226 ----
*** ../vim-8.2.4161/src/testdir/test_vim9_import.vim    2022-01-20 
17:35:45.577672159 +0000
--- src/testdir/test_vim9_import.vim    2022-01-20 19:08:30.049032928 +0000
***************
*** 1440,1445 ****
--- 1440,1519 ----
    &rtp = save_rtp
  enddef
  
+ def Test_vim9script_autoload_duplicate()
+   mkdir('Xdir/autoload', 'p')
+ 
+   var lines =<< trim END
+      vim9script
+ 
+      export def Func()
+      enddef
+ 
+      def Func()
+      enddef
+   END
+   writefile(lines, 'Xdir/autoload/dupfunc.vim')
+   assert_fails('source Xdir/autoload/dupfunc.vim', 'E1073:')
+ 
+   lines =<< trim END
+      vim9script
+ 
+      def Func()
+      enddef
+ 
+      export def Func()
+      enddef
+   END
+   writefile(lines, 'Xdir/autoload/dup2func.vim')
+   assert_fails('source Xdir/autoload/dup2func.vim', 'E1073:')
+ 
+   lines =<< trim END
+      vim9script
+ 
+      def Func()
+      enddef
+ 
+      export var Func = 'asdf'
+   END
+   writefile(lines, 'Xdir/autoload/dup3func.vim')
+   assert_fails('source Xdir/autoload/dup3func.vim', 'E1041: Redefining script 
item Func')
+ 
+   lines =<< trim END
+      vim9script
+ 
+      export var Func = 'asdf'
+ 
+      def Func()
+      enddef
+   END
+   writefile(lines, 'Xdir/autoload/dup4func.vim')
+   assert_fails('source Xdir/autoload/dup4func.vim', 'E707:')
+ 
+   lines =<< trim END
+      vim9script
+ 
+      var Func = 'asdf'
+ 
+      export def Func()
+      enddef
+   END
+   writefile(lines, 'Xdir/autoload/dup5func.vim')
+   assert_fails('source Xdir/autoload/dup5func.vim', 'E707:')
+ 
+   lines =<< trim END
+      vim9script
+ 
+      export def Func()
+      enddef
+ 
+      var Func = 'asdf'
+   END
+   writefile(lines, 'Xdir/autoload/dup6func.vim')
+   assert_fails('source Xdir/autoload/dup6func.vim', 'E1041: Redefining script 
item Func')
+ 
+   delete('Xdir', 'rf')
+ enddef
+ 
  def Test_import_autoload_postponed()
    mkdir('Xdir/autoload', 'p')
    var save_rtp = &rtp
*** ../vim-8.2.4161/src/version.c       2022-01-20 17:35:45.577672159 +0000
--- src/version.c       2022-01-20 18:53:50.229956056 +0000
***************
*** 752,753 ****
--- 752,755 ----
  {   /* Add new patch number below this line */
+ /**/
+     4162,
  /**/

-- 
Vi beats Emacs to death, and then again!
                        http://linuxtoday.com/stories/5764.html

 /// 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/20220120191051.7E35C1C188D%40moolenaar.net.

Raspunde prin e-mail lui