patch 9.0.2007: Vim9: covariant parameter types allowed
Problem: Vim9: covariant parameter types allowed when assigning
functions
Solution: Enforce invariant type check for arguments and return value
when assigning a funcref
closes: #13299
closes: #13305
Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
diff --git a/src/testdir/test_vim9_class.vim b/src/testdir/test_vim9_class.vim
index c911206..ce7d5f7 100644
--- a/src/testdir/test_vim9_class.vim
+++ b/src/testdir/test_vim9_class.vim
@@ -7154,4 +7154,41 @@
v9.CheckSourceSuccess(lines)
enddef
+" Test for checking the argument types and the return type when assigning a
+" funcref to make sure the invariant class type is used.
+def Test_funcref_argtype_returntype_check()
+ var lines =<< trim END
+ vim9script
+ class A
+ endclass
+ class B extends A
+ endclass
+
+ def Foo(p: B): B
+ return B.new()
+ enddef
+
+ var Bar: func(A): A = Foo
+ END
+ v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected func(object<A>): object<A> but got func(object<B>): object<B>', 11)
+
+ lines =<< trim END
+ vim9script
+ class A
+ endclass
+ class B extends A
+ endclass
+
+ def Foo(p: B): B
+ return B.new()
+ enddef
+
+ def Baz()
+ var Bar: func(A): A = Foo
+ enddef
+ Baz()
+ END
+ v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected func(object<A>): object<A> but got func(object<B>): object<B>', 1)
+enddef
+
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker