]> granicus.if.org Git - vim/commitdiff
patch 8.2.4324: Vim9: script-local function name can start with "_" v8.2.4324
authorBram Moolenaar <Bram@vim.org>
Mon, 7 Feb 2022 21:54:01 +0000 (21:54 +0000)
committerBram Moolenaar <Bram@vim.org>
Mon, 7 Feb 2022 21:54:01 +0000 (21:54 +0000)
Problem:    Vim9: script-local function name can start with "_".
Solution:   Check for leading capital after "s:".  Correct error message.

src/errors.h
src/testdir/test_vim9_func.vim
src/userfunc.c
src/version.c
src/vim9compile.c

index 53bd0cd26cdce1be5576409709f2dc43e9fc8332..ecd4eb92d020775b689e6c353d26236e7be2e7a9 100644 (file)
@@ -3228,3 +3228,7 @@ EXTERN char e_cannot_use_partial_here[]
 EXTERN char e_critical_error_in_python3_initialization_check_your_installation[]
        INIT(= N_("E1266: Critical error in python3 initialization, check your python3 installation"));
 #endif
+#ifdef FEAT_EVAL
+EXTERN char e_function_name_must_start_with_capital_str[]
+       INIT(= N_("E1267: Function name must start with a capital: %s"));
+#endif
index 86b0763dcc8b347cdb5d6ec6248ca94d112d62db..ebcd0fbc8ecc6d2e39e64cdf8a87044a277c499f 100644 (file)
@@ -97,7 +97,7 @@ def Test_wrong_function_name()
         echo 'foo'
       endfunc
   END
-  v9.CheckScriptFailure(lines, 'E128:')
+  v9.CheckScriptFailure(lines, 'E1267:')
 
   lines =<< trim END
       vim9script
@@ -105,7 +105,7 @@ def Test_wrong_function_name()
         echo 'foo'
       enddef
   END
-  v9.CheckScriptFailure(lines, 'E128:')
+  v9.CheckScriptFailure(lines, 'E1267:')
 enddef
 
 def Test_autoload_name_mismatch()
@@ -685,11 +685,11 @@ def Test_nested_function()
         def _Inner()
           echo 'bad'
         enddef
-        Inner()
+        _Inner()
       enddef
       defcompile
   END
-  v9.CheckScriptFailure(lines, 'E128:')
+  v9.CheckScriptFailure(lines, 'E1267:')
 
   lines =<< trim END
       vim9script
@@ -697,11 +697,27 @@ def Test_nested_function()
         def g:inner()
           echo 'bad'
         enddef
-        Inner()
+        g:inner()
       enddef
       defcompile
   END
-  v9.CheckScriptFailure(lines, 'E128:')
+  v9.CheckScriptFailure(lines, 'E1267:')
+
+  lines =<< trim END
+      vim9script
+      def g:_Func()
+        echo 'bad'
+      enddef
+  END
+  v9.CheckScriptFailure(lines, 'E1267:')
+
+  lines =<< trim END
+      vim9script
+      def s:_Func()
+        echo 'bad'
+      enddef
+  END
+  v9.CheckScriptFailure(lines, 'E1267:')
 
   # nested function inside conditional
   lines =<< trim END
@@ -2772,7 +2788,7 @@ def Test_nested_inline_lambda()
   lines =<< trim END
       vim9script
 
-      def s:func()
+      def s:Func()
         range(10)
           ->mapnew((_, _) => ({
             key: range(10)->mapnew((_, _) => {
index 23131b7515e26a3d7600d6e6fc1b94a7c1ac85a3..0c54e35749473c56387402877b8c266ef22199b3 100644 (file)
@@ -3884,15 +3884,6 @@ trans_function_name(
     // In Vim9 script a user function is script-local by default, unless it
     // starts with a lower case character: dict.func().
     vim9script = ASCII_ISUPPER(*start) && in_vim9script();
-    if (vim9script)
-    {
-       char_u *p;
-
-       // SomeScript#func() is a global function.
-       for (p = start; *p != NUL && *p != '('; ++p)
-           if (*p == AUTOLOAD_CHAR)
-               vim9script = FALSE;
-    }
 
     /*
      * Copy the function name to allocated memory.
@@ -3904,7 +3895,17 @@ trans_function_name(
     else if (lead > 0 || vim9script)
     {
        if (!vim9script)
+       {
+           if (in_vim9script() && lead == 2 && !ASCII_ISUPPER(*lv.ll_name))
+           {
+               semsg(_(in_vim9script()
+                          ? e_function_name_must_start_with_capital_str
+                          : e_function_name_must_start_with_capital_or_s_str),
+                                                                       start);
+               goto theend;
+           }
            lead = 3;
+       }
        if (vim9script || (lv.ll_exp_name != NULL
                                             && eval_fname_sid(lv.ll_exp_name))
                                                       || eval_fname_sid(*pp))
@@ -3925,7 +3926,10 @@ trans_function_name(
     else if (!(flags & TFN_INT) && (builtin_function(lv.ll_name, len)
                                   || (in_vim9script() && *lv.ll_name == '_')))
     {
-       semsg(_(e_function_name_must_start_with_capital_or_s_str), start);
+       semsg(_(in_vim9script()
+                          ? e_function_name_must_start_with_capital_str
+                          : e_function_name_must_start_with_capital_or_s_str),
+                                                                       start);
        goto theend;
     }
     if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
index 2d0bb2d6c00acbbf1aea6e92c23ea9dc39adca20..cefbd4089f58f23ce71d0760f1ad2c43b8e57ad2 100644 (file)
@@ -746,6 +746,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    4324,
 /**/
     4323,
 /**/
index 080a53c6931d6645d8375cd4e7964577cc02d8f8..debead41814b9987828c31a1e4746bd4f16e293c 100644 (file)
@@ -888,7 +888,7 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx, garray_T *lines_to_free)
        return NULL;
     if (!ASCII_ISUPPER(is_global ? name_start[2] : name_start[0]))
     {
-       semsg(_(e_function_name_must_start_with_capital_or_s_str), name_start);
+       semsg(_(e_function_name_must_start_with_capital_str), name_start);
        return NULL;
     }