0
0
mirror of https://github.com/vim/vim.git synced 2025-09-23 03:43:49 -04:00

patch 9.0.1898: Vim9: restrict access to static vars

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>
This commit is contained in:
Yegappan Lakshmanan
2023-09-15 20:14:55 +02:00
committed by Christian Brabandt
parent 35928ee8f8
commit c30a90d9b2
11 changed files with 1249 additions and 487 deletions

View File

@@ -332,24 +332,42 @@ script_var_exists(char_u *name, size_t len, cctx_T *cctx, cstack_T *cstack)
}
/*
* If "name[len]" is a class method in cctx->ctx_ufunc->uf_class return the
* class method index.
* Returns the index of a class method or class variable with name "name"
* accessible in the currently compiled function.
* If "cl_ret" is not NULL set it to the class.
* Otherwise return -1.
*/
int
cctx_class_method_idx(
static int
cctx_class_midx(
cctx_T *cctx,
int is_method,
char_u *name,
size_t len,
class_T **cl_ret)
{
if (cctx == NULL || cctx->ctx_ufunc == NULL
|| cctx->ctx_ufunc->uf_class == NULL)
|| cctx->ctx_ufunc->uf_class == NULL
|| cctx->ctx_ufunc->uf_defclass == NULL)
return -1;
class_T *cl = cctx->ctx_ufunc->uf_class;
int m_idx = class_method_idx(cl, name, len);
// Search for the class method or variable in the class where the calling
// function is defined.
class_T *cl = cctx->ctx_ufunc->uf_defclass;
int m_idx = is_method ? class_method_idx(cl, name, len)
: class_member_idx(cl, name, len);
if (m_idx < 0)
{
cl = cl->class_extends;
while (cl != NULL)
{
m_idx = is_method ? class_method_idx(cl, name, len)
: class_member_idx(cl, name, len);
if (m_idx >= 0)
break;
cl = cl->class_extends;
}
}
if (m_idx >= 0)
{
if (cl_ret != NULL)
@@ -360,10 +378,24 @@ cctx_class_method_idx(
}
/*
* If "name[len]" is a class member in cctx->ctx_ufunc->uf_class return the
* class member variable index.
* If "cl_ret" is not NULL set it to the class.
* Otherwise return -1;
* Returns the index of a class method with name "name" accessible in the
* currently compiled function. Returns -1 if not found. The class where the
* method is defined is returned in "cl_ret".
*/
int
cctx_class_method_idx(
cctx_T *cctx,
char_u *name,
size_t len,
class_T **cl_ret)
{
return cctx_class_midx(cctx, TRUE, name, len, cl_ret);
}
/*
* Returns the index of a class variable with name "name" accessible in the
* currently compiled function. Returns -1 if not found. The class where the
* variable is defined is returned in "cl_ret".
*/
int
cctx_class_member_idx(
@@ -372,19 +404,7 @@ cctx_class_member_idx(
size_t len,
class_T **cl_ret)
{
if (cctx == NULL || cctx->ctx_ufunc == NULL
|| cctx->ctx_ufunc->uf_class == NULL)
return -1;
class_T *cl = cctx->ctx_ufunc->uf_class;
int m_idx = class_member_idx(cl, name, len);
if (m_idx >= 0)
{
if (cl_ret != NULL)
*cl_ret = cl;
}
return m_idx;
return cctx_class_midx(cctx, FALSE, name, len, cl_ret);
}
/*
@@ -1639,6 +1659,8 @@ compile_lhs(
}
else
{
class_T *defcl;
// No specific kind of variable recognized, just a name.
if (check_reserved_name(lhs->lhs_name, lhs->lhs_has_index
&& *var_end == '.') == FAIL)
@@ -1681,8 +1703,16 @@ compile_lhs(
}
}
else if ((lhs->lhs_classmember_idx = cctx_class_member_idx(
cctx, var_start, lhs->lhs_varlen, NULL)) >= 0)
cctx, var_start, lhs->lhs_varlen, &defcl)) >= 0)
{
if (cctx->ctx_ufunc->uf_defclass != defcl)
{
// A class variable can be accessed without the class name
// only inside a class.
semsg(_(e_class_member_str_accessible_only_inside_class_str),
lhs->lhs_name, defcl->class_name);
return FAIL;
}
if (is_decl)
{
semsg(_(e_variable_already_declared_in_class_str),
@@ -1918,10 +1948,10 @@ compile_lhs(
int use_class = lhs->lhs_type != NULL
&& (lhs->lhs_type->tt_type == VAR_CLASS
|| lhs->lhs_type->tt_type == VAR_OBJECT);
|| lhs->lhs_type->tt_type == VAR_OBJECT);
if (lhs->lhs_type == NULL
|| (use_class ? lhs->lhs_type->tt_class == NULL
: lhs->lhs_type->tt_member == NULL))
: lhs->lhs_type->tt_member == NULL))
{
lhs->lhs_member_type = &t_any;
}
@@ -1930,9 +1960,10 @@ compile_lhs(
// for an object or class member get the type of the member
class_T *cl = lhs->lhs_type->tt_class;
ocmember_T *m;
int is_object = lhs->lhs_type->tt_type == VAR_OBJECT;
lhs->lhs_member_type = class_member_type(cl,
lhs->lhs_type->tt_type == VAR_OBJECT,
is_object,
after + 1, lhs->lhs_end,
&lhs->lhs_member_idx, &m);
if (lhs->lhs_member_idx < 0)
@@ -1946,7 +1977,11 @@ compile_lhs(
}
// If it is private member variable, then accessing it outside the
// class is not allowed.
if ((m->ocm_access != VIM_ACCESS_ALL) && !inside_class(cctx, cl))
// If it is a read only class variable, then it can be modified
// only inside the class where it is defined.
if ((m->ocm_access != VIM_ACCESS_ALL) &&
((is_object && !inside_class(cctx, cl))
|| (!is_object && cctx->ctx_ufunc->uf_class != cl)))
{
char *msg = (m->ocm_access == VIM_ACCESS_PRIVATE)
? e_cannot_access_private_member_str