diff options
author | Yegappan Lakshmanan <yegappan@yahoo.com> | 2023-08-30 16:38:26 +0200 |
---|---|---|
committer | Christian Brabandt <cb@256bit.org> | 2023-08-30 16:40:33 +0200 |
commit | 5bbcfbc4a260bc082829311086c3a1109c40f2d3 (patch) | |
tree | 61769de553e9c99eb0f3a09af1e35ecca30da1d6 /src | |
parent | 9d8ef7cc434076dfda62ca3d3101eaae52e316cd (diff) |
patch 9.0.1824: Vim9: private members may be modifiablev9.0.1824
Problem: Vim9: private members may be modifiable
Solution: prevent modification for def function
closes: #12963
Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/testdir/test_vim9_class.vim | 33 | ||||
-rw-r--r-- | src/version.c | 2 | ||||
-rw-r--r-- | src/vim9compile.c | 9 |
3 files changed, 44 insertions, 0 deletions
diff --git a/src/testdir/test_vim9_class.vim b/src/testdir/test_vim9_class.vim index 97be92d1a2..ad0be5b511 100644 --- a/src/testdir/test_vim9_class.vim +++ b/src/testdir/test_vim9_class.vim @@ -3653,4 +3653,37 @@ def Test_dup_member_variable() v9.CheckScriptFailure(lines, 'E1369: Duplicate member: val') enddef +" Test for accessing a private member outside a class in a def function +def Test_private_member_access_outside_class() + # private object member variable + var lines =<< trim END + vim9script + class A + this._val = 10 + def GetVal(): number + return this._val + enddef + endclass + def T() + var a = A.new() + a._val = 20 + enddef + T() + END + v9.CheckScriptFailure(lines, 'E1333: Cannot access private member: _val') + + # private class member variable + lines =<< trim END + vim9script + class A + static _val: number = 10 + endclass + def T() + A._val = 20 + enddef + T() + END + v9.CheckScriptFailure(lines, 'E1333: Cannot access private member: _val') +enddef + " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker diff --git a/src/version.c b/src/version.c index bb6f52a7f7..843db4c5bb 100644 --- a/src/version.c +++ b/src/version.c @@ -700,6 +700,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 1824, +/**/ 1823, /**/ 1822, diff --git a/src/vim9compile.c b/src/vim9compile.c index add9eda60b..5c173c190d 100644 --- a/src/vim9compile.c +++ b/src/vim9compile.c @@ -1866,6 +1866,15 @@ compile_lhs( { // for an object or class member get the type of the member class_T *cl = lhs->lhs_type->tt_class; + // If it is private member variable, then accessing it outside the + // class is not allowed. + if (*(after + 1) == '_' && !inside_class(cctx, cl)) + { + char_u *m_name = vim_strnsave(after + 1, lhs->lhs_end - after); + semsg(_(e_cannot_access_private_member_str), m_name); + vim_free(m_name); + return FAIL; + } lhs->lhs_member_type = class_member_type(cl, after + 1, lhs->lhs_end, &lhs->lhs_member_idx); if (lhs->lhs_member_idx < 0) |