Skip to content

Commit eaef676

Browse files
yegappanchrisbra
authored andcommitted
patch 9.2.1209: Vim9: number of type variables of interface method not checked
Problem: Vim9: when a class implements a generic method of an interface, the number of type variables is not checked, unlike when a method is overridden in a child class. Solution: Give an error when the number of type variables differs (Yegappan Lakshmanan). fixes: #21564 closes: #21495 Assisted-by: Claude Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 0321e7a commit eaef676

7 files changed

Lines changed: 133 additions & 11 deletions

File tree

‎runtime/doc/tags‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4694,6 +4694,7 @@ E1434 vim9.txt /*E1434*
46944694
E1435 vim9class.txt /*E1435*
46954695
E1436 vim9class.txt /*E1436*
46964696
E1437 eval.txt /*E1437*
4697+
E1438 vim9.txt /*E1438*
46974698
E144 various.txt /*E144*
46984699
E145 starting.txt /*E145*
46994700
E146 change.txt /*E146*

‎runtime/doc/vim9.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5373,6 +5373,10 @@ A generic class method in a base class can be overridden by a generic method
53735373
in a child class. The number of type variables must match between both
53745374
methods. A concrete class method cannot be overridden by a generic method,
53755375
and vice versa.
5376+
*E1438*
5377+
A method that implements an interface method must have the same number of
5378+
type variables as the interface method. A generic method cannot implement a
5379+
regular method, and vice versa.
53765380

53775381

53785382
Generic function reference~

‎src/errors.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3629,8 +3629,10 @@ EXTERN char e_interface_can_only_be_used_in_script[]
36293629
INIT(= N_("E1436: Interface can only be used in a script"));
36303630
EXTERN char e_can_only_compare_object_with_object[]
36313631
INIT(= N_("E1437: Can only compare Object with Object"));
3632+
EXTERN char e_type_var_count_of_method_str_differs_from_interface_str[]
3633+
INIT(= N_("E1438: Number of type variables of method \"%s\" differs from interface \"%s\""));
36323634
#endif
3633-
// E1438 - E1499 unused (reserved for Vim9 class support)
3635+
// E1439 - E1499 unused (reserved for Vim9 class support)
36343636
EXTERN char e_cannot_mix_positional_and_non_positional_str[]
36353637
INIT(= N_("E1500: Cannot mix positional and non-positional arguments: %s"));
36363638
EXTERN char e_fmt_arg_nr_unused_str[]

‎src/po/vim.pot‎

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/testdir/test_vim9_generics.vim‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3609,6 +3609,98 @@ def Test_generic_method_call_through_interface()
36093609
v9.CheckSourceSuccess(lines)
36103610
enddef
36113611

3612+
" Test for implementing an interface generic method with a method that has a
3613+
" different number of type variables
3614+
def Test_generic_interface_method_type_var_count()
3615+
var lines =<< trim END
3616+
vim9script
3617+
3618+
interface I
3619+
def Fn<T>(t: T): T
3620+
endinterface
3621+
3622+
class A implements I
3623+
def Fn(t: number): number
3624+
return t
3625+
enddef
3626+
endclass
3627+
END
3628+
v9.CheckSourceFailure(lines, 'E1438: Number of type variables of method "Fn" differs from interface "I"', 11)
3629+
3630+
lines =<< trim END
3631+
vim9script
3632+
3633+
interface I
3634+
def Fn(t: number): number
3635+
endinterface
3636+
3637+
class A implements I
3638+
def Fn<T>(t: T): T
3639+
return t
3640+
enddef
3641+
endclass
3642+
END
3643+
v9.CheckSourceFailure(lines, 'E1438: Number of type variables of method "Fn" differs from interface "I"', 11)
3644+
3645+
lines =<< trim END
3646+
vim9script
3647+
3648+
interface I
3649+
def Fn<T>(t: T): T
3650+
endinterface
3651+
3652+
class A implements I
3653+
def Fn<X, Y>(t: X): X
3654+
return t
3655+
enddef
3656+
endclass
3657+
END
3658+
v9.CheckSourceFailure(lines, 'E1438: Number of type variables of method "Fn" differs from interface "I"', 11)
3659+
3660+
# The method is inherited from the parent class
3661+
lines =<< trim END
3662+
vim9script
3663+
3664+
interface I
3665+
def Fn<T>(t: T): T
3666+
endinterface
3667+
3668+
class A
3669+
def Fn<X, Y>(t: X): X
3670+
return t
3671+
enddef
3672+
endclass
3673+
3674+
class B extends A implements I
3675+
endclass
3676+
END
3677+
v9.CheckSourceFailure(lines, 'E1438: Number of type variables of method "Fn" differs from interface "I"', 14)
3678+
3679+
# Same number of type variables with different names
3680+
lines =<< trim END
3681+
vim9script
3682+
3683+
interface I
3684+
def Fn<T>(t: T): T
3685+
def Fn2<A, B>(a: A, b: B): B
3686+
endinterface
3687+
3688+
class A implements I
3689+
def Fn<U>(t: U): U
3690+
return t
3691+
enddef
3692+
def Fn2<X, Y>(a: X, b: Y): Y
3693+
return b
3694+
enddef
3695+
endclass
3696+
3697+
var i: I = A.new()
3698+
assert_equal(3, i.Fn<number>(3))
3699+
assert_equal('b', i.Fn2<number, string>(1, 'b'))
3700+
END
3701+
v9.CheckSourceSuccess(lines)
3702+
enddef
3703+
36123704
" Test for using a generic method to initialize an object member variable
36133705
def Test_generic_method_in_object_member_init_expr()
36143706
var lines =<< trim END

‎src/version.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,8 @@ static char *(features[]) =
743743

744744
static int included_patches[] =
745745
{ /* Add new patch number below this line */
746+
/**/
747+
1209,
746748
/**/
747749
1208,
748750
/**/

‎src/vim9class.c‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -733,13 +733,26 @@ validate_interface_variables(
733733
}
734734

735735
/*
736-
* Returns TRUE if the method signature of "if_method" and "cl_method" matches.
736+
* Returns TRUE if the method signature of "if_method" of interface
737+
* "intf_name" and "cl_method" matches.
737738
*/
738739
static int
739-
intf_method_type_matches(ufunc_T *if_method, ufunc_T *cl_method)
740+
intf_method_type_matches(
741+
ufunc_T *if_method,
742+
ufunc_T *cl_method,
743+
char_u *intf_name)
740744
{
741745
where_T where = WHERE_INIT;
742746

747+
// A generic method must have the same number of type variables, like
748+
// when overriding a method, see validate_extends_generic_method().
749+
if (cl_method->uf_generic_argcount != if_method->uf_generic_argcount)
750+
{
751+
semsg(_(e_type_var_count_of_method_str_differs_from_interface_str),
752+
cl_method->uf_name, intf_name);
753+
return FALSE;
754+
}
755+
743756
// Ensure the type is matching.
744757
where.wt_func_name = (char *)if_method->uf_name;
745758
where.wt_kind = WT_METHOD;
@@ -751,16 +764,17 @@ intf_method_type_matches(ufunc_T *if_method, ufunc_T *cl_method)
751764
}
752765

753766
/*
754-
* Returns TRUE if the interface method "if_ufunc" is present in the list of
755-
* methods in "cl_fp" or in the parent lineage of one of the extended classes
756-
* in "extends_cl". For a class method, 'is_class_method' is TRUE.
767+
* Returns TRUE if the method "if_ufunc" of interface "intf_name" is present in
768+
* the list of methods in "cl_fp" or in the parent lineage of one of the
769+
* extended classes in "extends_cl".
757770
*/
758771
static int
759772
intf_method_present(
760773
ufunc_T *if_ufunc,
761774
ufunc_T **cl_fp,
762775
int cl_count,
763-
class_T *extends_cl)
776+
class_T *extends_cl,
777+
char_u *intf_name)
764778
{
765779
int method_present = FALSE;
766780

@@ -769,7 +783,7 @@ intf_method_present(
769783
if (STRCMP(if_ufunc->uf_name, cl_fp[cl_i]->uf_name) == 0)
770784
{
771785
// Ensure the type is matching.
772-
if (!intf_method_type_matches(if_ufunc, cl_fp[cl_i]))
786+
if (!intf_method_type_matches(if_ufunc, cl_fp[cl_i], intf_name))
773787
return FALSE;
774788
method_present = TRUE;
775789
break;
@@ -781,7 +795,7 @@ intf_method_present(
781795
ufunc_T **ext_cl_fp = (ufunc_T **)(extends_cl->class_obj_methods);
782796
int ext_cl_count = extends_cl->class_obj_method_count;
783797
return intf_method_present(if_ufunc, ext_cl_fp, ext_cl_count,
784-
extends_cl->class_extends);
798+
extends_cl->class_extends, intf_name);
785799
}
786800

787801
return method_present;
@@ -813,7 +827,8 @@ validate_interface_methods(
813827
{
814828
char_u *if_name = if_fp[if_i]->uf_name;
815829

816-
if (!intf_method_present(if_fp[if_i], cl_fp, cl_count, extends_cl))
830+
if (!intf_method_present(if_fp[if_i], cl_fp, cl_count, extends_cl,
831+
intf_class_name))
817832
{
818833
semsg(_(e_method_str_of_interface_str_not_implemented),
819834
if_name, intf_class_name);

0 commit comments

Comments
 (0)