Mercurial > vim
diff src/vim9expr.c @ 33260:aba1fa2b7d1e v9.0.1898
patch 9.0.1898: Vim9: restrict access to static vars
Commit: https://github.com/vim/vim/commit/c30a90d9b2c029f794cea502f6b824f71e4876dd
Author: Yegappan Lakshmanan <yegappan@yahoo.com>
Date: Fri Sep 15 20:14:55 2023 +0200
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>
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Fri, 15 Sep 2023 20:30:05 +0200 |
parents | 877dddec681f |
children | 0c3553cfe22e |
line wrap: on
line diff
--- a/src/vim9expr.c +++ b/src/vim9expr.c @@ -367,12 +367,19 @@ compile_class_object_index(cctx_T *cctx, } 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, 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, 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, *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. - res = generate_CLASSMEMBER(cctx, TRUE, cl, idx); + // 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. - push_type_stack(cctx, &t_class); - res = generate_CALL(cctx, cl->class_class_functions[mi], NULL, 0, - type, argcount); + // 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); + } + else + { + semsg(_(e_class_member_str_accessible_only_inside_class_str), + name, cl->class_name); + res = FAIL; + } goto theend; } }