diff src/userfunc.c @ 21598:7b5b9558500a v8.2.1349

patch 8.2.1349: Vim9: can define a function with the name of an import Commit: https://github.com/vim/vim/commit/eef2102e20d24f5fbd1c9f53c7a35df61585c5ab Author: Bram Moolenaar <Bram@vim.org> Date: Sat Aug 1 22:16:43 2020 +0200 patch 8.2.1349: Vim9: can define a function with the name of an import Problem: Vim9: can define a function with the name of an import. Solution: Disallow using an existing name. (closes https://github.com/vim/vim/issues/6585)
author Bram Moolenaar <Bram@vim.org>
date Sat, 01 Aug 2020 22:30:04 +0200
parents 5470c36ed7e6
children a640bc762196
line wrap: on
line diff
--- a/src/userfunc.c
+++ b/src/userfunc.c
@@ -2652,6 +2652,7 @@ def_function(exarg_T *eap, char_u *name_
     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_
 	}
 
 	fp = find_func_even_dead(name, is_global, NULL);
-	if (fp != NULL)
+	if (vim9script)
 	{
-	    int dead = fp->uf_flags & FC_DEAD;
+	    char_u *uname = untrans_function_name(name);
+
+	    import = find_imported(uname == NULL ? name : uname, 0, NULL);
+	}
+
+	if (fp != NULL || import != NULL)
+	{
+	    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)