Skip to content

Commit 0321e7a

Browse files
yegappanchrisbra
authored andcommitted
patch 9.2.1208: Vim9: type arguments not used when calling method through interface
Problem: Vim9: when a generic method is called with type arguments in a :def function through an interface or a parent class, the method of the object is called without the type arguments. Its type variables are then not checked, e.g. "Conv<string>()" can return a number. Solution: Keep the method with the type arguments in the ISN_METHODCALL instruction and call the method of the object with the same type arguments (Yegappan Lakshmanan). Looking up the method with the type arguments is slow, so the method found is cached in the instruction for the last object class used. fixes: #21550 closes: #21494 Assisted-by: Claude Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent d876a4c commit 0321e7a

7 files changed

Lines changed: 222 additions & 4 deletions

File tree

‎src/proto/vim9generics.pro‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ ufunc_T *eval_generic_func(ufunc_T *ufunc, char_u *name, char_u **argp);
1414
int generic_func_call(char_u **argp);
1515
void generic_func_copy_types(ufunc_T *fp, ufunc_T *new_fp);
1616
ufunc_T *generic_func_get(ufunc_T *fp, gfargs_tab_T *gfatab);
17+
ufunc_T *generic_func_get_same_types(ufunc_T *fp, ufunc_T *inst);
1718
ufunc_T *find_generic_func(ufunc_T *ufunc, char_u *name, char_u **argp);
1819
type_T *find_generic_type(char_u *gt_name, size_t name_len, ufunc_T *ufunc, cctx_T *cctx);
1920
void generic_func_clear_items(ufunc_T *fp);

‎src/testdir/test_vim9_generics.vim‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3510,6 +3510,105 @@ def Test_generic_inherited_method_type_args()
35103510
v9.CheckSourceFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got string', 1)
35113511
enddef
35123512

3513+
" Test for calling a generic method with type arguments in a def function
3514+
" through an interface or a parent class. The method of the object class is
3515+
" called with the same type arguments.
3516+
def Test_generic_method_call_through_interface()
3517+
var lines =<< trim END
3518+
vim9script
3519+
interface I
3520+
def Fn<T>(t: T): list<T>
3521+
def Conv<U>(x: any): U
3522+
endinterface
3523+
class A implements I
3524+
def Fn<T>(t: T): list<T>
3525+
return [t]
3526+
enddef
3527+
def Conv<U>(x: any): U
3528+
return x
3529+
enddef
3530+
endclass
3531+
class P
3532+
def Fn<T>(t: T): list<T>
3533+
return [t, t]
3534+
enddef
3535+
endclass
3536+
class C extends P
3537+
def Fn<X>(t: X): list<X>
3538+
return [t, t, t]
3539+
enddef
3540+
endclass
3541+
3542+
def UseI(i: I): list<any>
3543+
var l: list<string> = i.Fn<string>('a')
3544+
return [l, typename(l), i.Conv<number>(3)]
3545+
enddef
3546+
def UseP(p: P): list<any>
3547+
var l: list<number> = p.Fn<number>(1)
3548+
return [l, typename(l)]
3549+
enddef
3550+
assert_equal([['a'], 'list<string>', 3], UseI(A.new()))
3551+
assert_equal([[1, 1], 'list<number>'], UseP(P.new()))
3552+
assert_equal([[1, 1, 1], 'list<number>'], UseP(C.new()))
3553+
assert_match('METHODCALL I.Fn<string>(argc 1)',
3554+
execute('disassemble UseI'))
3555+
assert_match('METHODCALL P.Fn<number>(argc 1)',
3556+
execute('disassemble UseP'))
3557+
END
3558+
v9.CheckSourceSuccess(lines)
3559+
3560+
# The return type is checked with the type arguments of the call
3561+
lines =<< trim END
3562+
vim9script
3563+
interface I
3564+
def Conv<U>(x: any): U
3565+
endinterface
3566+
class A implements I
3567+
def Conv<U>(x: any): U
3568+
return x
3569+
enddef
3570+
endclass
3571+
def Fn(i: I): any
3572+
var s: string = i.Conv<string>(5)
3573+
return s
3574+
enddef
3575+
Fn(A.new())
3576+
END
3577+
v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected string but got number', 1)
3578+
3579+
# The same call is used with objects of different classes
3580+
lines =<< trim END
3581+
vim9script
3582+
interface I
3583+
def Fn<T>(t: T): list<T>
3584+
endinterface
3585+
class A implements I
3586+
def Fn<T>(t: T): list<T>
3587+
return [t]
3588+
enddef
3589+
endclass
3590+
class B implements I
3591+
def Fn<T>(t: T): list<T>
3592+
return [t, t]
3593+
enddef
3594+
endclass
3595+
class C extends B
3596+
endclass
3597+
def Fn(l: list<I>): list<any>
3598+
var res = []
3599+
for i in l
3600+
var r: list<string> = i.Fn<string>('a')
3601+
res->add(r)
3602+
endfor
3603+
return res
3604+
enddef
3605+
var objs: list<I> = [A.new(), A.new(), B.new(), C.new(), A.new()]
3606+
assert_equal([['a'], ['a'], ['a', 'a'], ['a', 'a'], ['a']], Fn(objs))
3607+
assert_equal([['a', 'a'], ['a']], Fn([B.new(), A.new()]))
3608+
END
3609+
v9.CheckSourceSuccess(lines)
3610+
enddef
3611+
35133612
" Test for using a generic method to initialize an object member variable
35143613
def Test_generic_method_in_object_member_init_expr()
35153614
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+
1208,
746748
/**/
747749
1207,
748750
/**/

‎src/vim9.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,16 @@ typedef struct {
250250
int cmf_idx; // index in "def_functions" for ISN_METHODCALL
251251
int cmf_argcount; // number of arguments on top of stack
252252
int cmf_is_super; // doing "super.Func", use cmf_itf, not cmf_idx
253+
ufunc_T *cmf_ufunc; // for a generic method: the method of
254+
// "cmf_itf" with the type arguments, NULL
255+
// otherwise
256+
// Cache for a generic method: "cmf_cache_ufunc" is the method of
257+
// "cmf_cache_cl" with the type arguments of "cmf_ufunc", for the generic
258+
// method "cmf_cache_generic". "cmf_cache_cl" is referenced so that the
259+
// functions are not freed.
260+
class_T *cmf_cache_cl;
261+
ufunc_T *cmf_cache_generic;
262+
ufunc_T *cmf_cache_ufunc;
253263
} cmfunc_T;
254264

255265
// arguments to ISN_PCALL

‎src/vim9execute.c‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4925,20 +4925,48 @@ exec_instructions(ectx_T *ectx)
49254925
}
49264926

49274927
ufunc_T *ufunc;
4928+
class_T *cl;
49284929
if (mfunc->cmf_is_super)
4930+
{
49294931
// Doing "super.Func", use the specific ufunc.
4930-
ufunc = mfunc->cmf_itf->class_obj_methods[
4931-
mfunc->cmf_idx];
4932+
cl = mfunc->cmf_itf;
4933+
ufunc = cl->class_obj_methods[mfunc->cmf_idx];
4934+
}
49324935
else
49334936
{
4934-
class_T *cl = obj->obj_class;
4937+
cl = obj->obj_class;
49354938

49364939
// convert the interface index to the object index
49374940
int idx = object_index_from_itf_index(mfunc->cmf_itf,
49384941
TRUE, mfunc->cmf_idx, cl);
49394942
ufunc = cl->class_obj_methods[idx];
49404943
}
49414944

4945+
// A generic method: use the method with the type arguments
4946+
// used when compiling. The last one is cached, since
4947+
// looking it up is slow.
4948+
if (mfunc->cmf_ufunc != NULL && IS_GENERIC_FUNC(ufunc))
4949+
{
4950+
if (mfunc->cmf_cache_cl == cl
4951+
&& mfunc->cmf_cache_generic == ufunc)
4952+
ufunc = mfunc->cmf_cache_ufunc;
4953+
else
4954+
{
4955+
ufunc_T *generic_ufunc = ufunc;
4956+
4957+
ufunc = generic_func_get_same_types(generic_ufunc,
4958+
mfunc->cmf_ufunc);
4959+
if (ufunc == NULL)
4960+
goto on_error;
4961+
4962+
++cl->class_refcount;
4963+
class_unref(mfunc->cmf_cache_cl);
4964+
mfunc->cmf_cache_cl = cl;
4965+
mfunc->cmf_cache_generic = generic_ufunc;
4966+
mfunc->cmf_cache_ufunc = ufunc;
4967+
}
4968+
}
4969+
49424970
if (call_ufunc(ufunc, NULL, mfunc->cmf_argcount, ectx,
49434971
NULL, NULL) == FAIL)
49444972
goto on_error;
@@ -7590,7 +7618,9 @@ list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
75907618

75917619
smsg("%s%4d METHODCALL %s.%s(argc %d)", pfx, current,
75927620
mfunc->cmf_itf->class_name.string,
7593-
mfunc->cmf_itf->class_obj_methods[
7621+
mfunc->cmf_ufunc != NULL
7622+
? mfunc->cmf_ufunc->uf_name
7623+
: mfunc->cmf_itf->class_obj_methods[
75947624
mfunc->cmf_idx]->uf_name,
75957625
mfunc->cmf_argcount);
75967626
}

‎src/vim9generics.c‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,72 @@ generic_func_get(ufunc_T *fp, gfargs_tab_T *gfatab)
11391139
return generic_fp;
11401140
}
11411141

1142+
/*
1143+
* Returns the concrete function of the generic function "fp" for the type
1144+
* arguments of "inst", a concrete function of another generic function with
1145+
* the same number of type variables. E.g. "inst" is the interface method
1146+
* "Get<string>" and "fp" the generic method "Get" of the object class. It is
1147+
* created when it does not exist yet.
1148+
* Returns NULL on failure, an error was given then (except for memory
1149+
* allocation failure).
1150+
*/
1151+
ufunc_T *
1152+
generic_func_get_same_types(ufunc_T *fp, ufunc_T *inst)
1153+
{
1154+
gfargs_tab_T gfatab;
1155+
ufunc_T *new_fp = NULL;
1156+
int argcount = inst->uf_generic_argcount;
1157+
int i;
1158+
1159+
generic_func_args_table_init(&gfatab);
1160+
1161+
if (ga_grow(&gfatab.gfat_args, argcount) == FAIL)
1162+
goto done;
1163+
for (i = 0; i < argcount; i++)
1164+
{
1165+
generic_T *generic_arg = (generic_T *)gfatab.gfat_args.ga_data + i;
1166+
type_T *type = inst->uf_generic_args[i].gt_type;
1167+
char *tofree = NULL;
1168+
1169+
generic_arg->gt_name = vim_strsave(
1170+
(char_u *)type_name(type, &tofree));
1171+
vim_free(tofree);
1172+
if (generic_arg->gt_name == NULL)
1173+
goto done;
1174+
// The types of "inst" are used for the lookup, see below.
1175+
generic_arg->gt_type = type;
1176+
gfatab.gfat_args.ga_len++;
1177+
}
1178+
1179+
// Usually the function exists, look it up first to avoid copying the
1180+
// types.
1181+
garray_T gfkey_ga;
1182+
1183+
ga_init2(&gfkey_ga, 1, 80);
1184+
new_fp = generic_lookup_func(fp, &gfatab, &gfkey_ga);
1185+
ga_clear(&gfkey_ga);
1186+
if (new_fp != NULL)
1187+
goto done;
1188+
1189+
// The new function owns the types, use a copy of the types of "inst".
1190+
for (i = 0; i < argcount; i++)
1191+
{
1192+
generic_T *generic_arg = (generic_T *)gfatab.gfat_args.ga_data + i;
1193+
type_T *type = copy_type_deep(generic_arg->gt_type,
1194+
&gfatab.gfat_arg_types);
1195+
1196+
// out of memory when the type is returned, it must not be used
1197+
if (type == generic_arg->gt_type)
1198+
goto done;
1199+
generic_arg->gt_type = type;
1200+
}
1201+
new_fp = generic_func_get(fp, &gfatab);
1202+
1203+
done:
1204+
generic_func_args_table_clear(&gfatab);
1205+
return new_fp;
1206+
}
1207+
11421208
/*
11431209
* Looks up or creates a concrete instance of a generic function "ufunc" using
11441210
* the type arguments specified after the function name in "name".

‎src/vim9instr.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,6 +2092,15 @@ generate_CALL(
20922092
isn->isn_arg.mfunc->cmf_idx = mi;
20932093
isn->isn_arg.mfunc->cmf_argcount = argcount;
20942094
isn->isn_arg.mfunc->cmf_is_super = is_super;
2095+
// For a generic method "ufunc" has the type arguments, the method of
2096+
// the object class is created with the same type arguments when
2097+
// called.
2098+
isn->isn_arg.mfunc->cmf_ufunc = NULL;
2099+
if (ufunc != cl->class_obj_methods[mi])
2100+
isn->isn_arg.mfunc->cmf_ufunc = ufunc;
2101+
isn->isn_arg.mfunc->cmf_cache_cl = NULL;
2102+
isn->isn_arg.mfunc->cmf_cache_generic = NULL;
2103+
isn->isn_arg.mfunc->cmf_cache_ufunc = NULL;
20952104
}
20962105
else if (isn->isn_type == ISN_DCALL)
20972106
{
@@ -2867,6 +2876,7 @@ delete_instr(isn_T *isn)
28672876
{
28682877
cmfunc_T *mfunc = isn->isn_arg.mfunc;
28692878
class_unref(mfunc->cmf_itf);
2879+
class_unref(mfunc->cmf_cache_cl);
28702880
vim_free(mfunc);
28712881
}
28722882
break;

0 commit comments

Comments
 (0)