Patch 8.2.4044
Problem:    Vim9: no error when importing the same script twice.
Solution:   Give an error, unless it is a reload.
Files:      src/vim9script.c, src/errors.h, src/testdir/test_vim9_import.vim


*** ../vim-8.2.4043/src/vim9script.c    2022-01-08 12:41:12.212795547 +0000
--- src/vim9script.c    2022-01-08 17:00:14.797099093 +0000
***************
*** 374,379 ****
--- 374,381 ----
      int               sid = -1;
      int               res;
      long      start_lnum = SOURCING_LNUM;
+     garray_T  *import_gap;
+     int               i;
  
      // The name of the file can be an expression, which must evaluate to a
      // string.
***************
*** 440,445 ****
--- 442,465 ----
        goto erret;
      }
  
+     import_gap = gap != NULL ? gap : &SCRIPT_ITEM(import_sid)->sn_imports;
+     for (i = 0; i < import_gap->ga_len; ++i)
+     {
+       imported_T *import = (imported_T *)import_gap->ga_data + i;
+ 
+       if (import->imp_sid == sid)
+       {
+           if (import->imp_flags & IMP_FLAGS_RELOAD)
+           {
+               // encountering same script first ime on a reload is OK
+               import->imp_flags &= ~IMP_FLAGS_RELOAD;
+               break;
+           }
+           semsg(_(e_cannot_import_same_script_twice_str), tv.vval.v_string);
+           goto erret;
+       }
+     }
+ 
      // Allow for the "as Name" to be in the next line.
      nextarg = eval_next_non_blank(expr_end, evalarg, &getnext);
      if (STRNCMP("as", nextarg, 2) == 0 && IS_WHITE_OR_NUL(nextarg[2]))
***************
*** 494,515 ****
        imported_T  *imported;
  
        imported = find_imported(as_name, STRLEN(as_name), cctx);
!       if (imported != NULL && imported->imp_sid == sid)
        {
!           if (imported->imp_flags & IMP_FLAGS_RELOAD)
!               // import already defined on a previous script load
!               imported->imp_flags &= ~IMP_FLAGS_RELOAD;
!           else
!           {
!               semsg(_(e_name_already_defined_str), as_name);
!               goto erret;
!           }
        }
!       else if (check_defined(as_name, STRLEN(as_name), cctx, FALSE) == FAIL)
            goto erret;
  
!       imported = new_imported(gap != NULL ? gap
!                                       : &SCRIPT_ITEM(import_sid)->sn_imports);
        if (imported == NULL)
            goto erret;
        imported->imp_name = as_name;
--- 514,529 ----
        imported_T  *imported;
  
        imported = find_imported(as_name, STRLEN(as_name), cctx);
!       if (imported != NULL && imported->imp_sid != sid)
        {
!           semsg(_(e_name_already_defined_str), as_name);
!           goto erret;
        }
!       else if (imported == NULL
!               && check_defined(as_name, STRLEN(as_name), cctx, FALSE) == FAIL)
            goto erret;
  
!       imported = new_imported(import_gap);
        if (imported == NULL)
            goto erret;
        imported->imp_name = as_name;
*** ../vim-8.2.4043/src/errors.h        2022-01-07 13:38:20.423442096 +0000
--- src/errors.h        2022-01-08 16:31:52.707074855 +0000
***************
*** 2891,2893 ****
--- 2891,2895 ----
        INIT(= N_("E1260: Cannot unlet an imported item: %s"));
  EXTERN char e_cannot_import_dot_vim_without_using_as[]
        INIT(= N_("E1261: Cannot import .vim without using \"as\""));
+ EXTERN char e_cannot_import_same_script_twice_str[]
+       INIT(= N_("E1262: Cannot import the same script twice: %s"));
*** ../vim-8.2.4043/src/testdir/test_vim9_import.vim    2022-01-07 
21:38:43.171435890 +0000
--- src/testdir/test_vim9_import.vim    2022-01-08 16:45:03.620189553 +0000
***************
*** 189,213 ****
    writefile(import_star_as_lines_dot_space, 'Ximport.vim')
    assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func')
  
!   var import_func_duplicated =<< trim END
      vim9script
      import './Xexport.vim' as expo
!     import './Xexport.vim' as expo
! 
!     ExportedInc()
    END
!   writefile(import_func_duplicated, 'Ximport.vim')
    assert_fails('source Ximport.vim', 'E1073:', '', 3, 'Ximport.vim')
! 
!   var import_star_as_duplicated =<< trim END
!     vim9script
!     import './Xexport.vim' as Export
!     var some = 'other'
!     import './Xexport.vim' as Export
!     defcompile
!   END
!   writefile(import_star_as_duplicated, 'Ximport.vim')
!   assert_fails('source Ximport.vim', 'E1073:', '', 4, 'Ximport.vim')
  
    var import_star_as_lines_script_no_dot =<< trim END
      vim9script
--- 189,203 ----
    writefile(import_star_as_lines_dot_space, 'Ximport.vim')
    assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func')
  
!   writefile(s:export_script_lines, 'Xexport2.vim')
!   var import_as_duplicated =<< trim END
      vim9script
      import './Xexport.vim' as expo
!     import './Xexport2.vim' as expo
    END
!   writefile(import_as_duplicated, 'Ximport.vim')
    assert_fails('source Ximport.vim', 'E1073:', '', 3, 'Ximport.vim')
!   delete('Xexport2.vim')
  
    var import_star_as_lines_script_no_dot =<< trim END
      vim9script
***************
*** 429,439 ****
--- 419,438 ----
        export var Ref = TheFunc
    END
    writefile([], 'Xthat.vim')
+ 
    lines =<< trim END
        import './Xthat.vim' as That
        That()
    END
    CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use That itself'])
+ 
+   lines =<< trim END
+       import './Xthat.vim' as one
+       import './Xthat.vim' as two
+   END
+   CheckScriptFailure(lines, 'E1262:')
+ 
+   delete('Xthat.vim')
   
    mkdir('Ximport')
  
***************
*** 773,779 ****
--- 772,785 ----
        g:result = Xsort.FastSort()
      enddef
      Test()
+   END
+   writefile(lines, 'Xscript.vim')
+   source Xscript.vim
+   assert_equal([4, 3, 2, 1, 0], g:result)
+   unlet g:result
  
+   lines =<< trim END
+     vim9script
      # using a function imported with "as"
      import './Xsort.vim' as anAlias
      assert_equal('yes', anAlias.GetString('yes'))
***************
*** 790,799 ****
    END
    writefile(lines, 'Xscript.vim')
  
-   source Xscript.vim
-   assert_equal([4, 3, 2, 1, 0], g:result)
- 
-   unlet g:result
    delete('Xsort.vim')
    delete('Xscript.vim')
  
--- 796,801 ----
*** ../vim-8.2.4043/src/version.c       2022-01-08 16:19:18.513639814 +0000
--- src/version.c       2022-01-08 16:25:24.326455414 +0000
***************
*** 752,753 ****
--- 752,755 ----
  {   /* Add new patch number below this line */
+ /**/
+     4044,
  /**/

-- 
hundred-and-one symptoms of being an internet addict:
266. You hear most of your jokes via e-mail instead of in person.

 /// 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/20220108170420.ADEFF1C0D8A%40moolenaar.net.

Raspunde prin e-mail lui