]> granicus.if.org Git - vim/commitdiff
patch 8.2.1349: Vim9: can define a function with the name of an import v8.2.1349
authorBram Moolenaar <Bram@vim.org>
Sat, 1 Aug 2020 20:16:43 +0000 (22:16 +0200)
committerBram Moolenaar <Bram@vim.org>
Sat, 1 Aug 2020 20:16:43 +0000 (22:16 +0200)
Problem:    Vim9: can define a function with the name of an import.
Solution:   Disallow using an existing name. (closes #6585)

src/globals.h
src/testdir/test_vim9_script.vim
src/userfunc.c
src/version.c
src/vim9compile.c

index a0ebc4506a7acd66f587f2440b0c223590be16fd..cc01ac2508d579b2e406255fdf28bf5b89a62a5b 100644 (file)
@@ -1746,6 +1746,7 @@ EXTERN char e_missing_dict_colon[] INIT(= N_("E720: Missing colon in Dictionary:
 EXTERN char e_duplicate_key[]  INIT(= N_("E721: Duplicate key in Dictionary: \"%s\""));
 EXTERN char e_missing_dict_comma[] INIT(= N_("E722: Missing comma in Dictionary: %s"));
 EXTERN char e_missing_dict_end[]    INIT(= N_("E723: Missing end of Dictionary '}': %s"));
+EXTERN char e_already_defined[] INIT(= N_("E1073: name already defined: %s"));
 #endif
 #ifdef FEAT_CLIENTSERVER
 EXTERN char e_invexprmsg[]     INIT(= N_("E449: Invalid expression received"));
index 8950f311a7afbf38e26ba4c385c125d3e7850be9..221b38d7a88466fd2b60c81282132642f53ebe00 100644 (file)
@@ -1618,6 +1618,39 @@ def Test_import_compile_error()
   delete('Ximport.vim')
 enddef
 
+def Test_func_overrules_import_fails()
+  let export_lines =<< trim END
+      vim9script
+      export def Func()
+        echo 'imported'
+      enddef
+  END
+  writefile(export_lines, 'XexportedFunc.vim')
+
+  let lines =<< trim END
+    vim9script
+    import Func from './XexportedFunc.vim'
+    def Func()
+      echo 'local to function'
+    enddef
+  END
+  CheckScriptFailure(lines, 'E1073:')
+
+  lines =<< trim END
+    vim9script
+    import Func from './XexportedFunc.vim'
+    def Outer()
+      def Func()
+        echo 'local to function'
+      enddef
+    enddef
+    defcompile
+  END
+  CheckScriptFailure(lines, 'E1073:')
+
+  delete('XexportedFunc.vim')
+enddef
+
 def Test_fixed_size_list()
   # will be allocated as one piece of memory, check that changes work
   let l = [1, 2, 3, 4]
index 520c114ef5df5b5d6731ebf96d60c4d3f0479bdf..43aa146a9827bd5a801336fd77d4af70679af43d 100644 (file)
@@ -2652,6 +2652,7 @@ def_function(exarg_T *eap, char_u *name_arg)
     char_u     *skip_until = NULL;
     char_u     *heredoc_trimmed = NULL;
     int                vim9script = in_vim9script();
+    imported_T *import = NULL;
 
     /*
      * ":function" without argument: list functions.
@@ -3235,17 +3236,29 @@ def_function(exarg_T *eap, char_u *name_arg)
        }
 
        fp = find_func_even_dead(name, is_global, NULL);
-       if (fp != NULL)
+       if (vim9script)
+       {
+           char_u *uname = untrans_function_name(name);
+
+           import = find_imported(uname == NULL ? name : uname, 0, NULL);
+       }
+
+       if (fp != NULL || import != NULL)
        {
-           int dead = fp->uf_flags & FC_DEAD;
+           int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
 
            // Function can be replaced with "function!" and when sourcing the
            // same script again, but only once.
-           if (!dead && !eap->forceit
+           // A name that is used by an import can not be overruled.
+           if (import != NULL
+                   || (!dead && !eap->forceit
                        && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
-                           || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
+                         || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
            {
-               emsg_funcname(e_funcexts, name);
+               if (vim9script)
+                   emsg_funcname(e_already_defined, name);
+               else
+                   emsg_funcname(e_funcexts, name);
                goto erret;
            }
            if (fp->uf_calls > 0)
index e715d8ae37b20de3fd80e753f464113690a6743a..c98d2ab76c7773e7dfbaeeecd0a281b05deaf6d4 100644 (file)
@@ -754,6 +754,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1349,
 /**/
     1348,
 /**/
index 38c097c7f8396852f35098b6cd83e7cee6a122ec..6581708bc9636b7879314b270572fa083f1ee1d2 100644 (file)
@@ -294,9 +294,10 @@ check_defined(char_u *p, size_t len, cctx_T *cctx)
     if (lookup_script(p, len) == OK
            || (cctx != NULL
                && (lookup_local(p, len, cctx) != NULL
-                   || find_imported(p, len, cctx) != NULL)))
+                   || lookup_arg(p, len, NULL, NULL, NULL, cctx) == OK))
+           || find_imported(p, len, cctx) != NULL)
     {
-       semsg("E1073: imported name already defined: %s", p);
+       semsg(_(e_already_defined), p);
        return FAIL;
     }
     return OK;
@@ -4899,17 +4900,21 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx)
     int                is_global = *eap->arg == 'g' && eap->arg[1] == ':';
     char_u     *name_start = eap->arg;
     char_u     *name_end = to_name_end(eap->arg, is_global);
-    char_u     *name = get_lambda_name();
+    char_u     *lambda_name;
     lvar_T     *lvar;
     ufunc_T    *ufunc;
     int                r;
 
+    if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
+       return NULL;
+
     eap->arg = name_end;
     eap->getline = exarg_getline;
     eap->cookie = cctx;
     eap->skip = cctx->ctx_skip == SKIP_YES;
     eap->forceit = FALSE;
-    ufunc = def_function(eap, name);
+    lambda_name = get_lambda_name();
+    ufunc = def_function(eap, lambda_name);
 
     if (ufunc == NULL)
        return NULL;
@@ -4925,13 +4930,15 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx)
        if (func_name == NULL)
            r = FAIL;
        else
-           r = generate_NEWFUNC(cctx, name, func_name);
+           r = generate_NEWFUNC(cctx, lambda_name, func_name);
     }
     else
     {
        // Define a local variable for the function reference.
        lvar = reserve_local(cctx, name_start, name_end - name_start,
                                                    TRUE, ufunc->uf_func_type);
+       if (lvar == NULL)
+           return NULL;
        if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL)
            return NULL;
        r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);