summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYegappan Lakshmanan <yegappan@yahoo.com>2023-09-17 17:03:19 +0200
committerChristian Brabandt <cb@256bit.org>2023-09-17 17:03:19 +0200
commit92d9ee5f4ca0d2de04c39afbafc7609da43fb2e9 (patch)
treed8482b4f9eaf3d6ab8dec32d29784c7bf64946d5
parent0483e49f90d85f5172f2050426913ac718f1c176 (diff)
patch 9.0.1906: Vim9: Interfaces should not support class methods and variablesv9.0.1906
Problem: Vim9: Interfaces should not support class methods and variables Solution: Make sure interface follow the interface specification Vim9 interface changes to follow the new interface specification: 1) An interface can have only read-only and read-write instance variables. 2) An interface can have only public instance methods. 3) An interface cannot have class variables and class methods. 4) An interface cannot have private instance variables and private instance methods. 5) A interface can extend another interface using "extends". The sub-interface gets all the variables and methods in the super interface. That means: - Interfaces should not support class methods and variables. - Adjust error numbers and add additional tests. - Interface methods can be defined in one of the super classes. - Interface variables can be defined in one of the super classes. and instance variables can be repeated in sub interfaces. - Check the class variable types with the type in interface. closes: #13100 Signed-off-by: Christian Brabandt <cb@256bit.org> Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
-rw-r--r--src/errors.h43
-rw-r--r--src/testdir/test_vim9_builtin.vim19
-rw-r--r--src/testdir/test_vim9_class.vim872
-rw-r--r--src/testdir/test_vim9_disassemble.vim2
-rw-r--r--src/version.c2
-rw-r--r--src/vim9class.c424
6 files changed, 909 insertions, 453 deletions
diff --git a/src/errors.h b/src/errors.h
index 6184037df8..1db70786b3 100644
--- a/src/errors.h
+++ b/src/errors.h
@@ -1572,8 +1572,7 @@ EXTERN char e_unknown_printer_font_str[]
#endif
EXTERN char e_class_required[]
INIT(= N_("E614: Class required"));
-EXTERN char e_object_required[]
- INIT(= N_("E615: Object required"));
+// E615 unused
EXTERN char e_object_required_for_argument_nr[]
INIT(= N_("E616: Object required for argument %d"));
#ifdef FEAT_GUI_GTK
@@ -3401,8 +3400,7 @@ EXTERN char e_object_required_found_str[]
INIT(= N_("E1327: Object required, found %s"));
EXTERN char e_constructor_default_value_must_be_vnone_str[]
INIT(= N_("E1328: Constructor default value must be v:none: %s"));
-EXTERN char e_cannot_get_object_member_type_from_initializer_str[]
- INIT(= N_("E1329: Cannot get object member type from initializer: %s"));
+// E1329 unused
EXTERN char e_invalid_type_for_object_member_str[]
INIT(= N_("E1330: Invalid type for object member: %s"));
EXTERN char e_public_must_be_followed_by_this_or_static[]
@@ -3411,6 +3409,7 @@ 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"));
+// E1334 unused
EXTERN char e_member_is_not_writable_str[]
INIT(= N_("E1335: Member is not writable: %s"));
#endif
@@ -3419,8 +3418,8 @@ EXTERN char e_internal_error_shortmess_too_long[]
#ifdef FEAT_EVAL
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"));
+EXTERN char e_interface_static_direct_access_str[]
+ INIT(= N_("E1338: Cannot directly access interface \"%s\" static member \"%s\""));
#endif
#ifdef FEAT_PROP_POPUP
EXTERN char e_cannot_add_textprop_with_text_after_using_textprop_with_negative_id[]
@@ -3444,9 +3443,9 @@ EXTERN char e_interface_name_not_found_str[]
EXTERN char e_not_valid_interface_str[]
INIT(= N_("E1347: Not a valid interface: %s"));
EXTERN char e_member_str_of_interface_str_not_implemented[]
- INIT(= N_("E1348: Member \"%s\" of interface \"%s\" not implemented"));
-EXTERN char e_function_str_of_interface_str_not_implemented[]
- INIT(= N_("E1349: Function \"%s\" of interface \"%s\" not implemented"));
+ INIT(= N_("E1348: Member \"%s\" of interface \"%s\" is not implemented"));
+EXTERN char e_method_str_of_interface_str_not_implemented[]
+ INIT(= N_("E1349: Method \"%s\" of interface \"%s\" is not implemented"));
EXTERN char e_duplicate_implements[]
INIT(= N_("E1350: Duplicate \"implements\""));
EXTERN char e_duplicate_interface_after_implements_str[]
@@ -3480,6 +3479,7 @@ EXTERN char e_incomplete_type[]
#endif
EXTERN char e_warning_pointer_block_corrupted[]
INIT(= N_("E1364: Warning: Pointer block corrupted"));
+#ifdef FEAT_EVAL
EXTERN char e_cannot_use_a_return_type_with_new[]
INIT(= N_("E1365: Cannot use a return type with the \"new\" function"));
EXTERN char e_cannot_access_private_method_str[]
@@ -3504,10 +3504,21 @@ 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\""));
+ INIT(= N_("E1377: Access level of method \"%s\" is different in class \"%s\""));
+EXTERN char e_static_cannot_be_used_in_interface[]
+ INIT(= N_("E1378: Static cannot be used in an interface"));
+EXTERN char e_private_variable_str_in_interface[]
+ INIT(= N_("E1379: Private variable not supported in an interface"));
+EXTERN char e_private_method_str_in_interface[]
+ INIT(= N_("E1380: Private method not supported in an interface"));
+EXTERN char e_interface_cannot_use_implements[]
+ INIT(= N_("E1381: Interface cannot use \"implements\""));
+EXTERN char e_member_str_type_mismatch_expected_str_but_got_str[]
+ INIT(= N_("E1382: Member \"%s\": type mismatch, expected %s but got %s"));
+EXTERN char e_method_str_type_mismatch_expected_str_but_got_str[]
+ INIT(= N_("E1383: Method \"%s\": type mismatch, expected %s but got %s"));
+#endif
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[]
@@ -3520,12 +3531,6 @@ EXTERN char e_positional_arg_num_type_inconsistent_str_str[]
INIT(= N_("E1404: Positional argument %d type used inconsistently: %s/%s"));
EXTERN char e_invalid_format_specifier_str[]
INIT(= N_("E1405: Invalid format specifier: %s"));
-EXTERN char e_member_str_type_mismatch_expected_str_but_got_str[]
- INIT(= N_("E1406: Member \"%s\": type mismatch, expected %s but got %s"));
-EXTERN char e_method_str_type_mismatch_expected_str_but_got_str[]
- INIT(= N_("E1407: Method \"%s\": type mismatch, expected %s but got %s"));
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\""));
-// E1376 - E1399 unused
+// E1384 - E1399 unused
diff --git a/src/testdir/test_vim9_builtin.vim b/src/testdir/test_vim9_builtin.vim
index c793da6b48..1efc47a074 100644
--- a/src/testdir/test_vim9_builtin.vim
+++ b/src/testdir/test_vim9_builtin.vim
@@ -2339,6 +2339,25 @@ def Test_instanceof()
Bar()
END
v9.CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected class<Unknown> but got number')
+
+ lines =<< trim END
+ vim9script
+ class Foo
+ endclass
+ instanceof(Foo.new(), [{}])
+ END
+ v9.CheckSourceFailure(lines, 'E614: Class required')
+
+ lines =<< trim END
+ vim9script
+ class Foo
+ endclass
+ def Bar()
+ instanceof(Foo.new(), [{}])
+ enddef
+ Bar()
+ END
+ v9.CheckSourceFailure(lines, 'E614: Class required')
enddef
def Test_invert()
diff --git a/src/testdir/test_vim9_class.vim b/src/testdir/test_vim9_class.vim
index 1796f5f72e..5e3b945591 100644
--- a/src/testdir/test_vim9_class.vim
+++ b/src/testdir/test_vim9_class.vim
@@ -145,6 +145,65 @@ def Test_class_basic()
END
v9.CheckSourceFailure(lines, 'E1170:')
+ # Test for using class as a bool
+ lines =<< trim END
+ vim9script
+ class A
+ endclass
+ if A
+ endif
+ END
+ v9.CheckSourceFailure(lines, 'E1319: Using a class as a Number')
+
+ # Test for using object as a bool
+ lines =<< trim END
+ vim9script
+ class A
+ endclass
+ var a = A.new()
+ if a
+ endif
+ END
+ v9.CheckSourceFailure(lines, 'E1320: Using an object as a Number')
+
+ # Test for using class as a float
+ lines =<< trim END
+ vim9script
+ class A
+ endclass
+ sort([1.1, A], 'f')
+ END
+ v9.CheckSourceFailure(lines, 'E1321: Using a class as a Float')
+
+ # Test for using object as a float
+ lines =<< trim END
+ vim9script
+ class A
+ endclass
+ var a = A.new()
+ sort([1.1, a], 'f')
+ END
+ v9.CheckSourceFailure(lines, 'E1322: Using an object as a Float')
+
+ # Test for using class as a string
+ lines =<< trim END
+ vim9script
+ class A
+ endclass
+ :exe 'call ' .. A
+ END
+ v9.CheckSourceFailure(lines, 'E1323: Using a class as a String')
+
+ # Test for using object as a string
+ lines =<< trim END
+ vim9script
+ class A
+ endclass
+ var a = A.new()
+ :exe 'call ' .. a
+ END
+ v9.CheckSourceFailure(lines, 'E1324: Using an object as a String')
+
lines =<< trim END
vim9script
@@ -689,6 +748,18 @@ def Test_class_default_new()
var missing = Person.new()
END
v9.CheckSourceFailure(lines, 'E119:')
+
+ # Using a specific value to initialize an instance variable in the new()
+ # method.
+ lines =<< trim END
+ vim9script
+ class A
+ this.val: string
+ def new(this.val = 'a')
+ enddef
+ endclass
+ END
+ v9.CheckSourceFailure(lines, "E1328: Constructor default value must be v:none: = 'a'")
enddef
@@ -825,6 +896,15 @@ def Test_class_object_member_inits()
var a = A.new()
END
v9.CheckSourceFailure(lines, 'E1001:')
+
+ # Test for initializing an object member with an special type
+ lines =<< trim END
+ vim9script
+ class A
+ this.value: void
+ endclass
+ END
+ v9.CheckSourceFailure(lines, 'E1330: Invalid type for object member: void')
enddef
" Test for instance variable access
@@ -1556,17 +1636,10 @@ func Test_interface_garbagecollect()
vim9script
interface I
- static ro_class_var: number
- public static rw_class_var: number
- static _priv_class_var: number
this.ro_obj_var: number
public this.rw_obj_var: number
- this._priv_obj_var: number
- static def ClassFoo(): number
- static def _ClassBar(): number
def ObjFoo(): number
- def _ObjBar(): number
endinterface
class A implements I
@@ -1736,8 +1809,8 @@ def Test_interface_basics()
var lines =<< trim END
vim9script
interface Something
- this.value: string
- static count: number
+ this.ro_var: string
+ public this.rw_var: list<number>
def GetCount(): number
endinterface
END
@@ -1754,16 +1827,6 @@ def Test_interface_basics()
vim9script
interface Some
- static count: number
- def Method(count: number)
- endinterface
- END
- v9.CheckSourceFailure(lines, 'E1340: Argument already declared in the class: count', 5)
-
- lines =<< trim END
- vim9script
-
- interface Some
this.value: number
def Method(value: number)
endinterface
@@ -1784,7 +1847,7 @@ def Test_interface_basics()
vim9script
interface SomethingWrong
this.value: string
- static count = 7
+ this.count = 7
def GetCount(): number
endinterface
END
@@ -1794,7 +1857,7 @@ def Test_interface_basics()
vim9script
interface SomethingWrong
this.value: string
- static count: number
+ this.count: number
def GetCount(): number
return 5
enddef
@@ -1845,12 +1908,12 @@ def Test_class_implements_interface()
vim9script
interface Some
- static count: number
+ this.count: number
def Method(nr: number)
endinterface
class SomeImpl implements Some
- static count: number
+ this.count: number
def Method(nr: number)
echo nr
enddef
@@ -1862,7 +1925,7 @@ def Test_class_implements_interface()
class AnotherImpl implements Some, Another
this.member = 'abc'
- static count: number
+ this.count = 20
def Method(nr: number)
echo nr
enddef
@@ -1874,11 +1937,11 @@ def Test_class_implements_interface()
vim9script
interface Some
- static counter: number
+ this.count: number
endinterface
class SomeImpl implements Some implements Some
- static count: number
+ this.count: number
endclass
END
v9.CheckSourceFailure(lines, 'E1350:')
@@ -1887,11 +1950,11 @@ def Test_class_implements_interface()
vim9script
interface Some
- static counter: number
+ this.count: number
endinterface
class SomeImpl implements Some, Some
- static count: number
+ this.count: number
endclass
END
v9.CheckSourceFailure(lines, 'E1351: Duplicate interface after "implements": Some')
@@ -1900,35 +1963,35 @@ def Test_class_implements_interface()
vim9script
interface Some
- static counter: number
+ this.counter: number
def Method(nr: number)
endinterface
class SomeImpl implements Some
- static count: number
+ this.count: number
def Method(nr: number)
echo nr
enddef
endclass
END
- v9.CheckSourceFailure(lines, 'E1348: Member "counter" of interface "Some" not implemented')
+ v9.CheckSourceFailure(lines, 'E1348: Member "counter" of interface "Some" is not implemented')
lines =<< trim END
vim9script
interface Some
- static count: number
+ this.count: number
def Methods(nr: number)
endinterface
class SomeImpl implements Some
- static count: number
+ this.count: number
def Method(nr: number)
echo nr
enddef
endclass
END
- v9.CheckSourceFailure(lines, 'E1349: Function "Methods" of interface "Some" not implemented')
+ v9.CheckSourceFailure(lines, 'E1349: Method "Methods" of interface "Some" is not implemented')
# Check different order of members in class and interface works.
lines =<< trim END
@@ -2005,17 +2068,6 @@ def Test_class_implements_interface()
END
v9.CheckSourceFailure(lines, 'E1347:')
- # all the class methods in an "interface" should be implemented
- lines =<< trim END
- vim9script
- interface A
- static def Foo()
- endinterface
- class B implements A
- endclass
- END
- v9.CheckSourceFailure(lines, 'E1349:')
-
# implements should be followed by a white space
lines =<< trim END
vim9script
@@ -2030,22 +2082,6 @@ def Test_class_implements_interface()
vim9script
interface One
- static matching: bool
- static as_any: any
- static not_matching: number
- endinterface
- class Two implements One
- static not_matching: string
- static as_any: string
- static matching: bool
- endclass
- END
- v9.CheckSourceFailure(lines, 'E1406: Member "not_matching": type mismatch, expected number but got string')
-
- lines =<< trim END
- vim9script
-
- interface One
def IsEven(nr: number): bool
endinterface
class Two implements One
@@ -2053,7 +2089,7 @@ def Test_class_implements_interface()
enddef
endclass
END
- v9.CheckSourceFailure(lines, 'E1407: Method "IsEven": type mismatch, expected func(number): bool but got func(number): string')
+ v9.CheckSourceFailure(lines, 'E1383: Method "IsEven": type mismatch, expected func(number): bool but got func(number): string')
lines =<< trim END
vim9script
@@ -2066,7 +2102,7 @@ def Test_class_implements_interface()
enddef
endclass
END
- v9.CheckSourceFailure(lines, 'E1407: Method "IsEven": type mismatch, expected func(number): bool but got func(bool): bool')
+ v9.CheckSourceFailure(lines, 'E1383: Method "IsEven": type mismatch, expected func(number): bool but got func(bool): bool')
lines =<< trim END
vim9script
@@ -2079,15 +2115,13 @@ def Test_class_implements_interface()
enddef
endclass
END
- v9.CheckSourceFailure(lines, 'E1407: Method "IsEven": type mismatch, expected func(number): bool but got func(number, ...list<number>): bool')
+ v9.CheckSourceFailure(lines, 'E1383: Method "IsEven": type mismatch, expected func(number): bool but got func(number, ...list<number>): bool')
# access superclass interface members from subclass, mix variable order
lines =<< trim END
vim9script
interface I1
- public static svar1: number
- public static svar2: number
public this.mvar1: number
public this.mvar2: number
endinterface
@@ -2140,15 +2174,11 @@ def Test_class_implements_interface()
vim9script
interface I1
- public static svar1: number
- public static svar2: number
public this.mvar1: number
public this.mvar2: number
endinterface
interface I2
- public static svar3: number
- public static svar4: number
public this.mvar3: number
public this.mvar4: number
endinterface
@@ -3747,146 +3777,6 @@ def Test_private_class_method()
v9.CheckSourceFailure(lines, 'E1325: Method not found on class "C": _Foo')
enddef
-" Test for an interface private object_method
-def Test_interface_private_object_method()
- # Implement an interface private method and use it from a public method
- var lines =<< trim END
- vim9script
- interface Intf
- def _Foo(): number
- endinterface
- class A implements Intf
- def _Foo(): number
- return 1234
- enddef
- def Bar(): number
- return this._Foo()
- enddef
- endclass
- var a = A.new()
- assert_equal(1234, a.Bar())
- END
- v9.CheckSourceSuccess(lines)
-
- # Call an interface private class method (script context)
- lines =<< trim END
- vim9script
- interface Intf
- def _Foo(): number
- endinterface
- class A implements Intf
- def _Foo(): number
- return 1234
- enddef
- endclass
- var a = A.new()
- assert_equal(1234, a._Foo())
- END
- v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo()')
-
- # Call an interface private class method (def context)
- lines =<< trim END
- vim9script
- interface Intf
- def _Foo(): number
- endinterface
- class A implements Intf
- def _Foo(): number
- return 1234
- enddef
- endclass
- def T()
- var a = A.new()
- assert_equal(1234, a._Foo())
- enddef
- T()
- END
- v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo()')
-
- # Implement an interface private object method as a private class method
- lines =<< trim END
- vim9script
- interface Intf
- def _Foo(): number
- endinterface
- class A implements Intf
- static def _Foo(): number
- return 1234
- enddef
- endclass
- END
- v9.CheckSourceFailure(lines, 'E1349: Function "_Foo" of interface "Intf" not implemented')
-enddef
-
-" Test for an interface private class method
-def Test_interface_private_class_method()
- # Implement an interface private class method and use it from a public method
- var lines =<< trim END
- vim9script
- interface Intf
- static def _Foo(): number
- endinterface
- class A implements Intf
- static def _Foo(): number
- return 1234
- enddef
- def Bar(): number
- return A._Foo()
- enddef
- endclass
- var a = A.new()
- assert_equal(1234, a.Bar())
- END
- v9.CheckSourceSuccess(lines)
-
- # Call an interface private class method (script context)
- lines =<< trim END
- vim9script
- interface Intf
- static def _Foo(): number
- endinterface
- class A implements Intf
- static def _Foo(): number
- return 1234
- enddef
- endclass
- assert_equal(1234, A._Foo())
- END
- v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo())')
-
- # Call an interface private class method (def context)
- lines =<< trim END
- vim9script
- interface Intf
- static def _Foo(): number
- endinterface
- class A implements Intf
- static def _Foo(): number
- return 1234
- enddef
- endclass
- def T()
- assert_equal(1234, A._Foo())
- enddef
- T()
- END
- v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo())')
-
- # Implement an interface private class method as a private object method
- lines =<< trim END
- vim9script
- interface Intf
- static def _Foo(): number
- endinterface
- class A implements Intf
- def _Foo(): number
- return 1234
- enddef
- endclass
- END
- v9.CheckSourceFailure(lines, 'E1349: Function "_Foo" of interface "Intf" not implemented')
-enddef
-
" Test for using the return value of a class/object method as a function
" argument.
def Test_objmethod_funcarg()
@@ -4142,124 +4032,6 @@ def Test_dup_member_variable()
v9.CheckSourceSuccess(lines)
enddef
-def Test_interface_static_member_access()
- # In a class cannot read from interface static
- var lines =<< trim END
- vim9script
- interface I
- public static num: number
- endinterface
- class C implements I
- public static num = 3
- def F()
- var x = I.num
- enddef
- endclass
- C.new().F()
- END
- v9.CheckSourceFailure(lines, 'E1409: Cannot directly access interface "I" static member "num"')
-
- # In a class cannot write to interface static
- lines =<< trim END
- vim9script
- interface I
- public static num: number
- endinterface
- class C implements I
- public static num = 3
- def F()
- I.num = 7
- enddef
- endclass
- C.new().F()
- END
- v9.CheckSourceFailure(lines, 'E1409: Cannot directly access interface "I" static member "num"')
-
- # In a def cannot read from interface static
- lines =<< trim END
- vim9script
- interface I
- public static num: number
- endinterface
- def F()
- var x = I.num
- enddef
- F()
- END
- v9.CheckSourceFailure(lines, 'E1409: Cannot directly access interface "I" static member "num"')
-
- # In a def cannot write to interface static
- lines =<< trim END
- vim9script
- interface I
- public static num: number
- endinterface
- def F()
- I.num = 7
- enddef
- F()
- END
- v9.CheckSourceFailure(lines, 'E1409: Cannot directly access interface "I" static member "num"')
-
- # script level cannot read interface static
- lines =<< trim END
- vim9script
- interface I
- public static s_var1: number
- endinterface
-
- var x = I.s_var1
- END
- v9.CheckSourceFailure(lines, 'E1409: Cannot directly access interface "I" static member "s_var1"')
-
- # script level cannot write interface static
- lines =<< trim END
- vim9script
- interface I
- public static s_var1: number
- endinterface
-
- I.s_var1 = 3
- END
- v9.CheckSourceFailure(lines, 'E1409: Cannot directly access interface "I" static member "I.s_var1 = 3"')
-
-enddef
-
-def Test_static_member_access_outside_class()
- # Verify access of statics implemented from interface
- # in a :def (outside of a class)
- # Note the order of the static is different
- # between the interface and the class,
- # since they are allocated in order in each interface/class;
- # so the static index is mapped from interfaced to class as needed.
-
- # Check reading statics
- var lines =<< trim END
- vim9script
-
- interface I
- public static s_var1: number
- public static s_var2: number
- endinterface
-
- class C implements I
- public static s_var2 = 2
- public static x_static = 7
- public static s_var1 = 1
- endclass
-
- def F1(): number
- assert_equal(1, C.s_var1)
- assert_equal(2, C.s_var2)
- assert_equal(7, C.x_static)
- return 11
- enddef
-
- assert_equal(11, F1())
- END
- v9.CheckSourceSuccess(lines)
-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
@@ -4627,9 +4399,11 @@ def Test_abstract_method()
abstract def Foo()
endinterface
class B implements A
+ def Foo()
+ enddef
endclass
END
- v9.CheckSourceFailure(lines, 'E1372: Abstract method "abstract def Foo()" cannot be defined in a concrete class')
+ v9.CheckSourceSuccess(lines)
# Abbreviate the "abstract" keyword
lines =<< trim END
@@ -4676,7 +4450,7 @@ def Test_abstract_method()
enddef
endclass
END
- v9.CheckSourceFailure(lines, 'E1407: Method "Foo": type mismatch, expected func(string, number): list<number> but got func(number, string): list<string>')
+ v9.CheckSourceFailure(lines, 'E1383: Method "Foo": type mismatch, expected func(string, number): list<number> but got func(number, string): list<string>')
# Use an abstract class to invoke an abstract method
# FIXME: This should fail
@@ -5149,7 +4923,7 @@ def Test_instance_method_access_level()
enddef
endclass
END
- v9.CheckSourceFailure(lines, 'E1378: Access level of method "_Foo" is different in class "A"')
+ v9.CheckSourceFailure(lines, 'E1377: Access level of method "_Foo" is different in class "A"')
# Public method in subclass
lines =<< trim END
@@ -5165,7 +4939,7 @@ def Test_instance_method_access_level()
enddef
endclass
END
- v9.CheckSourceFailure(lines, 'E1378: Access level of method "Foo" is different in class "A"')
+ v9.CheckSourceFailure(lines, 'E1377: Access level of method "Foo" is different in class "A"')
enddef
def Test_extend_empty_class()
@@ -5194,4 +4968,438 @@ def Test_extend_empty_class()
v9.CheckSourceSuccess(lines)
enddef
+" A interface cannot have a static variable or a static method or a private
+" variable or a private method
+def Test_interface_with_unsupported_members()
+ var lines =<< trim END
+ vim9script
+ interface A
+ static num: number
+ endinterface
+ END
+ v9.CheckSourceFailure(lines, 'E1378: Static cannot be used in an interface')
+
+ lines =<< trim END
+ vim9script
+ interface A
+ static _num: number
+ endinterface
+ END
+ v9.CheckSourceFailure(lines, 'E1378: Static cannot be used in an interface')
+
+ lines =<< trim END
+ vim9script
+ interface A
+ public static num: number
+ endinterface
+ END
+ v9.CheckSourceFailure(lines, 'E1378: Static cannot be used in an interface')
+
+ lines =<< trim END
+ vim9script
+ interface A
+ public static _num: number
+ endinterface
+ END
+ v9.CheckSourceFailure(lines, 'E1378: Static cannot be used in an interface')
+
+ lines =<< trim END
+ vim9script
+ interface A
+ static def Foo(d: dict<any>): list<string>
+ endinterface
+ END
+ v9.CheckSourceFailure(lines, 'E1378: Static cannot be used in an interface')
+
+ lines =<< trim END
+ vim9script
+ interface A
+ static def _Foo(d: dict<any>): list<string>
+ endinterface
+ END
+ v9.CheckSourceFailure(lines, 'E1378: Static cannot be used in an interface')
+
+ lines =<< trim END
+ vim9script
+ interface A
+ this._Foo: list<string>
+ endinterface
+ END
+ v9.CheckSourceFailure(lines, 'E1379: Private variable not supported in an interface')
+
+ lines =<< trim END
+ vim9script
+ interface A
+ def _Foo(d: dict<any>): list<string>
+ endinterface
+ END
+ v9.CheckSourceFailure(lines, 'E1380: Private method not supported in an interface')
+enddef
+
+" Test for extending an interface
+def Test_extend_interface()
+ var lines =<< trim END
+ vim9script
+ interface A
+ this.var1: list<string>
+ def Foo()
+ endinterface
+ interface B extends A
+ public this.var2: dict<string>
+ def Bar()
+ endinterface
+ class C implements A, B
+ this.var1 = [1, 2]
+ def Foo()
+ enddef
+ public this.var2 = {a: '1'}
+ def Bar()
+ enddef
+ endclass
+ END
+ v9.CheckSourceSuccess(lines)
+
+ lines =<< trim END
+ vim9script
+ interface A
+ def Foo()
+ endinterface
+ interface B extends A
+ public this.var2: dict<string>
+ endinterface
+ class C implements A, B
+ public this.var2 = {a: '1'}
+ endclass
+ END
+ v9.CheckSourceFailure(lines, 'E1349: Method "Foo" of interface "A" is not implemented')
+
+ lines =<< trim END
+ vim9script
+ interface A
+ def Foo()
+ endinterface
+ interface B extends A
+ public this.var2: dict<string>
+ endinterface
+ class C implements A, B
+ def Foo()
+ enddef
+ endclass
+ END
+ v9.CheckSourceFailure(lines, 'E1348: Member "var2" of interface "B" is not implemented')
+
+ # interface cannot extend a class
+ lines =<< trim END
+ vim9script
+ class A
+ endclass
+ interface B extends A
+ endinterface
+ END
+ v9.CheckSourceFailure(lines, 'E1354: Cannot extend A')
+
+ # class cannot extend an interface
+ lines =<< trim END
+ vim9script
+ interface A
+ endinterface
+ class B extends A
+ endclass
+ END
+ v9.CheckSourceFailure(lines, 'E1354: Cannot extend A')
+
+ # interface cannot implement another interface
+ lines =<< trim END
+ vim9script
+ interface A
+ endinterface
+ interface B implements A
+ endinterface
+ END
+ v9.CheckSourceFailure(lines, 'E1381: Interface cannot use "implements"')
+
+ # interface cannot extend multiple interfaces
+ lines =<< trim END
+ vim9script
+ interface A
+ endinterface
+ interface B
+ endinterface
+ interface C extends A, B
+ endinterface
+ END
+ v9.CheckSourceFailure(lines, 'E1315: White space required after name: A, B')
+
+ # Variable type in an extended interface is of different type
+ lines =<< trim END
+ vim9script
+ interface A
+ this.val1: number
+ endinterface
+ interface B extends A
+ this.val2: string
+ endinterface
+ interface C extends B
+ this.val1: string
+ this.val2: number
+ endinterface
+ END
+ v9.CheckSourceFailure(lines, 'E1382: Member "val1": type mismatch, expected number but got string')
+enddef
+
+" Test for a child class implementing an interface when some of the methods are
+" defined in the parent class.
+def Test_child_class_implements_interface()
+ var lines =<< trim END
+ vim9script
+
+ interface Intf
+ def F1(): list<list<number>>
+ def F2(): list<list<number>>
+ def F3(): list<list<number>>
+ this.var1: list<dict<number>>
+ this.var2: list<dict<number>>
+ this.var3: list<dict<number>>
+ endinterface
+
+ class A
+ def A1()
+ enddef
+ def F3(): list<list<number>>
+ return [[3]]
+ enddef
+ this.v1: list<list<number>> = [[0]]
+ this.var3 = [{c: 30}]
+ endclass
+
+ class B extends A
+ def B1()
+ enddef
+ def F2(): list<list<number>>
+ return [[2]]
+ enddef
+ this.v2: list<list<number>> = [[0]]
+ this.var2 = [{b: 20}]
+ endclass
+
+ class C extends B implements Intf
+ def C1()
+ enddef
+ def F1(): list<list<number>>
+ return [[1]]
+ enddef
+ this.v3: list<list<number>> = [[0]]
+ this.var1 = [{a: 10}]
+ endclass
+
+ def T(if: Intf)
+ assert_equal([[1]], if.F1())
+ assert_equal([[2]], if.F2())
+ assert_equal([[3]], if.F3())
+ assert_equal([{a: 10}], if.var1)
+ assert_equal([{b: 20}], if.var2)
+ assert_equal([{c: 30}], if.var3)
+ enddef
+
+ var c = C.new()
+ T(c)
+ assert_equal([[1]], c.F1())
+ assert_equal([[2]], c.F2())
+ assert_equal([[3]], c.F3())
+ assert_equal([{a: 10}], c.var1)
+ assert_equal([{b: 20}], c.var2)
+ assert_equal([{c: 30}], c.var3)
+ END
+ v9.CheckSourceSuccess(lines)
+
+ # One of the interface methods is not found
+ lines =<< trim END
+ vim9script
+
+ interface Intf
+ def F1()
+ def F2()
+ def F3()
+ endinterface
+
+ class A
+ def A1()
+ enddef
+ endclass
+
+ class B extends A
+ def B1()
+ enddef
+ def F2()
+ enddef
+ endclass
+
+ class C extends B implements Intf
+ def C1()
+ enddef
+ def F1()
+ enddef
+ endclass
+ END
+ v9.CheckSourceFailure(lines, 'E1349: Method "F3" of interface "Intf" is not implemented')
+
+ # One of the interface methods is of different type