diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/errors.h | 4 | ||||
-rw-r--r-- | src/evalvars.c | 15 | ||||
-rw-r--r-- | src/list.c | 16 | ||||
-rw-r--r-- | src/testdir/test_vim9_assign.vim | 52 | ||||
-rw-r--r-- | src/version.c | 2 |
5 files changed, 82 insertions, 7 deletions
diff --git a/src/errors.h b/src/errors.h index 0e0377a0e7..eec45dff2a 100644 --- a/src/errors.h +++ b/src/errors.h @@ -288,4 +288,8 @@ EXTERN char e_cannot_add_to_null_blob[] INIT(= N_("E1131: Cannot add to null blob")); EXTERN char e_missing_function_argument[] INIT(= N_("E1132: Missing function argument")); +EXTERN char e_cannot_extend_null_dict[] + INIT(= N_("E1133: Cannot extend a null dict")); +EXTERN char e_cannot_extend_null_list[] + INIT(= N_("E1134: Cannot extend a null list")); #endif diff --git a/src/evalvars.c b/src/evalvars.c index a5db6e9523..9c05c57a01 100644 --- a/src/evalvars.c +++ b/src/evalvars.c @@ -2553,7 +2553,22 @@ eval_variable( ret = FAIL; } else if (rettv != NULL) + { + // If a list or dict variable wasn't initialized, do it now. + if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL) + { + tv->vval.v_dict = dict_alloc(); + if (tv->vval.v_dict != NULL) + ++tv->vval.v_dict->dv_refcount; + } + else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL) + { + tv->vval.v_list = list_alloc(); + if (tv->vval.v_list != NULL) + ++tv->vval.v_list->lv_refcount; + } copy_tv(tv, rettv); + } } name[len] = cc; diff --git a/src/list.c b/src/list.c index beea6fae61..af12aea79d 100644 --- a/src/list.c +++ b/src/list.c @@ -2303,9 +2303,13 @@ f_extend(typval_T *argvars, typval_T *rettv) int error = FALSE; l1 = argvars[0].vval.v_list; + if (l1 == NULL) + { + emsg(_(e_cannot_extend_null_list)); + return; + } l2 = argvars[1].vval.v_list; - if (l1 != NULL && !value_check_lock(l1->lv_lock, arg_errmsg, TRUE) - && l2 != NULL) + if (!value_check_lock(l1->lv_lock, arg_errmsg, TRUE) && l2 != NULL) { if (argvars[2].v_type != VAR_UNKNOWN) { @@ -2339,9 +2343,13 @@ f_extend(typval_T *argvars, typval_T *rettv) int i; d1 = argvars[0].vval.v_dict; + if (d1 == NULL) + { + emsg(_(e_cannot_extend_null_dict)); + return; + } d2 = argvars[1].vval.v_dict; - if (d1 != NULL && !value_check_lock(d1->dv_lock, arg_errmsg, TRUE) - && d2 != NULL) + if (!value_check_lock(d1->dv_lock, arg_errmsg, TRUE) && d2 != NULL) { // Check the third argument. if (argvars[2].v_type != VAR_UNKNOWN) diff --git a/src/testdir/test_vim9_assign.vim b/src/testdir/test_vim9_assign.vim index 4ecfdee4fd..24f62d85d2 100644 --- a/src/testdir/test_vim9_assign.vim +++ b/src/testdir/test_vim9_assign.vim @@ -231,10 +231,14 @@ def Test_extend_list() var l: list<number> l += [123] assert_equal([123], l) + END + CheckScriptSuccess(lines) - var d: dict<number> - d['one'] = 1 - assert_equal(#{one: 1}, d) + lines =<< trim END + vim9script + var list: list<string> + extend(list, ['x']) + assert_equal(['x'], list) END CheckScriptSuccess(lines) @@ -249,6 +253,48 @@ def Test_extend_list() assert_equal(['a', 'b'], list) END CheckScriptSuccess(lines) + + lines =<< trim END + vim9script + var l: list<string> = test_null_list() + extend(l, ['x']) + assert_equal(['x'], l) + END + CheckScriptSuccess(lines) + + lines =<< trim END + vim9script + extend(test_null_list(), ['x']) + END + CheckScriptFailure(lines, 'E1134:', 2) +enddef + +def Test_extend_dict() + var lines =<< trim END + vim9script + var d: dict<number> + extend(d, #{a: 1}) + assert_equal(#{a: 1}, d) + + var d2: dict<number> + d2['one'] = 1 + assert_equal(#{one: 1}, d2) + END + CheckScriptSuccess(lines) + + lines =<< trim END + vim9script + var d: dict<string> = test_null_dict() + extend(d, #{a: 'x'}) + assert_equal(#{a: 'x'}, d) + END + CheckScriptSuccess(lines) + + lines =<< trim END + vim9script + extend(test_null_dict(), #{a: 'x'}) + END + CheckScriptFailure(lines, 'E1133:', 2) enddef def Test_single_letter_vars() diff --git a/src/version.c b/src/version.c index d380452c40..112077a98c 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 */ /**/ + 1949, +/**/ 1948, /**/ 1947, |