forked from aniani/vim
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>
This commit is contained in:
committed by
Christian Brabandt
parent
f4ee1cb74b
commit
e467189022
@@ -7154,4 +7154,41 @@ def Test_recursive_class_method_call()
|
|||||||
v9.CheckSourceSuccess(lines)
|
v9.CheckSourceSuccess(lines)
|
||||||
enddef
|
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
|
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
|
||||||
|
@@ -704,6 +704,8 @@ static char *(features[]) =
|
|||||||
|
|
||||||
static int included_patches[] =
|
static int included_patches[] =
|
||||||
{ /* Add new patch number below this line */
|
{ /* Add new patch number below this line */
|
||||||
|
/**/
|
||||||
|
2007,
|
||||||
/**/
|
/**/
|
||||||
2006,
|
2006,
|
||||||
/**/
|
/**/
|
||||||
|
@@ -874,7 +874,6 @@ check_type_maybe(
|
|||||||
{
|
{
|
||||||
where_T func_where = where;
|
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,
|
ret = check_type_maybe(expected->tt_member,
|
||||||
actual->tt_member, FALSE,
|
actual->tt_member, FALSE,
|
||||||
@@ -898,7 +897,6 @@ check_type_maybe(
|
|||||||
&& i < actual->tt_argcount; ++i)
|
&& i < actual->tt_argcount; ++i)
|
||||||
{
|
{
|
||||||
where_T func_where = where;
|
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.
|
// Allow for using "any" argument type, lambda's have them.
|
||||||
|
Reference in New Issue
Block a user