0
0
mirror of https://github.com/vim/vim.git synced 2025-07-26 11:04:33 -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
No known key found for this signature in database
GPG Key ID: F3F92DA383FDDE09
11 changed files with 1249 additions and 487 deletions

View File

@ -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

View File

@ -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;
}
}

View File

@ -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 : */

View File

@ -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

File diff suppressed because it is too large Load Diff

View File

@ -110,6 +110,40 @@ export def CheckScriptSuccess(lines: list<string>)
endtry
enddef
# :source a list of "lines" and check whether it fails with "error"
export def CheckSourceFailure(lines: list<string>, error: string, lnum = -3)
new
setline(1, lines)
try
assert_fails('source', error, lines, lnum)
finally
bw!
endtry
enddef
# :source a list of "lines" and check whether it fails with the list of
# "errors"
export def CheckSourceFailureList(lines: list<string>, errors: list<string>, lnum = -3)
new
setline(1, lines)
try
assert_fails('source', errors, lines, lnum)
finally
bw!
endtry
enddef
# :source a list of "lines" and check whether it succeeds
export def CheckSourceSuccess(lines: list<string>)
new
setline(1, lines)
try
:source
finally
bw!
endtry
enddef
export def CheckDefAndScriptSuccess(lines: list<string>)
CheckDefSuccess(lines)
CheckScriptSuccess(['vim9script'] + lines)

View File

@ -699,6 +699,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
1898,
/**/
1897,
/**/

View File

@ -322,27 +322,75 @@ validate_extends_class(char_u *extends_name, class_T **extends_clp)
}
/*
* Check whether a class/object member variable in "classmembers_gap" /
* "objmembers_gap" is a duplicate of a member in any of the extended parent
* class lineage. Returns TRUE if there are no duplicates.
* Check method names in the parent class lineage to make sure the access is
* the same for overridden methods.
*/
static int
validate_extends_methods(
garray_T *objmethods_gap,
class_T *extends_cl)
{
class_T *super = extends_cl;
int method_count = objmethods_gap->ga_len;
ufunc_T **cl_fp = (ufunc_T **)(objmethods_gap->ga_data);
while (super != NULL)
{
int extends_method_count = super->class_obj_method_count_child;
if (extends_method_count == 0)
{
super = super->class_extends;
continue;
}
ufunc_T **extends_methods = super->class_obj_methods;
for (int i = 0; i < extends_method_count; i++)
{
char_u *pstr = extends_methods[i]->uf_name;
int extends_private = (*pstr == '_');
if (extends_private)
pstr++;
for (int j = 0; j < method_count; j++)
{
char_u *qstr = cl_fp[j]->uf_name;
int priv_method = (*qstr == '_');
if (priv_method)
qstr++;
if (STRCMP(pstr, qstr) == 0 && priv_method != extends_private)
{
// Method access is different between the super class and
// the subclass
semsg(_(e_method_str_of_class_str_has_different_access),
cl_fp[j]->uf_name, super->class_name);
return FALSE;
}
}
}
super = super->class_extends;
}
return TRUE;
}
/*
* Check whether a object member variable in "objmembers_gap" is a duplicate of
* a member in any of the extended parent class lineage. Returns TRUE if there
* are no duplicates.
*/
static int
validate_extends_members(
garray_T *classmembers_gap,
garray_T *objmembers_gap,
class_T *extends_cl)
{
for (int loop = 1; loop <= 2; ++loop)
{
// loop == 1: check class members
// loop == 2: check object members
int member_count = loop == 1 ? classmembers_gap->ga_len
: objmembers_gap->ga_len;
int member_count = objmembers_gap->ga_len;
if (member_count == 0)
continue;
ocmember_T *members = (ocmember_T *)(loop == 1
? classmembers_gap->ga_data
: objmembers_gap->ga_data);
return TRUE;
ocmember_T *members = (ocmember_T *)(objmembers_gap->ga_data);
// Validate each member variable
for (int c_i = 0; c_i < member_count; c_i++)
@ -355,14 +403,13 @@ validate_extends_members(
// Check in all the parent classes in the lineage
while (p_cl != NULL)
{
int p_member_count = loop == 1
? p_cl->class_class_member_count
: p_cl->class_obj_member_count;
int p_member_count = p_cl->class_obj_member_count;
if (p_member_count == 0)
{
p_cl = p_cl->class_extends;
continue;
ocmember_T *p_members = (loop == 1
? p_cl->class_class_members
: p_cl->class_obj_members);
}
ocmember_T *p_members = p_cl->class_obj_members;
// Compare against all the members in the parent class
for (int p_i = 0; p_i < p_member_count; p_i++)
@ -380,7 +427,6 @@ validate_extends_members(
p_cl = p_cl->class_extends;
}
}
}
return TRUE;
}
@ -391,7 +437,7 @@ validate_extends_members(
* implemented.
*/
static int
validate_extends_methods(
validate_abstract_class_methods(
garray_T *classmethods_gap,
garray_T *objmethods_gap,
class_T *extends_cl)
@ -707,10 +753,17 @@ is_duplicate_member(garray_T *mgap, char_u *varname, char_u *varname_end)
* Returns TRUE if the method "name" is already defined.
*/
static int
is_duplicate_method(garray_T *fgap, char_u *name)
is_duplicate_method(
garray_T *classmethods_gap,
garray_T *objmethods_gap,
char_u *name)
{
char_u *pstr = (*name == '_') ? name + 1 : name;
// loop 1: class methods, loop 2: object methods
for (int loop = 1; loop <= 2; loop++)
{
garray_T *fgap = (loop == 1) ? classmethods_gap : objmethods_gap;
for (int i = 0; i < fgap->ga_len; ++i)
{
char_u *n = ((ufunc_T **)fgap->ga_data)[i]->uf_name;
@ -721,6 +774,7 @@ is_duplicate_method(garray_T *fgap, char_u *name)
return TRUE;
}
}
}
return FALSE;
}
@ -882,7 +936,7 @@ add_lookup_tables(class_T *cl, class_T *extends_cl, garray_T *objmethods_gap)
return FAIL;
}
// Update the lookup table for the extended class, if nay
// Update the lookup table for the extended class, if any
if (extends_cl != NULL)
{
class_T *pclass = extends_cl;
@ -1017,9 +1071,10 @@ add_classfuncs_objmethods(
int parent_count = 0;
if (extends_cl != NULL)
// Include functions from the parent.
// Include object methods from the parent.
// Don't include the parent class methods.
parent_count = loop == 1
? extends_cl->class_class_function_count
? 0
: extends_cl->class_obj_method_count;
*fcount = parent_count + gap->ga_len;
@ -1087,6 +1142,8 @@ add_classfuncs_objmethods(
{
ufunc_T *fp = (*fup)[i];
fp->uf_class = cl;
if (i < gap->ga_len)
fp->uf_defclass = cl;
if (loop == 2)
fp->uf_flags |= FC_OBJECT;
}
@ -1443,16 +1500,16 @@ early_ret:
break;
}
garray_T *fgap = has_static || is_new
? &classfunctions : &objmethods;
// Check the name isn't used already.
if (is_duplicate_method(fgap, name))
if (is_duplicate_method(&classfunctions, &objmethods, name))
{
success = FALSE;
func_clear_free(uf, FALSE);
break;
}
garray_T *fgap = has_static || is_new
? &classfunctions : &objmethods;
if (ga_grow(fgap, 1) == OK)
{
if (is_new)
@ -1517,19 +1574,23 @@ early_ret:
success = validate_extends_class(extends, &extends_cl);
VIM_CLEAR(extends);
// Check the new class members and object members are not duplicates of the
// members in the extended class lineage.
// Check the new object methods to make sure their access (public or
// private) is the same as that in the extended class lineage.
if (success && extends_cl != NULL)
success = validate_extends_members(&classmembers, &objmembers,
extends_cl);
success = validate_extends_methods(&objmethods, extends_cl);
// Check the new class and object variables are not duplicates of the
// variables in the extended class lineage.
if (success && extends_cl != NULL)
success = validate_extends_members(&objmembers, extends_cl);
// When extending an abstract class, make sure all the abstract methods in
// the parent class are implemented. If the current class is an abstract
// class, then there is no need for this check.
if (success && !is_abstract && extends_cl != NULL
&& (extends_cl->class_flags & CLASS_ABSTRACT))
success = validate_extends_methods(&classfunctions, &objmethods,
extends_cl);
success = validate_abstract_class_methods(&classfunctions,
&objmethods, extends_cl);
class_T **intf_classes = NULL;
@ -1572,12 +1633,10 @@ early_ret:
extends_cl->class_flags |= CLASS_EXTENDED;
}
// Add class and object members to "cl".
// Add class and object variables to "cl".
if (add_members_to_class(&classmembers,
extends_cl == NULL ? NULL
: extends_cl->class_class_members,
extends_cl == NULL ? 0
: extends_cl->class_class_member_count,
NULL,
0,
&cl->class_class_members,
&cl->class_class_member_count) == FAIL
|| add_members_to_class(&objmembers,
@ -1756,7 +1815,21 @@ class_member_type(
member_idx);
if (m == NULL)
{
semsg(_(e_unknown_variable_str), name);
char_u *varname = vim_strnsave(name, len);
if (varname != NULL)
{
if (is_object && class_member_idx(cl, name, len) >= 0)
// A class variable with this name is present
semsg(_(e_class_member_str_accessible_only_inside_class_str),
varname, cl->class_name);
else if (!is_object && object_member_idx(cl, name, len) >= 0)
// An instance variable with this name is present
semsg(_(e_object_member_str_accessible_only_using_object_str),
varname, cl->class_name);
else
semsg(_(e_unknown_variable_str), varname);
}
vim_free(varname);
return &t_any;
}
@ -1887,8 +1960,7 @@ class_object_index(
fp = method_lookup(cl, rettv->v_type, name, len, NULL);
if (fp == NULL)
{
semsg(_(e_method_not_found_on_class_str_str), cl->class_name,
name);
method_not_found_msg(cl, rettv->v_type, name, len);
return FAIL;
}
@ -1951,7 +2023,7 @@ class_object_index(
return OK;
}
semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
member_not_found_msg(cl, VAR_OBJECT, name, len);
}
else if (rettv->v_type == VAR_CLASS)
@ -1962,7 +2034,7 @@ class_object_index(
ocmember_T *m = class_member_lookup(cl, name, len, &m_idx);
if (m == NULL)
{
semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
member_not_found_msg(cl, VAR_CLASS, name, len);
return FAIL;
}
@ -2029,7 +2101,7 @@ fail_after_eval:
}
/*
* Returns the index of class member variable "name" in the class "cl".
* Returns the index of class variable "name" in the class "cl".
* Returns -1, if the variable is not found.
* If "namelen" is zero, then it is assumed that "name" is NUL terminated.
*/
@ -2497,6 +2569,68 @@ object_free_nonref(int copyID)
return did_free;
}
/*
* Echo a class or object method not found message.
*/
void
method_not_found_msg(class_T *cl, vartype_T v_type, char_u *name, size_t len)
{
char_u *method_name = vim_strnsave(name, len);
if ((v_type == VAR_OBJECT)
&& (class_method_idx(cl, name, len) >= 0))
{
// If this is a class method, then give a different error
if (*name == '_')
semsg(_(e_cannot_access_private_method_str), method_name);
else
semsg(_(e_class_member_str_accessible_only_using_class_str),
method_name, cl->class_name);
}
else if ((v_type == VAR_CLASS)
&& (object_method_idx(cl, name, len) >= 0))
{
// If this is an object method, then give a different error
if (*name == '_')
semsg(_(e_cannot_access_private_method_str), method_name);
else
semsg(_(e_object_member_str_accessible_only_using_object_str),
method_name, cl->class_name);
}
else
semsg(_(e_method_not_found_on_class_str_str), cl->class_name,
method_name);
vim_free(method_name);
}
/*
* Echo a class or object member not found message.
*/
void
member_not_found_msg(class_T *cl, vartype_T v_type, char_u *name, size_t len)
{
char_u *varname = len ? vim_strnsave(name, len) : vim_strsave(name);
if (v_type == VAR_OBJECT)
{
if (class_member_idx(cl, name, len) >= 0)
semsg(_(e_class_member_str_accessible_only_using_class_str),
varname, cl->class_name);
else
semsg(_(e_member_not_found_on_object_str_str), cl->class_name,
varname);
}
else
{
if (object_member_idx(cl, name, len) >= 0)
semsg(_(e_object_member_str_accessible_only_using_object_str),
varname, cl->class_name);
else
semsg(_(e_class_member_str_not_found_in_class_str),
varname, cl->class_name);
}
vim_free(varname);
}
/*
* Return TRUE when the class "cl", its base class or one of the implemented
* interfaces matches the class "other_cl".

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),
@ -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

View File

@ -2172,8 +2172,7 @@ execute_storeindex(isn_T *iptr, ectx_T *ectx)
{
if (*member == '_')
{
semsg(_(e_cannot_access_private_member_str),
m->ocm_name);
semsg(_(e_cannot_access_private_member_str), m->ocm_name);
status = FAIL;
}
@ -2181,8 +2180,7 @@ execute_storeindex(isn_T *iptr, ectx_T *ectx)
}
else
{
semsg(_(e_member_not_found_on_object_str_str),
cl->class_name, member);
member_not_found_msg(cl, VAR_OBJECT, member, 0);
status = FAIL;
}
}

View File

@ -367,12 +367,19 @@ compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
}
if (ufunc == NULL)
{
// TODO: different error for object method?
semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
method_not_found_msg(cl, type->tt_type, name, len);
return FAIL;
}
if (*ufunc->uf_name == '_' && !inside_class_hierarchy(cctx, cl))
// A private object method can be used only inside the class where it
// is defined or in one of the child classes.
// A private class method can be used only in the class where it is
// defined.
if (*ufunc->uf_name == '_' &&
((type->tt_type == VAR_OBJECT
&& !inside_class_hierarchy(cctx, cl))
|| (type->tt_type == VAR_CLASS
&& cctx->ctx_ufunc->uf_class != cl)))
{
semsg(_(e_cannot_access_private_method_str), name);
return FAIL;
@ -422,7 +429,7 @@ compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
return generate_FUNCREF(cctx, fp, NULL, 0, NULL);
}
semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
member_not_found_msg(cl, VAR_OBJECT, name, len);
}
else
{
@ -438,7 +445,9 @@ compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
cl->class_name, m->ocm_name);
return FAIL;
}
if (*name == '_' && !inside_class(cctx, cl))
// A private class variable can be accessed only in the class where
// it is defined.
if (*name == '_' && cctx->ctx_ufunc->uf_class != cl)
{
semsg(_(e_cannot_access_private_member_str), m->ocm_name);
return FAIL;
@ -447,7 +456,7 @@ compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
*arg = name_end;
return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
}
semsg(_(e_class_member_not_found_str), name);
member_not_found_msg(cl, VAR_CLASS, name, len);
}
return FAIL;
@ -762,9 +771,17 @@ compile_load(
}
else if ((idx = cctx_class_member_idx(cctx, *arg, len, &cl)) >= 0)
{
// Referencing a class member without the class name. Infer
// the class from the def function context.
// Referencing a class variable without the class name.
// A class variable can be referenced without the class name
// only in the class where the function is defined.
if (cctx->ctx_ufunc->uf_defclass == cl)
res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
else
{
semsg(_(e_class_member_str_accessible_only_inside_class_str),
name, cl->class_name);
res = FAIL;
}
}
else
{
@ -1130,12 +1147,23 @@ compile_call(
}
else if ((mi = cctx_class_method_idx(cctx, name, varlen, &cl)) >= 0)
{
// Class method invocation without the class name. The
// generate_CALL() function expects the class type at the top of
// the stack. So push the class type to the stack.
// Class method invocation without the class name.
// A class method can be referenced without the class name only in
// the class where the function is defined.
if (cctx->ctx_ufunc->uf_defclass == cl)
{
// The generate_CALL() function expects the class type at the
// top of the stack. So push the class type to the stack.
push_type_stack(cctx, &t_class);
res = generate_CALL(cctx, cl->class_class_functions[mi], NULL, 0,
type, argcount);
res = generate_CALL(cctx, cl->class_class_functions[mi], NULL,
0, type, argcount);
}
else
{
semsg(_(e_class_member_str_accessible_only_inside_class_str),
name, cl->class_name);
res = FAIL;
}
goto theend;
}
}