patch 9.0.1724: vim9class constructor argument type checking bug
Problem: vim9class constructor argument type checking bug
Solution: fix it
closes: #12816
Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: h-east <h.east.727@gmail.com>
diff --git a/src/vim9instr.c b/src/vim9instr.c
index ed99cb3..941adf8 100644
--- a/src/vim9instr.c
+++ b/src/vim9instr.c
@@ -1780,6 +1780,7 @@
ufunc_T *ufunc,
class_T *cl,
int mi,
+ type_T *mtype, // method type
int pushed_argcount)
{
isn_T *isn;
@@ -1805,6 +1806,8 @@
{
int i;
compiletype_T compile_type;
+ int class_constructor = (mtype->tt_type == VAR_CLASS
+ && STRNCMP(ufunc->uf_name, "new", 3) == 0);
for (i = 0; i < argcount; ++i)
{
@@ -1823,6 +1826,25 @@
if (ufunc->uf_arg_types == NULL)
continue;
expected = ufunc->uf_arg_types[i];
+
+ // When the method is a class constructor and the formal
+ // argument is an object member, the type check is performed on
+ // the object member type.
+ if (class_constructor && expected->tt_type == VAR_ANY)
+ {
+ class_T *clp = mtype->tt_class;
+ char_u *aname = ((char_u **)ufunc->uf_args.ga_data)[i];
+ for (int om = 0; om < clp->class_obj_member_count; ++om)
+ {
+ if (STRCMP(aname, clp->class_obj_members[om].ocm_name)
+ == 0)
+ {
+ expected = clp->class_obj_members[om].ocm_type;
+ break;
+ }
+ }
+
+ }
}
else if (ufunc->uf_va_type == NULL
|| ufunc->uf_va_type == &t_list_any)