diff src/vim9instr.c @ 32822:b3a42579bb3f v9.0.1724

patch 9.0.1724: vim9class constructor argument type checking bug Commit: https://github.com/vim/vim/commit/2261c89a49ff2115e1ccc9ab9211e9f0d5a37578 Author: h-east <h.east.727@gmail.com> Date: Wed Aug 16 21:49:54 2023 +0900 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>
author Christian Brabandt <cb@256bit.org>
date Thu, 17 Aug 2023 22:45:03 +0200
parents 695b50472e85
children ba1b40b520e8
line wrap: on
line diff
--- a/src/vim9instr.c
+++ b/src/vim9instr.c
@@ -1780,6 +1780,7 @@ generate_CALL(
 	ufunc_T	    *ufunc,
 	class_T	    *cl,
 	int	    mi,
+	type_T	    *mtype,	// method type
 	int	    pushed_argcount)
 {
     isn_T	*isn;
@@ -1805,6 +1806,8 @@ generate_CALL(
     {
 	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 @@ generate_CALL(
 		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)