Skip to content

Commit

Permalink
patch 9.0.2002: Vim9: need cleanup of class related interface code
Browse files Browse the repository at this point in the history
Problem:  Vim9: need cleanup of class related interface code
Solution: Remove the unused class variable and class method related code
          for interfaces.

Remove unused class variable and class method related code for
interfaces.

Refactor the code.

Optimize the object/class member double lookup in compile_lhs().

Change unused global functions to static functions.

closes: #13302

Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
  • Loading branch information
yegappan authored and chrisbra committed Oct 8, 2023
1 parent 75b277d commit b852305
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 256 deletions.
4 changes: 0 additions & 4 deletions src/proto/vim9class.pro
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ ufunc_T *find_class_func(char_u **arg);
int class_member_idx(class_T *cl, char_u *name, size_t namelen);
ocmember_T *class_member_lookup(class_T *cl, char_u *name, size_t namelen, int *idx);
int class_method_idx(class_T *cl, char_u *name, size_t namelen);
ufunc_T *class_method_lookup(class_T *cl, char_u *name, size_t namelen, int *idx);
int object_member_idx(class_T *cl, char_u *name, size_t namelen);
ocmember_T *object_member_lookup(class_T *cl, char_u *name, size_t namelen, int *idx);
int object_method_idx(class_T *cl, char_u *name, size_t namelen);
ufunc_T *object_method_lookup(class_T *cl, char_u *name, size_t namelen, int *idx);
ocmember_T *member_lookup(class_T *cl, vartype_T v_type, char_u *name, size_t namelen, int *idx);
void emsg_var_cl_define(char *msg, char_u *name, size_t len, class_T *cl);
ufunc_T *method_lookup(class_T *cl, vartype_T v_type, char_u *name, size_t namelen, int *idx);
Expand All @@ -26,7 +23,6 @@ void class_unref(class_T *cl);
int class_free_nonref(int copyID);
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);
Expand Down
57 changes: 51 additions & 6 deletions src/testdir/test_vim9_class.vim
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,28 @@ def Test_class_new_with_object_member()
Check()
END
v9.CheckSourceSuccess(lines)

# Try using "this." argument in a class method
lines =<< trim END
vim9script
class A
this.val = 10
static def Foo(this.val: number)
enddef
endclass
END
v9.CheckSourceFailure(lines, 'E1390: Cannot use an object variable "this.val" except with the "new" method', 4)

# Try using "this." argument in an object method
lines =<< trim END
vim9script
class A
this.val = 10
def Foo(this.val: number)
enddef
endclass
END
v9.CheckSourceFailure(lines, 'E1390: Cannot use an object variable "this.val" except with the "new" method', 4)
enddef

def Test_class_object_member_inits()
Expand Down Expand Up @@ -1722,7 +1744,7 @@ def Test_class_member()
var a = A.new()
var v = a.bar
END
v9.CheckSourceFailure(lines, 'E1326: Variable not found on object "A": bar', 5)
v9.CheckSourceFailure(lines, 'E1337: Class variable "bar" not found in class "A"', 5)
enddef

" These messages should show the defining class of the variable (base class),
Expand Down Expand Up @@ -4255,7 +4277,7 @@ def Test_private_object_method()
var a = A.new()
a._Foo()
END
v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo()', 9)
v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo', 9)

# Try calling a private method using an object (from a def function)
lines =<< trim END
Expand Down Expand Up @@ -4468,7 +4490,7 @@ def Test_private_object_method()
var c = C.new()
assert_equal(1234, c._Foo())
END
v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo()', 16)
v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo', 16)

# Using "_" prefix in a method name should fail outside of a class
lines =<< trim END
Expand All @@ -4494,7 +4516,7 @@ def Test_private_class_method()
endclass
A._Foo()
END
v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo()', 8)
v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo', 8)

# Try calling a class private method (from a def function)
lines =<< trim END
Expand Down Expand Up @@ -5122,7 +5144,7 @@ def Test_class_variable_access_using_object()
var a = A.new()
echo a.svar2
END
v9.CheckSourceFailure(lines, 'E1375: Class variable "svar2" accessible only using class "A"', 8)
v9.CheckSourceFailure(lines, 'E1337: Class variable "svar2" not found in class "A"', 8)

# Cannot write to a class variable using an object in script context
lines =<< trim END
Expand Down Expand Up @@ -5597,7 +5619,7 @@ def Test_class_variable()
var a = A.new()
var i = a.val
END
v9.CheckSourceFailure(lines, 'E1375: Class variable "val" accessible only using class "A"', 7)
v9.CheckSourceFailure(lines, 'E1337: Class variable "val" not found in class "A"', 7)

# Modifying a class variable using an object at function level
lines =<< trim END
Expand Down Expand Up @@ -5969,6 +5991,18 @@ def Test_extend_interface()
END
v9.CheckSourceSuccess(lines)

# extending empty interface
lines =<< trim END
vim9script
interface A
endinterface
interface B extends A
endinterface
class C implements B
endclass
END
v9.CheckSourceSuccess(lines)

lines =<< trim END
vim9script
interface A
Expand Down Expand Up @@ -6567,6 +6601,17 @@ def Test_reserved_varname()
o.F()
END
v9.CheckSourceFailure(lines, $'E1034: Cannot use reserved name {kword}', 3)

# class variable name
if kword != 'this'
lines =<< trim eval END
vim9script
class C
public static {kword}: list<number> = [1, 2, 3]
endclass
END
v9.CheckSourceFailure(lines, $'E1034: Cannot use reserved name {kword}', 3)
endif
endfor
enddef

Expand Down
2 changes: 2 additions & 0 deletions src/version.c
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
/**/
2002,
/**/
2001,
/**/
Expand Down

0 comments on commit b852305

Please sign in to comment.