Skip to content

Commit 3beaf9c

Browse files
committed
patch 8.2.2159: Vim9: when declaring a list it is not allocated yet
Problem: Vim9: when declaring a list it is not allocated yet, causing a following extend() to fail. Solution: When fetching a variable value for a list or dict that is null allocate the list or dict, so it can be used. (closes #7491)
1 parent 6e562fc commit 3beaf9c

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/testdir/test_vim9_assign.vim

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,16 @@ def Test_extend_list()
391391
assert_equal(['a', 'b'], list)
392392
END
393393
CheckScriptSuccess(lines)
394+
lines =<< trim END
395+
vim9script
396+
var list: list<string>
397+
def Func()
398+
extend(list, ['x', 'b'])
399+
enddef
400+
Func()
401+
assert_equal(['x', 'b'], list)
402+
END
403+
CheckScriptSuccess(lines)
394404

395405
lines =<< trim END
396406
vim9script
@@ -584,8 +594,9 @@ def Test_assignment_dict()
584594
return test
585595
enddef
586596
FillDict()
597+
assert_equal({a: 43}, test)
587598
END
588-
CheckScriptFailure(lines, 'E1103:')
599+
CheckScriptSuccess(lines)
589600

590601
# assignment to global dict
591602
lines =<< trim END

src/version.c

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

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2159,
753755
/**/
754756
2158,
755757
/**/

src/vim9execute.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,26 @@ store_var(char_u *name, typval_T *tv)
791791
restore_funccal();
792792
}
793793

794+
/*
795+
* When the value of "sv" is a null list of dict, allocate it.
796+
*/
797+
static void
798+
allocate_if_null(typval_T *tv)
799+
{
800+
switch (tv->v_type)
801+
{
802+
case VAR_LIST:
803+
if (tv->vval.v_list == NULL)
804+
rettv_list_alloc(tv);
805+
break;
806+
case VAR_DICT:
807+
if (tv->vval.v_dict == NULL)
808+
rettv_dict_alloc(tv);
809+
break;
810+
default:
811+
break;
812+
}
813+
}
794814

795815
/*
796816
* Execute a function by "name".
@@ -1289,6 +1309,7 @@ call_def_function(
12891309

12901310
sv = ((svar_T *)si->sn_var_vals.ga_data)
12911311
+ iptr->isn_arg.script.script_idx;
1312+
allocate_if_null(sv->sv_tv);
12921313
if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
12931314
goto failed;
12941315
copy_tv(sv->sv_tv, STACK_TV_BOT(0));

0 commit comments

Comments
 (0)