changeset 27233:73232ed49cf2 v8.2.4145

patch 8.2.4145: confusing error when using name of import for a function Commit: https://github.com/vim/vim/commit/937610bc9f9c827e3e25fed32661fcbf3f994e10 Author: Bram Moolenaar <Bram@vim.org> Date: Wed Jan 19 17:21:29 2022 +0000 patch 8.2.4145: confusing error when using name of import for a function Problem: Confusing error when using name of import for a function. Solution: Pass a flag to trans_function_name().
author Bram Moolenaar <Bram@vim.org>
date Wed, 19 Jan 2022 18:30:05 +0100
parents d0ce0a7ce04c
children 01767caa86b2
files src/eval.c src/proto/userfunc.pro src/testdir/test_vim9_import.vim src/userfunc.c src/version.c src/vim.h
diffstat 6 files changed, 27 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/eval.c
+++ b/src/eval.c
@@ -2072,7 +2072,7 @@ eval_func(
     // If "s" is the name of a variable of type VAR_FUNC
     // use its contents.
     s = deref_func_name(s, &len, &partial,
-			in_vim9script() ? &type : NULL, !evaluate, &found_var);
+		 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
 
     // Need to make a copy, in case evaluating the arguments makes
     // the name invalid.
--- a/src/proto/userfunc.pro
+++ b/src/proto/userfunc.pro
@@ -4,7 +4,7 @@ hashtab_T *func_tbl_get(void);
 char_u *get_lambda_name(void);
 char_u *register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state);
 int get_lambda_tv(char_u **arg, typval_T *rettv, int types_optional, evalarg_T *evalarg);
-char_u *deref_func_name(char_u *name, int *lenp, partial_T **partialp, type_T **type, int no_autoload, int *found_var);
+char_u *deref_func_name(char_u *name, int *lenp, partial_T **partialp, type_T **type, int no_autoload, int new_function, int *found_var);
 void emsg_funcname(char *ermsg, char_u *name);
 int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, evalarg_T *evalarg, funcexe_T *funcexe);
 char_u *fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error);
--- a/src/testdir/test_vim9_import.vim
+++ b/src/testdir/test_vim9_import.vim
@@ -458,6 +458,16 @@ def Test_import_fails()
   CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use That itself'])
 
   lines =<< trim END
+      vim9script
+      import './Xthat.vim' as That
+      def Func()
+        echo That()
+      enddef
+      Func()
+  END
+  CheckScriptFailure(lines, 'E1236: Cannot use That itself')
+
+  lines =<< trim END
       import './Xthat.vim' as one
       import './Xthat.vim' as two
   END
@@ -1000,7 +1010,7 @@ def Test_func_overrules_import_fails()
       echo 'local to function'
     enddef
   END
-  CheckScriptFailure(lines, 'E1236:')
+  CheckScriptFailure(lines, 'E1213: Redefining imported item "Func"')
 
   lines =<< trim END
     vim9script
--- a/src/userfunc.c
+++ b/src/userfunc.c
@@ -1567,6 +1567,7 @@ errret:
  * "partialp".
  * If "type" is not NULL and a Vim9 script-local variable is found look up the
  * type of the variable.
+ * If "new_function" is TRUE the name is for a new function.
  * If "found_var" is not NULL and a variable was found set it to TRUE.
  */
     char_u *
@@ -1576,6 +1577,7 @@ deref_func_name(
 	partial_T   **partialp,
 	type_T	    **type,
 	int	    no_autoload,
+	int	    new_function,
 	int	    *found_var)
 {
     dictitem_T	*v;
@@ -1614,7 +1616,10 @@ deref_func_name(
 	if (import != NULL)
 	{
 	    name[len] = NUL;
-	    semsg(_(e_cannot_use_str_itself_it_is_imported), name);
+	    if (new_function)
+		semsg(_(e_redefining_imported_item_str), name);
+	    else
+		semsg(_(e_cannot_use_str_itself_it_is_imported), name);
 	    name[len] = cc;
 	    *lenp = 0;
 	    return (char_u *)"";	// just in case
@@ -3751,7 +3756,7 @@ trans_function_name(
     {
 	len = (int)STRLEN(lv.ll_exp_name);
 	name = deref_func_name(lv.ll_exp_name, &len, partial, type,
-						flags & TFN_NO_AUTOLOAD, NULL);
+			  flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
 	if (name == lv.ll_exp_name)
 	    name = NULL;
     }
@@ -3783,7 +3788,7 @@ trans_function_name(
     {
 	len = (int)(end - *pp);
 	name = deref_func_name(*pp, &len, partial, type,
-						flags & TFN_NO_AUTOLOAD, NULL);
+			  flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
 	if (name == *pp)
 	    name = NULL;
     }
@@ -4146,7 +4151,7 @@ define_function(exarg_T *eap, char_u *na
     else
     {
 	name = save_function_name(&p, &is_global, eap->skip,
-						       TFN_NO_AUTOLOAD, &fudi);
+					TFN_NO_AUTOLOAD | TFN_NEW_FUNC, &fudi);
 	paren = (vim_strchr(p, '(') != NULL);
 	if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
 	{
@@ -5199,7 +5204,8 @@ ex_call(exarg_T *eap)
     // from trans_function_name().
     len = (int)STRLEN(tofree);
     name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
-	    in_vim9script() && type == NULL ? &type : NULL, FALSE, &found_var);
+	    in_vim9script() && type == NULL ? &type : NULL,
+						     FALSE, FALSE, &found_var);
 
     // Skip white space to allow ":call func ()".  Not good, but required for
     // backward compatibility.
--- a/src/version.c
+++ b/src/version.c
@@ -751,6 +751,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    4145,
+/**/
     4144,
 /**/
     4143,
--- a/src/vim.h
+++ b/src/vim.h
@@ -2632,6 +2632,7 @@ typedef enum {
 #define TFN_READ_ONLY	0x10	// will not change the var
 #define TFN_NO_DECL	0x20	// only used for GLV_NO_DECL
 #define TFN_COMPILING	0x40	// only used for GLV_COMPILING
+#define TFN_NEW_FUNC	0x80	// defining a new function
 
 // Values for get_lval() flags argument:
 #define GLV_QUIET	TFN_QUIET	// no error messages