From: Yegappan Lakshmanan Date: Mon, 9 Oct 2023 16:01:06 +0000 (+0200) Subject: patch 9.0.2007: Vim9: covariant parameter types allowed X-Git-Tag: v9.0.2007^0 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=e4671890220ef3f2bca43fde6ffe5d3ef3ed0e42;p=thirdparty%2Fvim.git 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 Co-authored-by: Yegappan Lakshmanan --- diff --git a/src/testdir/test_vim9_class.vim b/src/testdir/test_vim9_class.vim index c911206c9f..ce7d5f7faa 100644 --- a/src/testdir/test_vim9_class.vim +++ b/src/testdir/test_vim9_class.vim @@ -7154,4 +7154,41 @@ def Test_recursive_class_method_call() 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): object but got func(object): object', 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): object but got func(object): object', 1) +enddef + " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker diff --git a/src/version.c b/src/version.c index 08c34e1894..489ecb871e 100644 --- a/src/version.c +++ b/src/version.c @@ -704,6 +704,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ +/**/ + 2007, /**/ 2006, /**/ diff --git a/src/vim9type.c b/src/vim9type.c index de1033c2e9..338aee14a7 100644 --- a/src/vim9type.c +++ b/src/vim9type.c @@ -874,8 +874,7 @@ check_type_maybe( { where_T func_where = where; - if (where.wt_kind == WT_METHOD) - func_where.wt_kind = WT_METHOD_RETURN; + func_where.wt_kind = WT_METHOD_RETURN; ret = check_type_maybe(expected->tt_member, actual->tt_member, FALSE, func_where); @@ -898,8 +897,7 @@ check_type_maybe( && i < actual->tt_argcount; ++i) { where_T func_where = where; - if (where.wt_kind == WT_METHOD) - func_where.wt_kind = WT_METHOD_ARG; + func_where.wt_kind = WT_METHOD_ARG; // Allow for using "any" argument type, lambda's have them. if (actual->tt_args[i] != &t_any && check_type(