Patch 8.2.0368
Problem:    Vim9: import that redefines local variable does not fail.
Solution:   Check for already defined symbols.
Files:      src/vim9script.c, src/proto/vim9script.pro, src/vim9compile.c,
            src/proto/vim9compile.pro, src/testdir/test_vim9_script.vim


*** ../vim-8.2.0367/src/vim9script.c    2020-02-23 22:35:01.159497752 +0100
--- src/vim9script.c    2020-03-09 19:14:02.051962546 +0100
***************
*** 143,149 ****
        emsg(_(e_needs_vim9));
      else
      {
!       char_u *cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid);
  
        if (cmd_end != NULL)
            eap->nextcmd = check_nextcmd(cmd_end);
--- 143,150 ----
        emsg(_(e_needs_vim9));
      else
      {
!       char_u *cmd_end = handle_import(eap->arg, NULL,
!                                                   current_sctx.sc_sid, NULL);
  
        if (cmd_end != NULL)
            eap->nextcmd = check_nextcmd(cmd_end);
***************
*** 238,244 ****
   * Returns a pointer to after the command or NULL in case of failure
   */
      char_u *
! handle_import(char_u *arg_start, garray_T *gap, int import_sid)
  {
      char_u    *arg = arg_start;
      char_u    *cmd_end;
--- 239,245 ----
   * Returns a pointer to after the command or NULL in case of failure
   */
      char_u *
! handle_import(char_u *arg_start, garray_T *gap, int import_sid, void *cctx)
  {
      char_u    *arg = arg_start;
      char_u    *cmd_end;
***************
*** 278,283 ****
--- 279,286 ----
                    ++arg;
            as_len = (int)(arg - as_ptr);
            arg = skipwhite(arg);
+           if (check_defined(as_ptr, as_len, cctx) == FAIL)
+               return NULL;
        }
        else if (*arg_start == '*')
        {
***************
*** 389,394 ****
--- 392,400 ----
            if (idx < 0 && ufunc == NULL)
                return NULL;
  
+           if (check_defined(name, name_len, cctx) == FAIL)
+               return NULL;
+ 
            imported = new_imported(gap != NULL ? gap
                                       : &SCRIPT_ITEM(import_sid)->sn_imports);
            if (imported == NULL)
*** ../vim-8.2.0367/src/proto/vim9script.pro    2020-02-23 21:25:50.464675047 
+0100
--- src/proto/vim9script.pro    2020-03-09 19:07:16.734331083 +0100
***************
*** 5,9 ****
  void free_imports(int sid);
  void ex_import(exarg_T *eap);
  int find_exported(int sid, char_u **argp, int *name_len, ufunc_T **ufunc, 
type_T **type);
! char_u *handle_import(char_u *arg_start, garray_T *gap, int import_sid);
  /* vim: set ft=c : */
--- 5,9 ----
  void free_imports(int sid);
  void ex_import(exarg_T *eap);
  int find_exported(int sid, char_u **argp, int *name_len, ufunc_T **ufunc, 
type_T **type);
! char_u *handle_import(char_u *arg_start, garray_T *gap, int import_sid, void 
*cctx);
  /* vim: set ft=c : */
*** ../vim-8.2.0367/src/vim9compile.c   2020-03-04 22:20:23.366900841 +0100
--- src/vim9compile.c   2020-03-09 19:06:10.082736788 +0100
***************
*** 203,208 ****
--- 203,227 ----
      return di == NULL ? FAIL: OK;
  }
  
+ /*
+  * Check if "p[len]" is already defined, either in script "import_sid" or in
+  * compilation context "cctx".
+  * Return FAIL and give an error if it defined.
+  */
+     int
+ check_defined(char_u *p, int len, cctx_T *cctx)
+ {
+     if (lookup_script(p, len) == OK
+           || (cctx != NULL
+               && (lookup_local(p, len, cctx) >= 0
+                   || find_imported(p, len, cctx) != NULL)))
+     {
+       semsg("E1073: imported name already defined: %s", p);
+       return FAIL;
+     }
+     return OK;
+ }
+ 
      static type_T *
  get_list_type(type_T *member_type, garray_T *type_list)
  {
***************
*** 3812,3818 ****
      static char_u *
  compile_import(char_u *arg, cctx_T *cctx)
  {
!     return handle_import(arg, &cctx->ctx_imports, 0);
  }
  
  /*
--- 3831,3837 ----
      static char_u *
  compile_import(char_u *arg, cctx_T *cctx)
  {
!     return handle_import(arg, &cctx->ctx_imports, 0, cctx);
  }
  
  /*
*** ../vim-8.2.0367/src/proto/vim9compile.pro   2020-03-02 22:53:28.460549750 
+0100
--- src/proto/vim9compile.pro   2020-03-09 19:06:31.190607650 +0100
***************
*** 1,4 ****
--- 1,5 ----
  /* vim9compile.c */
+ int check_defined(char_u *p, int len, cctx_T *cctx);
  char_u *skip_type(char_u *start);
  type_T *parse_type(char_u **arg, garray_T *type_list);
  char *vartype_name(vartype_T type);
*** ../vim-8.2.0367/src/testdir/test_vim9_script.vim    2020-03-04 
21:50:43.572107266 +0100
--- src/testdir/test_vim9_script.vim    2020-03-09 19:22:46.093037867 +0100
***************
*** 362,368 ****
    enddef
  END
  
! def Test_vim9script()
    let import_script_lines =<< trim END
      vim9script
      import {exported, Exported} from './Xexport.vim'
--- 362,368 ----
    enddef
  END
  
! def Test_vim9_import_export()
    let import_script_lines =<< trim END
      vim9script
      import {exported, Exported} from './Xexport.vim'
***************
*** 449,454 ****
--- 449,481 ----
    writefile(import_not_exported_lines, 'Ximport.vim')
    assert_fails('source Ximport.vim', 'E1049:')
  
+   " try to import something that is already defined
+   let import_already_defined =<< trim END
+     vim9script
+     let exported = 'something'
+     import exported from './Xexport.vim'
+   END
+   writefile(import_already_defined, 'Ximport.vim')
+   assert_fails('source Ximport.vim', 'E1073:')
+ 
+   " try to import something that is already defined
+   import_already_defined =<< trim END
+     vim9script
+     let exported = 'something'
+     import * as exported from './Xexport.vim'
+   END
+   writefile(import_already_defined, 'Ximport.vim')
+   assert_fails('source Ximport.vim', 'E1073:')
+ 
+   " try to import something that is already defined
+   import_already_defined =<< trim END
+     vim9script
+     let exported = 'something'
+     import {exported} from './Xexport.vim'
+   END
+   writefile(import_already_defined, 'Ximport.vim')
+   assert_fails('source Ximport.vim', 'E1073:')
+ 
    " import a very long name, requires making a copy
    let import_long_name_lines =<< trim END
      vim9script
***************
*** 482,491 ****
      vim9script
      import {exported name} from './Xexport.vim'
    END
!   writefile(import_missing_comma_lines, 'Ximport.vim')
!   assert_fails('source Ximport.vim', 'E1046:')
  
    delete('Ximport.vim')
    delete('Xexport.vim')
  
    " Check that in a Vim9 script 'cpo' is set to the Vim default.
--- 509,519 ----
      vim9script
      import {exported name} from './Xexport.vim'
    END
!   writefile(import_missing_comma_lines, 'Ximport3.vim')
!   assert_fails('source Ximport3.vim', 'E1046:')
  
    delete('Ximport.vim')
+   delete('Ximport3.vim')
    delete('Xexport.vim')
  
    " Check that in a Vim9 script 'cpo' is set to the Vim default.
*** ../vim-8.2.0367/src/version.c       2020-03-09 16:40:36.250782076 +0100
--- src/version.c       2020-03-09 18:36:01.800413872 +0100
***************
*** 740,741 ****
--- 740,743 ----
  {   /* Add new patch number below this line */
+ /**/
+     368,
  /**/

-- 
hundred-and-one symptoms of being an internet addict:
201. When somebody asks you where you are, you tell them in which chat room.

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
 \\\            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/202003091827.029IR51v024210%40masaka.moolenaar.net.

Raspunde prin e-mail lui