]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 8.2.4460: Vim9: wrong error for defining dict function v8.2.4460
authorBram Moolenaar <Bram@vim.org>
Wed, 23 Feb 2022 22:12:02 +0000 (22:12 +0000)
committerBram Moolenaar <Bram@vim.org>
Wed, 23 Feb 2022 22:12:02 +0000 (22:12 +0000)
Problem:    Vim9: wrong error for defining dict function.
Solution:   Explicitly check for trying to define a dict function.
            (closes 9827)

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

index 5f2685ab3ea4f8d9458529ee370dd8739479ff22..72c0d1284f28142062847b9d16c8c75f07482a07 100644 (file)
@@ -3010,7 +3010,8 @@ EXTERN char e_variable_arguments_type_must_be_list_str[]
        INIT(= N_("E1180: Variable arguments type must be a list: %s"));
 EXTERN char e_cannot_use_underscore_here[]
        INIT(= N_("E1181: Cannot use an underscore here"));
-// E1182 unused
+EXTERN char e_cannot_define_dict_func_in_vim9_script_str[]
+       INIT(= N_("E1182: Cannot define a dict function in Vim9 script: %s"));
 EXTERN char e_cannot_use_range_with_assignment_operator_str[]
        INIT(= N_("E1183: Cannot use a range with an assignment operator: %s"));
 #endif
index 81a67d97fbacb1449fa41d32b74bcc1e5e9ec355..74883975bb3b179eca33d8e98e61379296b0960c 100644 (file)
@@ -106,6 +106,63 @@ def Test_wrong_function_name()
       enddef
   END
   v9.CheckScriptFailure(lines, 'E1267:')
+
+  lines =<< trim END
+      vim9script
+      var Object = {}
+      function Object.Method()
+      endfunction
+  END
+  v9.CheckScriptFailure(lines, 'E1182:')
+
+  lines =<< trim END
+      vim9script
+      var Object = {}
+      def Object.Method()
+      enddef
+  END
+  v9.CheckScriptFailure(lines, 'E1182:')
+
+  lines =<< trim END
+      vim9script
+      g:Object = {}
+      function g:Object.Method()
+      endfunction
+  END
+  v9.CheckScriptFailure(lines, 'E1182:')
+
+  lines =<< trim END
+      let s:Object = {}
+      def Define()
+        function s:Object.Method()
+        endfunction
+      enddef
+      defcompile
+  END
+  v9.CheckScriptFailure(lines, 'E1182:')
+  delfunc g:Define
+
+  lines =<< trim END
+      let s:Object = {}
+      def Define()
+        def Object.Method()
+        enddef
+      enddef
+      defcompile
+  END
+  v9.CheckScriptFailure(lines, 'E1182:')
+  delfunc g:Define
+
+  lines =<< trim END
+      let g:Object = {}
+      def Define()
+        function g:Object.Method()
+        endfunction
+      enddef
+      defcompile
+  END
+  v9.CheckScriptFailure(lines, 'E1182:')
+  delfunc g:Define
 enddef
 
 def Test_autoload_name_mismatch()
index 3a66d273248dd671b29a66bc9f636f6058e419e4..ed2fb8668cc1179a537b13248cb7b3a1be9d868b 100644 (file)
@@ -4268,10 +4268,21 @@ define_function(exarg_T *eap, char_u *name_arg, garray_T *lines_to_free)
     }
     else
     {
-       if (vim9script && p[0] == 's' && p[1] == ':')
+       if (vim9script)
        {
-           semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
-           return NULL;
+           if (p[0] == 's' && p[1] == ':')
+           {
+               semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
+               return NULL;
+           }
+           p = to_name_end(p, TRUE);
+           if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
+           {
+               semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
+                                                                    eap->arg);
+               return NULL;
+           }
+           p = eap->arg;
        }
 
        name = save_function_name(&p, &is_global, eap->skip,
index f8fb3139c9d170874a86ba8ace06851e87aaa065..a34bad1e759fc1c583bc1cb5be93742fee409705 100644 (file)
@@ -754,6 +754,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    4460,
 /**/
     4459,
 /**/
index 6eb6e160e26ce66647a48694883f042166a0ca6a..898712396e1073394ab41b557d110c95f2e03193 100644 (file)
@@ -831,7 +831,11 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx, garray_T *lines_to_free)
     {
        if (!ends_excmd2(name_start, name_end))
        {
-           semsg(_(e_invalid_command_str), eap->cmd);
+           if (*skipwhite(name_end) == '.')
+               semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
+                                                                    eap->cmd);
+           else
+               semsg(_(e_invalid_command_str), eap->cmd);
            return NULL;
        }