Skip to content

Commit 7398f36

Browse files
yegappanchrisbra
authored andcommitted
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 <[email protected]> Co-authored-by: Yegappan Lakshmanan <[email protected]>
1 parent 960822a commit 7398f36

3 files changed

Lines changed: 41 additions & 9 deletions

File tree

src/testdir/test_vim9_class.vim

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5675,4 +5675,34 @@ def Test_dict_object_member()
56755675
v9.CheckSourceSuccess(lines)
56765676
enddef
56775677

5678+
" The following test was failing after 9.0.1914. This was caused by using a
5679+
" freed object from a previous method call.
5680+
def Test_freed_object_from_previous_method_call()
5681+
var lines =<< trim END
5682+
vim9script
5683+
5684+
class Context
5685+
endclass
5686+
5687+
class Result
5688+
endclass
5689+
5690+
def Failure(): Result
5691+
return Result.new()
5692+
enddef
5693+
5694+
def GetResult(ctx: Context): Result
5695+
return Failure()
5696+
enddef
5697+
5698+
def Test_GetResult()
5699+
var ctx = Context.new()
5700+
var result = GetResult(ctx)
5701+
enddef
5702+
5703+
Test_GetResult()
5704+
END
5705+
v9.CheckSourceSuccess(lines)
5706+
enddef
5707+
56785708
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,8 @@ static char *(features[]) =
699699

700700
static int included_patches[] =
701701
{ /* Add new patch number below this line */
702+
/**/
703+
1932,
702704
/**/
703705
1931,
704706
/**/

src/vim9execute.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -535,15 +535,6 @@ call_dfunc(
535535
// If this is an object method, the object is just before the arguments.
536536
typval_T *obj = STACK_TV_BOT(0) - argcount - vararg_count - 1;
537537

538-
if (obj->v_type == VAR_OBJECT && obj->vval.v_object == NULL
539-
&& !IS_CONSTRUCTOR_METHOD(ufunc))
540-
{
541-
// If this is not the constructor method, then a valid object is
542-
// needed.
543-
emsg(_(e_using_null_object));
544-
return FAIL;
545-
}
546-
547538
// Check the argument types.
548539
if (check_ufunc_arg_types(ufunc, argcount, vararg_count, ectx) == FAIL)
549540
return FAIL;
@@ -610,6 +601,15 @@ call_dfunc(
610601
// the first local variable.
611602
if (IS_OBJECT_METHOD(ufunc))
612603
{
604+
if (obj->v_type == VAR_OBJECT && obj->vval.v_object == NULL
605+
&& !IS_CONSTRUCTOR_METHOD(ufunc))
606+
{
607+
// If this is not a constructor method, then a valid object is
608+
// needed.
609+
emsg(_(e_using_null_object));
610+
return FAIL;
611+
}
612+
613613
*STACK_TV_VAR(0) = *obj;
614614
obj->v_type = VAR_UNKNOWN;
615615
}

0 commit comments

Comments
 (0)