changeset 33337:19b4f85c2649 v9.0.1932

patch 9.0.1932: Vim9: error when using null object constructor Commit: https://github.com/vim/vim/commit/7398f367d5125eedfb4058c63a5d167fe8601e3d Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Sun Sep 24 23:09:10 2023 +0200 patch 9.0.1932: Vim9: error when using null object constructor Problem: Vim9: error when using null object constructor Solution: Check for a null object only when calling an object method closes: #13154 closes: #13163 Signed-off-by: Christian Brabandt <cb@256bit.org> Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
author Christian Brabandt <cb@256bit.org>
date Sun, 24 Sep 2023 23:15:07 +0200
parents 4541997c3638
children 9e4fde36162a
files src/testdir/test_vim9_class.vim src/version.c src/vim9execute.c
diffstat 3 files changed, 41 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/testdir/test_vim9_class.vim
+++ b/src/testdir/test_vim9_class.vim
@@ -5675,4 +5675,34 @@ def Test_dict_object_member()
   v9.CheckSourceSuccess(lines)
 enddef
 
+" The following test was failing after 9.0.1914.  This was caused by using a
+" freed object from a previous method call.
+def Test_freed_object_from_previous_method_call()
+  var lines =<< trim END
+    vim9script
+
+    class Context
+    endclass
+
+    class Result
+    endclass
+
+    def Failure(): Result
+      return Result.new()
+    enddef
+
+    def GetResult(ctx: Context): Result
+      return Failure()
+    enddef
+
+    def Test_GetResult()
+      var ctx = Context.new()
+      var result = GetResult(ctx)
+    enddef
+
+    Test_GetResult()
+  END
+  v9.CheckSourceSuccess(lines)
+enddef
+
 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
--- a/src/version.c
+++ b/src/version.c
@@ -700,6 +700,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1932,
+/**/
     1931,
 /**/
     1930,
--- a/src/vim9execute.c
+++ b/src/vim9execute.c
@@ -535,15 +535,6 @@ call_dfunc(
     // If this is an object method, the object is just before the arguments.
     typval_T	*obj = STACK_TV_BOT(0) - argcount - vararg_count - 1;
 
-    if (obj->v_type == VAR_OBJECT && obj->vval.v_object == NULL
-					&& !IS_CONSTRUCTOR_METHOD(ufunc))
-    {
-	// If this is not the constructor method, then a valid object is
-	// needed.
-	emsg(_(e_using_null_object));
-	return FAIL;
-    }
-
     // Check the argument types.
     if (check_ufunc_arg_types(ufunc, argcount, vararg_count, ectx) == FAIL)
 	return FAIL;
@@ -610,6 +601,15 @@ call_dfunc(
     // the first local variable.
     if (IS_OBJECT_METHOD(ufunc))
     {
+	if (obj->v_type == VAR_OBJECT && obj->vval.v_object == NULL
+					    && !IS_CONSTRUCTOR_METHOD(ufunc))
+	{
+	    // If this is not a constructor method, then a valid object is
+	    // needed.
+	    emsg(_(e_using_null_object));
+	    return FAIL;
+	}
+
 	*STACK_TV_VAR(0) = *obj;
 	obj->v_type = VAR_UNKNOWN;
     }