Skip to content

Commit c51578f

Browse files
yegappanchrisbra
authored andcommitted
patch 9.1.0314: Vim9: Can define a class in a function
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>
1 parent 122d068 commit c51578f

8 files changed

Lines changed: 43 additions & 17 deletions

File tree

runtime/doc/tags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4534,6 +4534,7 @@ E1423 vim9class.txt /*E1423*
45344534
E1424 vim9class.txt /*E1424*
45354535
E1425 vim9class.txt /*E1425*
45364536
E1426 vim9class.txt /*E1426*
4537+
E1429 vim9class.txt /*E1429*
45374538
E143 autocmd.txt /*E143*
45384539
E144 various.txt /*E144*
45394540
E145 starting.txt /*E145*

runtime/doc/vim9.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*vim9.txt* For Vim version 9.1. Last change: 2024 Apr 12
1+
*vim9.txt* For Vim version 9.1. Last change: 2024 Apr 13
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -1521,7 +1521,6 @@ Custom types can be defined with `:type`: >
15211521
:type MyList list<string>
15221522
Custom types must start with a capital letter, to avoid name clashes with
15231523
builtin types added later, similarly to user functions.
1524-
{not implemented yet}
15251524

15261525
And classes and interfaces can be used as types: >
15271526
:class MyClass

runtime/doc/vim9class.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*vim9class.txt* For Vim version 9.1. Last change: 2024 Apr 04
1+
*vim9class.txt* For Vim version 9.1. Last change: 2024 Apr 13
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -593,7 +593,7 @@ A class is defined between `:class` and `:endclass`. The whole class is
593593
defined in one script file. It is not possible to add to a class later.
594594

595595
A class can only be defined in a |Vim9| script file. *E1316*
596-
A class cannot be defined inside a function.
596+
A class cannot be defined inside a function. *E1429*
597597

598598
It is possible to define more than one class in a script file. Although it
599599
usually is better to export only one main class. It can be useful to define

src/errors.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3412,9 +3412,9 @@ EXTERN char e_invalid_class_variable_declaration_str[]
34123412
EXTERN char e_invalid_type_for_object_variable_str[]
34133413
INIT(= N_("E1330: Invalid type for object variable: %s"));
34143414
EXTERN char e_public_must_be_followed_by_var_static_final_or_const[]
3415-
INIT(= N_("E1331: Public must be followed by \"var\" or \"static\" or \"final\" or \"const\""));
3415+
INIT(= N_("E1331: public must be followed by \"var\" or \"static\" or \"final\" or \"const\""));
34163416
EXTERN char e_public_variable_name_cannot_start_with_underscore_str[]
3417-
INIT(= N_("E1332: Public variable name cannot start with underscore: %s"));
3417+
INIT(= N_("E1332: public variable name cannot start with underscore: %s"));
34183418
EXTERN char e_cannot_access_protected_variable_str[]
34193419
INIT(= N_("E1333: Cannot access protected variable \"%s\" in class \"%s\""));
34203420
// E1334 unused
@@ -3532,9 +3532,9 @@ EXTERN char e_class_method_str_accessible_only_using_class_str[]
35323532
EXTERN char e_object_method_str_accessible_only_using_object_str[]
35333533
INIT(= N_("E1386: Object method \"%s\" accessible only using class \"%s\" object"));
35343534
EXTERN char e_public_variable_not_supported_in_interface[]
3535-
INIT(= N_("E1387: Public variable not supported in an interface"));
3535+
INIT(= N_("E1387: public variable not supported in an interface"));
35363536
EXTERN char e_public_keyword_not_supported_for_method[]
3537-
INIT(= N_("E1388: Public keyword not supported for a method"));
3537+
INIT(= N_("E1388: public keyword not supported for a method"));
35383538
EXTERN char e_missing_name_after_implements[]
35393539
INIT(= N_("E1389: Missing name after implements"));
35403540
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[]
36153615
INIT(= N_("E1427: Enum \"%s\" name cannot be modified"));
36163616
EXTERN char e_duplicate_enum_str[]
36173617
INIT(= N_("E1428: Duplicate enum value: %s"));
3618+
EXTERN char e_class_can_only_be_used_in_script[]
3619+
INIT(= N_("E1429: Class can only be used in a script"));
36183620
#endif
36193621
// E1429 - E1499 unused (reserved for Vim9 class support)
36203622
EXTERN char e_cannot_mix_positional_and_non_positional_str[]

src/testdir/test_vim9_class.vim

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ def Test_class_def_method()
400400
enddef
401401
endclass
402402
END
403-
v9.CheckSourceFailure(lines, 'E1331: Public must be followed by "var" or "static"', 3)
403+
v9.CheckSourceFailure(lines, 'E1388: public keyword not supported for a method', 3)
404404

405405
# Using the "public" keyword when defining a class method
406406
lines =<< trim END
@@ -410,7 +410,7 @@ def Test_class_def_method()
410410
enddef
411411
endclass
412412
END
413-
v9.CheckSourceFailure(lines, 'E1388: Public keyword not supported for a method', 3)
413+
v9.CheckSourceFailure(lines, 'E1388: public keyword not supported for a method', 3)
414414

415415
# Using the "public" keyword when defining an object protected method
416416
lines =<< trim END
@@ -420,7 +420,7 @@ def Test_class_def_method()
420420
enddef
421421
endclass
422422
END
423-
v9.CheckSourceFailure(lines, 'E1331: Public must be followed by "var" or "static"', 3)
423+
v9.CheckSourceFailure(lines, 'E1388: public keyword not supported for a method', 3)
424424

425425
# Using the "public" keyword when defining a class protected method
426426
lines =<< trim END
@@ -430,7 +430,7 @@ def Test_class_def_method()
430430
enddef
431431
endclass
432432
END
433-
v9.CheckSourceFailure(lines, 'E1388: Public keyword not supported for a method', 3)
433+
v9.CheckSourceFailure(lines, 'E1388: public keyword not supported for a method', 3)
434434

435435
# Using a "def" keyword without an object method name
436436
lines =<< trim END
@@ -1191,7 +1191,7 @@ def Test_instance_variable_access()
11911191
public var _val = 10
11921192
endclass
11931193
END
1194-
v9.CheckSourceFailure(lines, 'E1332: Public variable name cannot start with underscore: public var _val = 10', 3)
1194+
v9.CheckSourceFailure(lines, 'E1332: public variable name cannot start with underscore: public var _val = 10', 3)
11951195

11961196
lines =<< trim END
11971197
vim9script
@@ -1287,7 +1287,7 @@ def Test_instance_variable_access()
12871287
public val = 1
12881288
endclass
12891289
END
1290-
v9.CheckSourceFailure(lines, 'E1331: Public must be followed by "var" or "static"', 3)
1290+
v9.CheckSourceFailure(lines, 'E1331: public must be followed by "var" or "static"', 3)
12911291

12921292
# Modify a instance variable using the class name in the script context
12931293
lines =<< trim END
@@ -6537,15 +6537,15 @@ def Test_interface_with_unsupported_members()
65376537
public static var num: number
65386538
endinterface
65396539
END
6540-
v9.CheckSourceFailure(lines, 'E1387: Public variable not supported in an interface', 3)
6540+
v9.CheckSourceFailure(lines, 'E1387: public variable not supported in an interface', 3)
65416541

65426542
lines =<< trim END
65436543
vim9script
65446544
interface A
65456545
public static var num: number
65466546
endinterface
65476547
END
6548-
v9.CheckSourceFailure(lines, 'E1387: Public variable not supported in an interface', 3)
6548+
v9.CheckSourceFailure(lines, 'E1387: public variable not supported in an interface', 3)
65496549

65506550
lines =<< trim END
65516551
vim9script
@@ -10625,4 +10625,17 @@ def Test_abstract_method_defcompile()
1062510625
v9.CheckScriptFailure(lines, 'E476: Invalid command: pass', 1)
1062610626
enddef
1062710627

10628+
" Test for defining a class in a function
10629+
def Test_class_definition_in_a_function()
10630+
var lines =<< trim END
10631+
vim9script
10632+
def Foo()
10633+
class A
10634+
endclass
10635+
enddef
10636+
defcompile
10637+
END
10638+
v9.CheckScriptFailure(lines, 'E1429: Class can only be used in a script', 1)
10639+
enddef
10640+
1062810641
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ static char *(features[]) =
704704

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
314,
707709
/**/
708710
313,
709711
/**/

src/vim9class.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2080,6 +2080,12 @@ ex_class(exarg_T *eap)
20802080
has_public = TRUE;
20812081
p = skipwhite(line + 6);
20822082

2083+
if (STRNCMP(p, "def", 3) == 0)
2084+
{
2085+
emsg(_(e_public_keyword_not_supported_for_method));
2086+
break;
2087+
}
2088+
20832089
if (STRNCMP(p, "var", 3) != 0 && STRNCMP(p, "static", 6) != 0
20842090
&& STRNCMP(p, "final", 5) != 0 && STRNCMP(p, "const", 5) != 0)
20852091
{

src/vim9compile.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4028,10 +4028,13 @@ compile_def_function(
40284028
line = (char_u *)"";
40294029
break;
40304030

4031+
case CMD_class:
4032+
emsg(_(e_class_can_only_be_used_in_script));
4033+
goto erret;
4034+
40314035
case CMD_type:
40324036
emsg(_(e_type_can_only_be_used_in_script));
40334037
goto erret;
4034-
break;
40354038

40364039
case CMD_global:
40374040
if (check_global_and_subst(ea.cmd, p) == FAIL)

0 commit comments

Comments
 (0)