]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.0314: Vim9: Can define a class in a function v9.1.0314
authorYegappan Lakshmanan <yegappan@yahoo.com>
Sat, 13 Apr 2024 15:58:09 +0000 (17:58 +0200)
committerChristian Brabandt <cb@256bit.org>
Sat, 13 Apr 2024 16:04:53 +0000 (18:04 +0200)
Problem:  Vim9: Can define a class in a function
          (Doug Kearns)
Solution: Give an error for a class defined in a function,
          slightly reword some public error messages
          (Yegappan Lakshmanan)

fixes: #13184
fixes: #13326
closes: #14537

Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
runtime/doc/tags
runtime/doc/vim9.txt
runtime/doc/vim9class.txt
src/errors.h
src/testdir/test_vim9_class.vim
src/version.c
src/vim9class.c
src/vim9compile.c

index fc55f843737b6cb50c3c1d9de7c4192a0e43c3c6..229a4ef1eae487800bc51004e738d3d54b338eae 100644 (file)
@@ -4534,6 +4534,7 @@ E1423     vim9class.txt   /*E1423*
 E1424  vim9class.txt   /*E1424*
 E1425  vim9class.txt   /*E1425*
 E1426  vim9class.txt   /*E1426*
+E1429  vim9class.txt   /*E1429*
 E143   autocmd.txt     /*E143*
 E144   various.txt     /*E144*
 E145   starting.txt    /*E145*
index ffb5c2e19ae8f629181bc13abef425163c8aa021..7a49aa082637f4ab83182f176d0f72ef13f0ccbb 100644 (file)
@@ -1,4 +1,4 @@
-*vim9.txt*     For Vim version 9.1.  Last change: 2024 Apr 12
+*vim9.txt*     For Vim version 9.1.  Last change: 2024 Apr 13
 
 
                  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -1521,7 +1521,6 @@ Custom types can be defined with `:type`: >
        :type MyList list<string>
 Custom types must start with a capital letter, to avoid name clashes with
 builtin types added later, similarly to user functions.
-{not implemented yet}
 
 And classes and interfaces can be used as types: >
        :class MyClass
index 804e02dc17a0e14ec5bf50d2e0984ddf041d15d9..ef96aa9076e2e9518d5a39b14b017d5122be66f1 100644 (file)
@@ -1,4 +1,4 @@
-*vim9class.txt*        For Vim version 9.1.  Last change: 2024 Apr 04
+*vim9class.txt*        For Vim version 9.1.  Last change: 2024 Apr 13
 
 
                  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -593,7 +593,7 @@ A class is defined between `:class` and `:endclass`.  The whole class is
 defined in one script file.  It is not possible to add to a class later.
 
 A class can only be defined in a |Vim9| script file.  *E1316*
-A class cannot be defined inside a function.
+A class cannot be defined inside a function.  *E1429*
 
 It is possible to define more than one class in a script file.  Although it
 usually is better to export only one main class.  It can be useful to define
index 00f7df07c3ccb5338c87551259b6f7215c1b36e0..4387a29ec7fc4d7f6c4f4dfa37b72a5429ed3895 100644 (file)
@@ -3412,9 +3412,9 @@ EXTERN char e_invalid_class_variable_declaration_str[]
 EXTERN char e_invalid_type_for_object_variable_str[]
        INIT(= N_("E1330: Invalid type for object variable: %s"));
 EXTERN char e_public_must_be_followed_by_var_static_final_or_const[]
-       INIT(= N_("E1331: Public must be followed by \"var\" or \"static\" or \"final\" or \"const\""));
+       INIT(= N_("E1331: public must be followed by \"var\" or \"static\" or \"final\" or \"const\""));
 EXTERN char e_public_variable_name_cannot_start_with_underscore_str[]
-       INIT(= N_("E1332: Public variable name cannot start with underscore: %s"));
+       INIT(= N_("E1332: public variable name cannot start with underscore: %s"));
 EXTERN char e_cannot_access_protected_variable_str[]
        INIT(= N_("E1333: Cannot access protected variable \"%s\" in class \"%s\""));
 // E1334 unused
@@ -3532,9 +3532,9 @@ EXTERN char e_class_method_str_accessible_only_using_class_str[]
 EXTERN char e_object_method_str_accessible_only_using_object_str[]
        INIT(= N_("E1386: Object method \"%s\" accessible only using class \"%s\" object"));
 EXTERN char e_public_variable_not_supported_in_interface[]
-       INIT(= N_("E1387: Public variable not supported in an interface"));
+       INIT(= N_("E1387: public variable not supported in an interface"));
 EXTERN char e_public_keyword_not_supported_for_method[]
-       INIT(= N_("E1388: Public keyword not supported for a method"));
+       INIT(= N_("E1388: public keyword not supported for a method"));
 EXTERN char e_missing_name_after_implements[]
        INIT(= N_("E1389: Missing name after implements"));
 EXTERN char e_cannot_use_an_object_variable_except_with_the_new_method_str[]
@@ -3615,6 +3615,8 @@ EXTERN char e_enum_str_name_cannot_be_modified[]
        INIT(= N_("E1427: Enum \"%s\" name cannot be modified"));
 EXTERN char e_duplicate_enum_str[]
        INIT(= N_("E1428: Duplicate enum value: %s"));
+EXTERN char e_class_can_only_be_used_in_script[]
+       INIT(= N_("E1429: Class can only be used in a script"));
 #endif
 // E1429 - E1499 unused (reserved for Vim9 class support)
 EXTERN char e_cannot_mix_positional_and_non_positional_str[]
index 6d6d1785104454c27b4e20e99bb1bae305ca5210..5957f57ae1d3631219910114ff343c367467178f 100644 (file)
@@ -400,7 +400,7 @@ def Test_class_def_method()
       enddef
     endclass
   END
-  v9.CheckSourceFailure(lines, 'E1331: Public must be followed by "var" or "static"', 3)
+  v9.CheckSourceFailure(lines, 'E1388: public keyword not supported for a method', 3)
 
   # Using the "public" keyword when defining a class method
   lines =<< trim END
@@ -410,7 +410,7 @@ def Test_class_def_method()
       enddef
     endclass
   END
-  v9.CheckSourceFailure(lines, 'E1388: Public keyword not supported for a method', 3)
+  v9.CheckSourceFailure(lines, 'E1388: public keyword not supported for a method', 3)
 
   # Using the "public" keyword when defining an object protected method
   lines =<< trim END
@@ -420,7 +420,7 @@ def Test_class_def_method()
       enddef
     endclass
   END
-  v9.CheckSourceFailure(lines, 'E1331: Public must be followed by "var" or "static"', 3)
+  v9.CheckSourceFailure(lines, 'E1388: public keyword not supported for a method', 3)
 
   # Using the "public" keyword when defining a class protected method
   lines =<< trim END
@@ -430,7 +430,7 @@ def Test_class_def_method()
       enddef
     endclass
   END
-  v9.CheckSourceFailure(lines, 'E1388: Public keyword not supported for a method', 3)
+  v9.CheckSourceFailure(lines, 'E1388: public keyword not supported for a method', 3)
 
   # Using a "def" keyword without an object method name
   lines =<< trim END
@@ -1191,7 +1191,7 @@ def Test_instance_variable_access()
       public var _val = 10
     endclass
   END
-  v9.CheckSourceFailure(lines, 'E1332: Public variable name cannot start with underscore: public var _val = 10', 3)
+  v9.CheckSourceFailure(lines, 'E1332: public variable name cannot start with underscore: public var _val = 10', 3)
 
   lines =<< trim END
     vim9script
@@ -1287,7 +1287,7 @@ def Test_instance_variable_access()
       public val = 1
     endclass
   END
-  v9.CheckSourceFailure(lines, 'E1331: Public must be followed by "var" or "static"', 3)
+  v9.CheckSourceFailure(lines, 'E1331: public must be followed by "var" or "static"', 3)
 
   # Modify a instance variable using the class name in the script context
   lines =<< trim END
@@ -6537,7 +6537,7 @@ def Test_interface_with_unsupported_members()
       public static var num: number
     endinterface
   END
-  v9.CheckSourceFailure(lines, 'E1387: Public variable not supported in an interface', 3)
+  v9.CheckSourceFailure(lines, 'E1387: public variable not supported in an interface', 3)
 
   lines =<< trim END
     vim9script
@@ -6545,7 +6545,7 @@ def Test_interface_with_unsupported_members()
       public static var num: number
     endinterface
   END
-  v9.CheckSourceFailure(lines, 'E1387: Public variable not supported in an interface', 3)
+  v9.CheckSourceFailure(lines, 'E1387: public variable not supported in an interface', 3)
 
   lines =<< trim END
     vim9script
@@ -10625,4 +10625,17 @@ def Test_abstract_method_defcompile()
   v9.CheckScriptFailure(lines, 'E476: Invalid command: pass', 1)
 enddef
 
+" Test for defining a class in a function
+def Test_class_definition_in_a_function()
+  var lines =<< trim END
+    vim9script
+    def Foo()
+      class A
+      endclass
+    enddef
+    defcompile
+  END
+  v9.CheckScriptFailure(lines, 'E1429: Class can only be used in a script', 1)
+enddef
+
 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
index dd7f2035ee7557e53b4e9539c4b2b2d912781cb2..9faa0539edd99a7b2c82f78770bd5857951a20d6 100644 (file)
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    314,
 /**/
     313,
 /**/
index a6b3ee2dc67fdb978d788c56be26e5ebb08b1020..52c2f764db819ec4b7481985d4507529976ac3eb 100644 (file)
@@ -2080,6 +2080,12 @@ early_ret:
            has_public = TRUE;
            p = skipwhite(line + 6);
 
+           if (STRNCMP(p, "def", 3) == 0)
+           {
+               emsg(_(e_public_keyword_not_supported_for_method));
+               break;
+           }
+
            if (STRNCMP(p, "var", 3) != 0 && STRNCMP(p, "static", 6) != 0
                && STRNCMP(p, "final", 5) != 0 && STRNCMP(p, "const", 5) != 0)
            {
index c6b02478ce26bbf6bf587e518636afd436859cfc..7e1d911c1cbfeabb045f09951360247c57d33988 100644 (file)
@@ -4028,10 +4028,13 @@ compile_def_function(
                    line = (char_u *)"";
                    break;
 
+           case CMD_class:
+                   emsg(_(e_class_can_only_be_used_in_script));
+                   goto erret;
+
            case CMD_type:
                    emsg(_(e_type_can_only_be_used_in_script));
                    goto erret;
-                   break;
 
            case CMD_global:
                    if (check_global_and_subst(ea.cmd, p) == FAIL)