diff src/vim9cmds.c @ 32903:54c01bb98b8e v9.0.1760

patch 9.0.1760: vim9 class problem with new() constructor Commit: https://github.com/vim/vim/commit/6ac1544e13d78e0deeca1f357d1d73947180171c Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Sun Aug 20 18:20:17 2023 +0200 patch 9.0.1760: vim9 class problem with new() constructor Problem: vim9 class problem with new() constructor Solution: Don't allow a return type for the new() class constructor. closes: #12863 closes: #12040 Signed-off-by: Christian Brabandt <cb@256bit.org> Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
author Christian Brabandt <cb@256bit.org>
date Sun, 20 Aug 2023 18:30:05 +0200
parents a39314fa9495
children 6340c608ca54
line wrap: on
line diff
--- a/src/vim9cmds.c
+++ b/src/vim9cmds.c
@@ -2617,8 +2617,16 @@ compile_return(char_u *arg, int check_re
 	    return NULL;
 	}
 
-	// No argument, return zero.
-	generate_PUSHNR(cctx, 0);
+	if (cctx->ctx_ufunc->uf_flags & FC_NEW)
+	{
+	    // For a class new() constructor, return an object of the class.
+	    generate_instr(cctx, ISN_RETURN_OBJECT);
+	    cctx->ctx_ufunc->uf_ret_type =
+		&cctx->ctx_ufunc->uf_class->class_object_type;
+	}
+	else
+	    // No argument, return zero.
+	    generate_PUSHNR(cctx, 0);
     }
 
     // may need ENDLOOP when inside a :for or :while loop