patch 9.0.1829: Vim9 missing access-checks for private vars
Problem: Vim9 missing access-checks for private vars
Solution: Use the proper check for private/readonly variable. Access
level for a member cannot be changed in a class implementing an
interface. Update the code indentation
closes: #12978
Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Co-authored-by: Ernie Rael <errael@raelity.com>
diff --git a/src/testdir/test_vim9_class.vim b/src/testdir/test_vim9_class.vim
index ad0be5b..98db71d 100644
--- a/src/testdir/test_vim9_class.vim
+++ b/src/testdir/test_vim9_class.vim
@@ -490,7 +490,7 @@
vim9script
class Foo
- this.x: number
+ public this.x: number
def Add(n: number)
this.x += n
@@ -2593,15 +2593,15 @@
vim9script
class A
- this.val1: number = 0
+ public this.val1: number = 0
endclass
class B extends A
- this.val2: number = 0
+ public this.val2: number = 0
endclass
class C extends B
- this.val3: number = 0
+ public this.val3: number = 0
endclass
def A_members(a: A)
@@ -3672,18 +3672,60 @@
END
v9.CheckScriptFailure(lines, 'E1333: Cannot access private member: _val')
- # private class member variable
+ # access a non-existing private object member variable
lines =<< trim END
vim9script
class A
- static _val: number = 10
+ this._val = 10
endclass
def T()
- A._val = 20
+ var a = A.new()
+ a._a = 1
enddef
T()
END
- v9.CheckScriptFailure(lines, 'E1333: Cannot access private member: _val')
+ v9.CheckScriptFailure(lines, 'E1089: Unknown variable: _a = 1')
+enddef
+
+" Test for changing the member access of an interface in a implementation class
+def Test_change_interface_member_access()
+ var lines =<< trim END
+ vim9script
+ interface A
+ public this.val: number
+ endinterface
+ class B implements A
+ this.val = 10
+ endclass
+ END
+ v9.CheckScriptFailure(lines, 'E1367: Access level of member "val" of interface "A" is different')
+
+ lines =<< trim END
+ vim9script
+ interface A
+ this.val: number
+ endinterface
+ class B implements A
+ public this.val = 10
+ endclass
+ END
+ v9.CheckScriptFailure(lines, 'E1367: Access level of member "val" of interface "A" is different')
+enddef
+
+" Test for trying to change a readonly member from a def function
+def Test_readonly_member_change_in_def_func()
+ var lines =<< trim END
+ vim9script
+ class A
+ this.val: number
+ endclass
+ def T()
+ var a = A.new()
+ a.val = 20
+ enddef
+ T()
+ END
+ v9.CheckScriptFailure(lines, 'E46: Cannot change read-only variable "val"')
enddef
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker