patch 9.0.1741: No type checking in interfaces
Problem: No type checking in interfaces
Solution: Implement member type check in vim9 interfaces
Most of the code is a small refactoring to allow the use of a where_T
for signaling the type mismatch, the type checking itself is pretty
simple.
Improve where_T error reports
Let the caller explicitly define the kind of location it's referring to
and free the WT_ARGUMENT enum from its catch-all role.
Implement type checking for interface methods
Follows closely the logic used for type-checking the members.
closes: #12844
Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: LemonBoy <thatlemon@gmail.com>
diff --git a/src/testdir/test_vim9_class.vim b/src/testdir/test_vim9_class.vim
index 684bf6d..a79ccc8 100644
--- a/src/testdir/test_vim9_class.vim
+++ b/src/testdir/test_vim9_class.vim
@@ -1576,6 +1576,61 @@
endclass
END
v9.CheckScriptFailure(lines, 'E1349:')
+
+ lines =<< trim END
+ 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.CheckScriptFailure(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
+ def IsEven(nr: number): string
+ enddef
+ endclass
+ END
+ v9.CheckScriptFailure(lines, 'E1407: Member "IsEven": type mismatch, expected func(number): bool but got func(number): string')
+
+ lines =<< trim END
+ vim9script
+
+ interface One
+ def IsEven(nr: number): bool
+ endinterface
+ class Two implements One
+ def IsEven(nr: bool): bool
+ enddef
+ endclass
+ END
+ v9.CheckScriptFailure(lines, 'E1407: Member "IsEven": type mismatch, expected func(number): bool but got func(bool): bool')
+
+ lines =<< trim END
+ vim9script
+
+ interface One
+ def IsEven(nr: number): bool
+ endinterface
+ class Two implements One
+ def IsEven(nr: number, ...extra: list<number>): bool
+ enddef
+ endclass
+ END
+ v9.CheckScriptFailure(lines, 'E1407: Member "IsEven": type mismatch, expected func(number): bool but got func(number, ...list<number>): bool')
enddef
def Test_call_interface_method()