summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2020-10-10 15:37:58 +0200
committerBram Moolenaar <Bram@vim.org>2020-10-10 15:37:58 +0200
commit1a73923705744ab8297dd856d194e20297563456 (patch)
treeb0d7cff9a5858d931d31a38a769e313d862607f1
parent820d55a50bbc8c0ad0505b7e4302a734896b6bab (diff)
downloadvim-git-1a73923705744ab8297dd856d194e20297563456.tar.gz
patch 8.2.1821: Vim9: concatenating to a NULL list doesn't workv8.2.1821
Problem: Vim9: concatenating to a NULL list doesn't work. Solution: Handle a NULL list like an empty list. (closes #7064)
-rw-r--r--src/list.c19
-rw-r--r--src/testdir/test_vim9_assign.vim12
-rw-r--r--src/version.c2
3 files changed, 27 insertions, 6 deletions
diff --git a/src/list.c b/src/list.c
index e86ec8686..59c614ffb 100644
--- a/src/list.c
+++ b/src/list.c
@@ -824,7 +824,7 @@ f_flatten(typval_T *argvars, typval_T *rettv)
}
/*
- * Extend "l1" with "l2".
+ * Extend "l1" with "l2". "l1" must not be NULL.
* If "bef" is NULL append at the end, otherwise insert before this item.
* Returns FAIL when out of memory.
*/
@@ -832,8 +832,13 @@ f_flatten(typval_T *argvars, typval_T *rettv)
list_extend(list_T *l1, list_T *l2, listitem_T *bef)
{
listitem_T *item;
- int todo = l2->lv_len;
+ int todo;
+
+ // NULL list is equivalent to an empty list: nothing to do.
+ if (l2 == NULL || l2->lv_len == 0)
+ return OK;
+ todo = l2->lv_len;
CHECK_LIST_MATERIALIZE(l1);
CHECK_LIST_MATERIALIZE(l2);
@@ -854,15 +859,17 @@ list_concat(list_T *l1, list_T *l2, typval_T *tv)
{
list_T *l;
- if (l1 == NULL || l2 == NULL)
- return FAIL;
-
// make a copy of the first list.
- l = list_copy(l1, FALSE, 0);
+ if (l1 == NULL)
+ l = list_alloc();
+ else
+ l = list_copy(l1, FALSE, 0);
if (l == NULL)
return FAIL;
tv->v_type = VAR_LIST;
tv->vval.v_list = l;
+ if (l1 == NULL)
+ ++l->lv_refcount;
// append all items from the second list
return list_extend(l, l2, NULL);
diff --git a/src/testdir/test_vim9_assign.vim b/src/testdir/test_vim9_assign.vim
index 189ff4da0..1b56df54b 100644
--- a/src/testdir/test_vim9_assign.vim
+++ b/src/testdir/test_vim9_assign.vim
@@ -236,6 +236,18 @@ def Test_extend_list()
assert_equal(#{one: 1}, d)
END
CheckScriptSuccess(lines)
+
+ # appending to NULL list from a function
+ lines =<< trim END
+ vim9script
+ var list: list<string>
+ def Func()
+ list += ['a', 'b']
+ enddef
+ Func()
+ assert_equal(['a', 'b'], list)
+ END
+ CheckScriptSuccess(lines)
enddef
def Test_single_letter_vars()
diff --git a/src/version.c b/src/version.c
index 8e2115e5d..8dabd6059 100644
--- a/src/version.c
+++ b/src/version.c
@@ -751,6 +751,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 1821,
+/**/
1820,
/**/
1819,