0
0
mirror of https://github.com/vim/vim.git synced 2025-07-26 11:04:33 -04:00

patch 8.2.1949: Vim9: using extend() on null dict is silently ignored

Problem:    Vim9: using extend() on null dict is silently ignored.
Solution:   Give an error message.  Initialize a dict variable with an empty
            dictionary. (closes #7251)
This commit is contained in:
Bram Moolenaar 2020-11-04 11:36:35 +01:00
parent 4778b4d0e1
commit 348be7ed07
5 changed files with 82 additions and 7 deletions

View File

@ -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

View File

@ -2553,8 +2553,23 @@ 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;

View File

@ -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)

View File

@ -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()

View File

@ -750,6 +750,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
1949,
/**/
1948,
/**/