diff options
author | Yegappan Lakshmanan <yegappan@yahoo.com> | 2023-09-15 20:14:55 +0200 |
---|---|---|
committer | Christian Brabandt <cb@256bit.org> | 2023-09-15 20:14:55 +0200 |
commit | c30a90d9b2c029f794cea502f6b824f71e4876dd (patch) | |
tree | 53c789bae67beb6fe099686fcc6894b2c6fdb7b8 /src | |
parent | 35928ee8f80ea721e92bb856c8ecde2cced46bb9 (diff) |
patch 9.0.1898: Vim9: restrict access to static varsv9.0.1898
Problem: Vim9: restrict access to static vars and methods
Solution: Class members are accesible only from the class where they are
defined.
Based on the #13004 discussion, the following changes are made:
1) Static variables and methods are accessible only using the class
name and inside the class where they are defined.
2) Static variables and methods can be used without the class name in
the class where they are defined.
3) Static variables of a super class are not copied to the sub class.
4) A sub class can declare a class variable with the same name as the
super class.
5) When a method or member is found during compilation, use more
specific error messages.
This aligns the Vim9 class variable/method implementation with the Dart
implementation.
Also while at it, ignore duplicate class and object methods.
The access level of an object method can however be changed in a
subclass.
For the tests, use the new CheckSourceFailure() function instead of the
CheckScriptFailure() function in the tests.
closes: #13086
Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/errors.h | 19 | ||||
-rw-r--r-- | src/eval.c | 5 | ||||
-rw-r--r-- | src/proto/vim9class.pro | 2 | ||||
-rw-r--r-- | src/structs.h | 7 | ||||
-rw-r--r-- | src/testdir/test_vim9_class.vim | 1222 | ||||
-rw-r--r-- | src/testdir/vim9.vim | 34 | ||||
-rw-r--r-- | src/version.c | 2 | ||||
-rw-r--r-- | src/vim9class.c | 280 | ||||
-rw-r--r-- | src/vim9compile.c | 93 | ||||
-rw-r--r-- | src/vim9execute.c | 6 | ||||
-rw-r--r-- | src/vim9expr.c | 58 |
11 files changed, 1245 insertions, 483 deletions
diff --git a/src/errors.h b/src/errors.h index 0d53fa84cd..6184037df8 100644 --- a/src/errors.h +++ b/src/errors.h @@ -3411,16 +3411,14 @@ EXTERN char e_public_member_name_cannot_start_with_underscore_str[] INIT(= N_("E1332: Public member name cannot start with underscore: %s")); EXTERN char e_cannot_access_private_member_str[] INIT(= N_("E1333: Cannot access private member: %s")); -EXTERN char e_object_member_not_found_str[] - INIT(= N_("E1334: Object member not found: %s")); EXTERN char e_member_is_not_writable_str[] INIT(= N_("E1335: Member is not writable: %s")); #endif EXTERN char e_internal_error_shortmess_too_long[] INIT(= "E1336: Internal error: shortmess too long"); #ifdef FEAT_EVAL -EXTERN char e_class_member_not_found_str[] - INIT(= N_("E1337: Class member not found: %s")); +EXTERN char e_class_member_str_not_found_in_class_str[] + INIT(= N_("E1337: Class member \"%s\" not found in class \"%s\"")); EXTERN char e_member_not_found_on_class_str_str[] INIT(= N_("E1338: Member not found on class \"%s\": %s")); #endif @@ -3488,7 +3486,6 @@ EXTERN char e_cannot_access_private_method_str[] INIT(= N_("E1366: Cannot access private method: %s")); EXTERN char e_member_str_of_interface_str_has_different_access[] INIT(= N_("E1367: Access level of member \"%s\" of interface \"%s\" is different")); - EXTERN char e_static_cannot_be_followed_by_this[] INIT(= N_("E1368: Static cannot be followed by \"this\" in a member name")); EXTERN char e_duplicate_member_str[] @@ -3501,6 +3498,16 @@ EXTERN char e_abstract_method_in_concrete_class[] INIT(= N_("E1372: Abstract method \"%s\" cannot be defined in a concrete class")); EXTERN char e_abstract_method_str_not_found[] INIT(= N_("E1373: Abstract method \"%s\" is not implemented")); +EXTERN char e_class_member_str_accessible_only_inside_class_str[] + INIT(= N_("E1374: Class member \"%s\" accessible only inside class \"%s\"")); +EXTERN char e_class_member_str_accessible_only_using_class_str[] + INIT(= N_("E1375: Class member \"%s\" accessible only using class \"%s\"")); +EXTERN char e_object_member_str_accessible_only_using_object_str[] + INIT(= N_("E1376: Object member \"%s\" accessible only using class \"%s\" object")); +EXTERN char e_static_member_not_supported_in_interface[] + INIT(= N_("E1377: Static member is not supported in an interface")); +EXTERN char e_method_str_of_class_str_has_different_access[] + INIT(= N_("E1378: Access level of method \"%s\" is different in class \"%s\"")); EXTERN char e_cannot_mix_positional_and_non_positional_str[] INIT(= N_("E1400: Cannot mix positional and non-positional arguments: %s")); EXTERN char e_fmt_arg_nr_unused_str[] @@ -3521,4 +3528,4 @@ EXTERN char e_aptypes_is_null_nr_str[] INIT(= "E1408: Internal error: ap_types or ap_types[idx] is NULL: %d: %s"); EXTERN char e_interface_static_direct_access_str[] INIT(= N_("E1409: Cannot directly access interface \"%s\" static member \"%s\"")); -// E1371 - E1399 unused +// E1376 - E1399 unused diff --git a/src/eval.c b/src/eval.c index dd7b8d8f33..523546957e 100644 --- a/src/eval.c +++ b/src/eval.c @@ -1596,10 +1596,7 @@ get_lval( if (lp->ll_valtype == NULL) { - if (v_type == VAR_OBJECT) - semsg(_(e_object_member_not_found_str), key); - else - semsg(_(e_class_member_not_found_str), key); + member_not_found_msg(cl, v_type, key, p - key); return NULL; } } diff --git a/src/proto/vim9class.pro b/src/proto/vim9class.pro index a61c40f239..1448f19159 100644 --- a/src/proto/vim9class.pro +++ b/src/proto/vim9class.pro @@ -26,6 +26,8 @@ int set_ref_in_classes(int copyID); void object_created(object_T *obj); void object_cleared(object_T *obj); int object_free_nonref(int copyID); +void method_not_found_msg(class_T *cl, vartype_T v_type, char_u *name, size_t len); +void member_not_found_msg(class_T *cl, vartype_T v_type, char_u *name, size_t len); void f_instanceof(typval_T *argvars, typval_T *rettv); int class_instance_of(class_T *cl, class_T *other_cl); /* vim: set ft=c : */ diff --git a/src/structs.h b/src/structs.h index 9214096973..4a82c82e6d 100644 --- a/src/structs.h +++ b/src/structs.h @@ -1791,8 +1791,11 @@ struct ufunc_S def_status_T uf_def_status; // UF_NOT_COMPILED, UF_TO_BE_COMPILED, etc. int uf_dfunc_idx; // only valid if uf_def_status is UF_COMPILED - class_T *uf_class; // for object method and constructor; does not - // count for class_refcount + class_T *uf_class; // for class/object method and constructor; + // does not count for class_refcount. + // class of the object which is invoking this + // function. + class_T *uf_defclass; // class where this function is defined. garray_T uf_args; // arguments, including optional arguments garray_T uf_def_args; // default argument expressions diff --git a/src/testdir/test_vim9_class.vim b/src/testdir/test_vim9_class.vim index be4eb2eb8a..1796f5f72e 100644 --- a/src/testdir/test_vim9_class.vim +++ b/src/testdir/test_vim9_class.vim @@ -8,56 +8,56 @@ def Test_class_basic() class NotWorking endclass END - v9.CheckScriptFailure(lines, 'E1316:') + v9.CheckSourceFailure(lines, 'E1316:') lines =<< trim END vim9script class notWorking endclass END - v9.CheckScriptFailure(lines, 'E1314:') + v9.CheckSourceFailure(lines, 'E1314:') lines =<< trim END vim9script class Not@working endclass END - v9.CheckScriptFailure(lines, 'E1315:') + v9.CheckSourceFailure(lines, 'E1315:') lines =<< trim END vim9script abstract noclass Something endclass END - v9.CheckScriptFailure(lines, 'E475:') + v9.CheckSourceFailure(lines, 'E475:') lines =<< trim END vim9script abstract classy Something endclass END - v9.CheckScriptFailure(lines, 'E475:') + v9.CheckSourceFailure(lines, 'E475:') lines =<< trim END vim9script class Something endcl END - v9.CheckScriptFailure(lines, 'E1065:') + v9.CheckSourceFailure(lines, 'E1065:') lines =<< trim END vim9script class Something endclass school's out END - v9.CheckScriptFailure(lines, 'E488:') + v9.CheckSourceFailure(lines, 'E488:') lines =<< trim END vim9script class Something endclass | echo 'done' END - v9.CheckScriptFailure(lines, 'E488:') + v9.CheckSourceFailure(lines, 'E488:') lines =<< trim END vim9script @@ -65,7 +65,7 @@ def Test_class_basic() this endclass END - v9.CheckScriptFailure(lines, 'E1317:') + v9.CheckSourceFailure(lines, 'E1317:') lines =<< trim END vim9script @@ -73,7 +73,7 @@ def Test_class_basic() this. endclass END - v9.CheckScriptFailure(lines, 'E1317:') + v9.CheckSourceFailure(lines, 'E1317:') lines =<< trim END vim9script @@ -81,7 +81,7 @@ def Test_class_basic() this .count endclass END - v9.CheckScriptFailure(lines, 'E1317:') + v9.CheckSourceFailure(lines, 'E1317:') lines =<< trim END vim9script @@ -89,7 +89,7 @@ def Test_class_basic() this. count endclass END - v9.CheckScriptFailure(lines, 'E1317:') + v9.CheckSourceFailure(lines, 'E1317:') lines =<< trim END vim9script @@ -98,7 +98,7 @@ def Test_class_basic() that.count endclass END - v9.CheckScriptFailure(lines, 'E1318: Not a valid command in a class: that.count') + v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: that.count') lines =<< trim END vim9script @@ -106,7 +106,7 @@ def Test_class_basic() this.count endclass END - v9.CheckScriptFailure(lines, 'E1022:') + v9.CheckSourceFailure(lines, 'E1022:') lines =<< trim END vim9script @@ -117,7 +117,7 @@ def Test_class_basic() endclass var obj = Something.new() END - v9.CheckScriptFailure(lines, 'E1089:') + v9.CheckSourceFailure(lines, 'E1089:') lines =<< trim END vim9script @@ -125,7 +125,7 @@ def Test_class_basic() this.count : number endclass END - v9.CheckScriptFailure(lines, 'E1059:') + v9.CheckSourceFailure(lines, 'E1059:') lines =<< trim END vim9script @@ -133,7 +133,7 @@ def Test_class_basic() this.count:number endclass END - v9.CheckScriptFailure(lines, 'E1069:') + v9.CheckSourceFailure(lines, 'E1069:') # Test for unsupported comment specifier lines =<< trim END @@ -143,7 +143,7 @@ def Test_class_basic() #{ endclass END - v9.CheckScriptFailure(lines, 'E1170:') + v9.CheckSourceFailure(lines, 'E1170:') lines =<< trim END vim9script @@ -171,7 +171,7 @@ def Test_class_basic() assert_equal('class<TextPosition>', typename(TextPosition)) assert_equal('object<TextPosition>', typename(pos)) END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) # When referencing object methods, space cannot be used after a "." lines =<< trim END @@ -184,7 +184,7 @@ def Test_class_basic() var a = A.new() var v = a. Foo() END - v9.CheckScriptFailure(lines, 'E1202:') + v9.CheckSourceFailure(lines, 'E1202:') # Using an object without specifying a method or a member variable lines =<< trim END @@ -197,7 +197,7 @@ def Test_class_basic() var a = A.new() var v = a. END - v9.CheckScriptFailure(lines, 'E15:') + v9.CheckSourceFailure(lines, 'E15:') # Error when parsing the arguments of an object method. lines =<< trim END @@ -209,7 +209,7 @@ def Test_class_basic() var a = A.new() var v = a.Foo(,) END - v9.CheckScriptFailure(lines, 'E15:') + v9.CheckSourceFailure(lines, 'E15:') lines =<< trim END vim9script @@ -220,7 +220,7 @@ def Test_class_basic() endclass var a = A.new() END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) enddef def Test_class_defined_twice() @@ -232,7 +232,7 @@ def Test_class_defined_twice() class There endclass END - v9.CheckScriptFailure(lines, 'E1041: Redefining script item: "There"') + v9.CheckSourceFailure(lines, 'E1041: Redefining script item: "There"') # one class, reload same script twice is OK lines =<< trim END @@ -259,7 +259,7 @@ def Test_returning_null_object() var buffers = BufferList.new() echo buffers.Current() END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) enddef def Test_using_null_class() @@ -276,7 +276,7 @@ def Test_class_interface_wrong_end() this.member = 'text' endinterface END - v9.CheckScriptFailure(lines, 'E476: Invalid command: endinterface, expected endclass') + v9.CheckSourceFailure(lines, 'E476: Invalid command: endinterface, expected endclass') lines =<< trim END vim9script @@ -284,7 +284,7 @@ def Test_class_interface_wrong_end() this.member: string endclass END - v9.CheckScriptFailure(lines, 'E476: Invalid command: endclass, expected endinterface') + v9.CheckSourceFailure(lines, 'E476: Invalid command: endclass, expected endinterface') enddef def Test_object_not_set() @@ -299,7 +299,7 @@ def Test_object_not_set() var db = {'xyz': 789} echo db[state.value] END - v9.CheckScriptFailure(lines, 'E1360:') + v9.CheckSourceFailure(lines, 'E1360:') lines =<< trim END vim9script @@ -317,7 +317,7 @@ def Test_object_not_set() enddef Func() END - v9.CheckScriptFailure(lines, 'E1360:') + v9.CheckSourceFailure(lines, 'E1360:') lines =<< trim END vim9script @@ -337,7 +337,7 @@ def Test_object_not_set() var bg: Background # UNINITIALIZED echo Colorscheme.new(bg).GetBackground() END - v9.CheckScriptFailure(lines, 'E1360:') + v9.CheckSourceFailure(lines, 'E1360:') # TODO: this should not give an error but be handled at runtime lines =<< trim END @@ -356,7 +356,7 @@ def Test_object_not_set() enddef Func() END - v9.CheckScriptFailure(lines, 'E1363:') + v9.CheckSourceFailure(lines, 'E1363:') enddef def Test_null_object_assign_compare() @@ -396,7 +396,7 @@ def Test_null_object_assign_compare() o2 = null_object assert_true(o2 == null) END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) enddef def Test_class_member_initializer() @@ -432,7 +432,7 @@ def Test_class_member_initializer() '\d\+ RETURN object.*', instr) END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) enddef def Test_member_any_used_as_object() @@ -456,7 +456,7 @@ def Test_member_any_used_as_object() F(outer_obj) assert_equal(1, inner_obj.value) END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) lines =<< trim END vim9script @@ -482,7 +482,51 @@ def Test_member_any_used_as_object() Test_assign_to_nested_typed_member() END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) + + # Try modifying a private variable using an "any" object + lines =<< trim END + vim9script + + class Inner + this._value: string = '' + endclass + + class Outer + this.inner: any + endclass + + def F(outer: Outer) + outer.inner._value = 'b' + enddef + + var inner_obj = Inner.new('a') + var outer_obj = Outer.new(inner_obj) + F(outer_obj) + END + v9.CheckSourceFailure(lines, 'E1333: Cannot access private member: _value') + + # Try modifying a non-existing variable using an "any" object + lines =<< trim END + vim9script + + class Inner + this.value: string = '' + endclass + + class Outer + this.inner: any + endclass + + def F(outer: Outer) + outer.inner.someval = 'b' + enddef + + var inner_obj = Inner.new('a') + var outer_obj = Outer.new(inner_obj) + F(outer_obj) + END + v9.CheckSourceFailure(lines, 'E1326: Member not found on object "Inner": someval') enddef def Test_assignment_with_operator() @@ -508,7 +552,7 @@ def Test_assignment_with_operator() AddToFoo(f) assert_equal(23, f.x) END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) # do the same thing, but through an interface lines =<< trim END @@ -538,7 +582,7 @@ def Test_assignment_with_operator() AddToFoo(f) assert_equal(23, f.x) END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) enddef def Test_list_of_objects() @@ -559,7 +603,7 @@ def Test_list_of_objects() var l: list<Foo> = [Foo.new()] ProcessList(l) END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) enddef def Test_expr_after_using_object() @@ -578,7 +622,7 @@ def Test_expr_after_using_object() Foo() END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) enddef def Test_class_default_new() @@ -606,7 +650,7 @@ def Test_class_default_new() assert_equal(1, pos.lnum) assert_equal(33, pos.col) END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) lines =<< trim END vim9script @@ -629,7 +673,7 @@ def Test_class_default_new() assert_equal(4, chris.age) assert_equal("none", chris.education) END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) lines =<< trim END vim9script @@ -644,7 +688,7 @@ def Test_class_default_new() var missing = Person.new() END - v9.CheckScriptFailure(lines, 'E119:') + v9.CheckSourceFailure(lines, 'E119:') enddef @@ -677,7 +721,7 @@ def Test_class_new_with_object_member() Check() END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) lines =<< trim END vim9script @@ -699,7 +743,7 @@ def Test_class_new_with_object_member() Check() END - v9.CheckScriptFailure(lines, 'E1013:') + v9.CheckSourceFailure(lines, 'E1013:') lines =<< trim END vim9script @@ -721,7 +765,7 @@ def Test_class_new_with_object_member() Check() END - v9.CheckScriptFailure(lines, 'E1013:') + v9.CheckSourceFailure(lines, 'E1013:') enddef def Test_class_object_member_inits() @@ -738,7 +782,7 @@ def Test_class_object_member_inits() assert_equal(1, pos.col) assert_equal(2, pos.addcol) END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) lines =<< trim END vim9script @@ -747,7 +791,7 @@ def Test_class_object_member_inits() this.col = 1 endclass END - v9.CheckScriptFailure(lines, 'E1022:') + v9.CheckSourceFailure(lines, 'E1022:') # If the type is not specified for a member, then it should be set during # object creation and not when defining the class. @@ -770,7 +814,7 @@ def Test_class_object_member_inits() var a = A.new() assert_equal(init_count, 2) END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) # Test for initializing an object member with an unknown variable/type lines =<< trim END @@ -780,10 +824,11 @@ def Test_class_object_member_inits() endclass var a = A.new() END - v9.CheckScriptFailure(lines, 'E1001:') + v9.CheckSourceFailure(lines, 'E1001:') enddef -def Test_class_object_member_access() +" Test for instance variable access +def Test_instance_variable_access() var lines =<< trim END vim9script class Triple @@ -807,9 +852,9 @@ def Test_class_object_member_access() trip.three = 33 assert_equal(33, trip.three) - assert_fails('trip.four = 4', 'E1334') + assert_fails('trip.four = 4', 'E1326') END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) # Test for a public member variable name beginning with an underscore lines =<< trim END @@ -818,7 +863,7 @@ def Test_class_object_member_access() public this._val = 10 endclass END - v9.CheckScriptFailure(lines, 'E1332:') + v9.CheckSourceFailure(lines, 'E1332:') lines =<< trim END vim9script @@ -854,7 +899,7 @@ def Test_class_object_member_access() enddef CheckCar() END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) lines =<< trim END vim9script @@ -870,7 +915,7 @@ def Test_class_object_member_access() var c = MyCar.new("abc") var c = MyCar.new("def") END - v9.CheckScriptFailure(lines, 'E1041:') + v9.CheckSourceFailure(lines, 'E1041:') lines =<< trim END vim9script @@ -896,7 +941,7 @@ def Test_class_object_member_access() .Add(2) .x END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) # Test for "public" cannot be abbreviated lines =<< trim END @@ -905,7 +950,7 @@ def Test_class_object_member_access() pub this.val = 1 endclass END - v9.CheckScriptFailure(lines, 'E1065:') + v9.CheckSourceFailure(lines, 'E1065:') # Test for "public" keyword must be followed by "this" or "static". lines =<< trim END @@ -914,25 +959,53 @@ def Test_class_object_member_access() public val = 1 endclass END - v9.CheckScriptFailure(lines, 'E1331:') + v9.CheckSourceFailure(lines, 'E1331:') - # Test for "static" cannot be abbreviated + # Modify a instance variable using the class name in the script context lines =<< trim END vim9script - class Something - stat this.val = 1 + class A + public this.val = 1 endclass + A.val = 1 END - v9.CheckScriptFailure(lines, 'E1065:') + v9.CheckSourceFailure(lines, 'E1376: Object member "val" accessible only using class "A" object') - # Test for "static" cannot be followed by "this". + # Read a instance variable using the class name in the script context lines =<< trim END vim9script - class Something - static this.val = 1 + class A + public this.val = 1 + endclass + var i = A.val + END + v9.CheckSourceFailure(lines, 'E1376: Object member "val" accessible only using class "A" object') + + # Modify a instance variable using the class name in a def function + lines =<< trim END + vim9script + class A + public this.val = 1 endclass + def T() + A.val = 1 + enddef + T() END - v9.CheckScriptFailure(lines, 'E1368: Static cannot be followed by "this" in a member name') + v9.CheckSourceFailure(lines, 'E1376: Object member "val" accessible only using class "A" object') + + # Read a instance variable using the class name in a def function + lines =<< trim END + vim9script + class A + public this.val = 1 + endclass + def T() + var i = A.val + enddef + T() + END + v9.CheckSourceFailure(lines, 'E1376: Object member "val" accessible only using class "A" object') # Access from child class extending a class: lines =<< trim END @@ -941,10 +1014,6 @@ def Test_class_object_member_access() this.ro_obj_var = 10 public this.rw_obj_var = 20 this._priv_obj_var = 30 - - static ro_class_var = 40 - public static rw_class_var = 50 - static _priv_class_var = 60 endclass class B extends A @@ -956,27 +1025,127 @@ def Test_class_object_member_access() this.rw_obj_var = 0 x = this._priv_obj_var this._priv_obj_var = 0 + enddef + endclass + + var b = B.new() + b.Foo() + END + v9.CheckSourceSuccess(lines) +enddef + +" Test for class variable access +def Test_class_variable_access() + # Test for "static" cannot be abbreviated + var lines =<< trim END + vim9script + class Something + stat this.val = 1 + endclass + END + v9.CheckSourceFailure(lines, 'E1065:') + + # Test for "static" cannot be followed by "this". + lines =<< trim END + vim9script + class Something + static this.val = 1 + endclass + END + v9.CheckSourceFailure(lines, 'E1368: Static cannot be followed by "this" in a member name') + + # Test for "static" cannot be followed by "public". + lines =<< trim END + vim9script + class Something + static public val = 1 + endclass + END + v9.CheckSourceFailure(lines, 'E1022: Type or initialization required') + + # A readonly class variable cannot be modified from a child class + lines =<< trim END + vim9script + class A + static ro_class_var = 40 + endclass + + class B extends A + def Foo() + A.ro_class_var = 50 + enddef + endclass + + var b = B.new() + b.Foo() + END + v9.CheckSourceFailure(lines, 'E46: Cannot change read-only variable "ro_class_var"') + + # A private class variable cannot be accessed from a child class + lines =<< trim END + vim9script + class A + static _priv_class_var = 60 + endclass + + class B extends A + def Foo() + var i = A._priv_class_var + enddef + endclass + + var b = B.new() + b.Foo() + END + v9.CheckSourceFailure(lines, 'E1333: Cannot access private member: _priv_class_var') + + # A private class variable cannot be modified from a child class + lines =<< trim END + vim9script + class A + static _priv_class_var = 60 + endclass + + class B extends A + def Foo() + A._priv_class_var = 0 + enddef + endclass - x = ro_class_var - ro_class_var = 0 - x = rw_class_var - rw_class_var = 0 - x = _priv_class_var - _priv_class_var = 0 + var b = B.new() + b.Foo() + END + v9.CheckSourceFailure(lines, 'E1333: Cannot access private member: _priv_class_var') + + # Access from child class extending a class and from script context + lines =<< trim END + vim9script + class A + static ro_class_var = 10 + public static rw_class_var = 20 + static _priv_class_var = 30 + endclass + class B extends A + def Foo() + var x: number x = A.ro_class_var - A.ro_class_var = 0 + assert_equal(10, x) x = A.rw_class_var - A.rw_class_var = 0 - x = A._priv_class_var - A._priv_class_var = 0 + assert_equal(25, x) + A.rw_class_var = 20 + assert_equal(20, A.rw_class_var) enddef endclass + assert_equal(10, A.ro_class_var) + assert_equal(20, A.rw_class_var) + A.rw_class_var = 25 + assert_equal(25, A.rw_class_var) var b = B.new() b.Foo() END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) enddef def Test_class_object_compare() @@ -1005,8 +1174,8 @@ def Test_class_object_compare() assert_notequal(i1, io2) END - v9.CheckScriptSuccess(class_lines + test_lines) - v9.CheckScriptSuccess( + v9.CheckSourceSuccess(class_lines + test_lines) + v9.CheckSourceSuccess( class_lines + ['def Test()'] + test_lines + ['enddef', 'Test()']) for op in ['>', '>=', '<', '<=', '=~', '!~'] @@ -1015,8 +1184,8 @@ def Test_class_object_compare() 'var i2 = Item.new()', 'echo i1 ' .. op .. ' i2', ] - v9.CheckScriptFailure(class_lines + op_lines, 'E1153: Invalid operation for object') - v9.CheckScriptFailure(class_lines + v9.CheckSourceFailure(class_lines + op_lines, 'E1153: Invalid operation for object') + v9.CheckSourceFailure(class_lines + ['def Test()'] + op_lines + ['enddef', 'Test()'], 'E1153: Invalid operation for object') endfor enddef @@ -1042,7 +1211,7 @@ def Test_object_type() t = m END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) lines =<< trim END vim9script @@ -1056,7 +1225,7 @@ def Test_object_type() var o: One = Two.new() END - v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object<One> but got object<Two>') + v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected object<One> but got object<Two>') lines =<< trim END vim9script @@ -1074,7 +1243,7 @@ def Test_object_type() var o: One = Two.new(5) assert_equal(5, o.GetMember()) END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) lines =<< trim END vim9script @@ -1094,7 +1263,7 @@ def Test_object_type() echo Fn(Num.new(4)) END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) enddef def Test_class_member() @@ -1116,7 +1285,7 @@ def Test_class_member() assert_equal(0, TextPos.counter) TextPos.AddToCounter(3) assert_equal(3, TextPos.counter) - assert_fails('echo TextPos.noSuchMember', 'E1338:') + assert_fails('echo TextPos.noSuchMember', 'E1337:') def GetCounter(): number return TextPos.counter @@ -1136,7 +1305,7 @@ def Test_class_member() TextPos.anybody += 5 assert_equal(17, TextPos.anybody) END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) # example in the help lines =<< trim END @@ -1155,7 +1324,7 @@ def Test_class_member() var to7 = OtherThing.new(7) assert_equal(10, OtherThing.totalSize) END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) # using static class member twice lines =<< trim END @@ -1172,7 +1341,7 @@ def Test_class_member() assert_equal('some text', HTML.MacroSubstitute('some text')) assert_equal('some text', HTML.MacroSubstitute('some text')) END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) # access private member in lambda lines =<< trim END @@ -1190,7 +1359,7 @@ def Test_class_member() var foo = Foo.new() assert_equal(5, foo.Add(5)) END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) # access private member in lambda body lines =<< trim END @@ -1211,7 +1380,7 @@ def Test_class_member() var foo = Foo.new() assert_equal(13, foo.Add(7)) END - v9.CheckScriptSuccess(lines) + v9.CheckSourceSuccess(lines) # check shadowing lines =<< trim END @@ -1227,7 +1396,7 @@ def Test_class_member() var s = Some.new() s.Method(7) END - v9.CheckScriptFailure(lines, 'E1340: Argument already declared in the class: count') + v9.CheckSourceFailure(lines, 'E1340: Argument already declared in the class: count') lines =<< trim END vim9script @@ -1243,7 +1412,7 @@ def Test_class_member() var s = Some.new() s.Method(7) END - v9.CheckScriptFailure(lines, 'E1341: Variable already declared in the class: count') + v9.CheckSourceFailure(lines, 'E1341: Variable already declared in the class: count') # Test for using an invalid type for a member variable lines =<< trim END @@ -1252,7 +1421,7 @@ def Test_class_member() this.val: xxx endclass END - v9.CheckScriptFailure(lines, 'E1010:') + v9.CheckSourceFailure(lines, 'E1010:') # Test for setting a member on a null object lines =<< trim END @@ -1267,7 +1436,7 @@ def Test_class_member() enddef F() END - v9.CheckScriptFailure(lines, 'E1360: Using a null object') + v9.CheckSourceFailure(lines, 'E1360: Using a null object') # Test for accessing a member on a null object lines =<< trim END @@ -1282,7 +1451,7 @@ def Test_class_member() enddef F() END - v9.CheckScriptFailure(lines, 'E1360: Using a null object') + v9.CheckSourceFailure(lines, 'E1360: Using a null object') # Test for setting a member on a null object, at script level lines =<< trim END @@ -1295,7 +1464,7 @@ def Test_class_member() obj.val = "" END # FIXME(in source): this should give E1360 as well! - v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object<A> but got string') + v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected object<A> but got string') # Test for accessing a member on a null object, at script level lines =<< trim END @@ -1307,7 +1476,7 @@ def Test_class_member() var obj: A ec |