Skip to content

Commit 0780204

Browse files
committed
patch 8.2.3423: Vim9: list += list creates a new list in :def function
Problem: Vim9: list += list creates a new list in :def function. Solution: Append to the existing list.
1 parent efc084e commit 0780204

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed

src/structs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4106,6 +4106,9 @@ typedef enum
41064106
EXPR_MULT, // *
41074107
EXPR_DIV, // /
41084108
EXPR_REM, // %
4109+
// used with ISN_ADDLIST
4110+
EXPR_COPY, // create new list
4111+
EXPR_APPEND, // append to first list
41094112
} exprtype_T;
41104113

41114114
/*

src/testdir/test_vim9_assign.vim

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -557,20 +557,21 @@ enddef
557557

558558
def Test_extend_list()
559559
var lines =<< trim END
560-
vim9script
561-
var l: list<number>
562-
l += [123]
563-
assert_equal([123], l)
560+
var l1: list<number>
561+
var l2 = l1
562+
assert_true(l1 is l2)
563+
l1 += [123]
564+
assert_equal([123], l1)
565+
assert_true(l1 is l2)
564566
END
565-
CheckScriptSuccess(lines)
567+
CheckDefAndScriptSuccess(lines)
566568

567569
lines =<< trim END
568-
vim9script
569570
var list: list<string>
570571
extend(list, ['x'])
571572
assert_equal(['x'], list)
572573
END
573-
CheckScriptSuccess(lines)
574+
CheckDefAndScriptSuccess(lines)
574575

575576
# appending to NULL list from a function
576577
lines =<< trim END

src/version.c

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

756756
static int included_patches[] =
757757
{ /* Add new patch number below this line */
758+
/**/
759+
3423,
758760
/**/
759761
3422,
760762
/**/

src/vim9compile.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -690,12 +690,16 @@ check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
690690
return OK;
691691
}
692692

693+
/*
694+
* Generate instruction for "+". For a list this creates a new list.
695+
*/
693696
static int
694697
generate_add_instr(
695698
cctx_T *cctx,
696699
vartype_T vartype,
697700
type_T *type1,
698-
type_T *type2)
701+
type_T *type2,
702+
exprtype_T expr_type)
699703
{
700704
garray_T *stack = &cctx->ctx_type_stack;
701705
isn_T *isn = generate_instr_drop(cctx,
@@ -715,7 +719,12 @@ generate_add_instr(
715719
return FAIL;
716720

717721
if (isn != NULL)
718-
isn->isn_arg.op.op_type = EXPR_ADD;
722+
{
723+
if (isn->isn_type == ISN_ADDLIST)
724+
isn->isn_arg.op.op_type = expr_type;
725+
else
726+
isn->isn_arg.op.op_type = EXPR_ADD;
727+
}
719728

720729
// When concatenating two lists with different member types the member type
721730
// becomes "any".
@@ -769,7 +778,8 @@ generate_two_op(cctx_T *cctx, char_u *op)
769778
switch (*op)
770779
{
771780
case '+':
772-
if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
781+
if (generate_add_instr(cctx, vartype, type1, type2,
782+
EXPR_COPY) == FAIL)
773783
return FAIL;
774784
break;
775785

@@ -7186,7 +7196,8 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
71867196
{
71877197
if (generate_add_instr(cctx,
71887198
operator_type(lhs.lhs_member_type, stacktype),
7189-
lhs.lhs_member_type, stacktype) == FAIL)
7199+
lhs.lhs_member_type, stacktype,
7200+
EXPR_APPEND) == FAIL)
71907201
goto theend;
71917202
}
71927203
else if (generate_two_op(cctx, op) == FAIL)

src/vim9execute.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3677,7 +3677,14 @@ exec_instructions(ectx_T *ectx)
36773677

36783678
// add two lists or blobs
36793679
if (iptr->isn_type == ISN_ADDLIST)
3680-
eval_addlist(tv1, tv2);
3680+
{
3681+
if (iptr->isn_arg.op.op_type == EXPR_APPEND
3682+
&& tv1->vval.v_list != NULL)
3683+
list_extend(tv1->vval.v_list, tv2->vval.v_list,
3684+
NULL);
3685+
else
3686+
eval_addlist(tv1, tv2);
3687+
}
36813688
else
36823689
eval_addblob(tv1, tv2);
36833690
clear_tv(tv2);

0 commit comments

Comments
 (0)