mirror of
https://github.com/neovim/neovim.git
synced 2025-09-05 19:08:15 +00:00
Merge pull request #20955 from zeertzjq/vim-8.2.2918
vim-patch:8.2.{2918,2920,2921},9.0.0836: variable can shadow builtin function
This commit is contained in:
@@ -1760,6 +1760,9 @@ void set_var_lval(lval_T *lp, char *endp, typval_T *rettv, int copy, const bool
|
||||
semsg(_(e_dictkey), lp->ll_newkey);
|
||||
return;
|
||||
}
|
||||
if (tv_dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv, lp->ll_newkey)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Need to add an item to the Dictionary.
|
||||
di = tv_dict_item_alloc((const char *)lp->ll_newkey);
|
||||
|
@@ -2206,6 +2206,15 @@ bool tv_dict_get_callback(dict_T *const d, const char *const key, const ptrdiff_
|
||||
return res;
|
||||
}
|
||||
|
||||
/// Check for adding a function to g: or l:.
|
||||
/// If the name is wrong give an error message and return true.
|
||||
int tv_dict_wrong_func_name(dict_T *d, typval_T *tv, const char *name)
|
||||
{
|
||||
return (d == &globvardict || &d->dv_hashtab == get_funccal_local_ht())
|
||||
&& tv_is_func(*tv)
|
||||
&& var_wrong_func_name(name, true);
|
||||
}
|
||||
|
||||
//{{{2 dict_add*
|
||||
|
||||
/// Add item to dictionary
|
||||
@@ -2217,6 +2226,9 @@ bool tv_dict_get_callback(dict_T *const d, const char *const key, const ptrdiff_
|
||||
int tv_dict_add(dict_T *const d, dictitem_T *const item)
|
||||
FUNC_ATTR_NONNULL_ALL
|
||||
{
|
||||
if (tv_dict_wrong_func_name(d, &item->di_tv, (const char *)item->di_key)) {
|
||||
return FAIL;
|
||||
}
|
||||
return hash_add(&d->dv_hashtab, item->di_key);
|
||||
}
|
||||
|
||||
@@ -2447,17 +2459,9 @@ void tv_dict_extend(dict_T *const d1, dict_T *const d2, const char *const action
|
||||
|
||||
TV_DICT_ITER(d2, di2, {
|
||||
dictitem_T *const di1 = tv_dict_find(d1, (const char *)di2->di_key, -1);
|
||||
if (d1->dv_scope != VAR_NO_SCOPE) {
|
||||
// Disallow replacing a builtin function in l: and g:.
|
||||
// Check the key to be valid when adding to any scope.
|
||||
if (d1->dv_scope == VAR_DEF_SCOPE
|
||||
&& tv_is_func(di2->di_tv)
|
||||
&& var_wrong_func_name((const char *)di2->di_key, di1 == NULL)) {
|
||||
break;
|
||||
}
|
||||
if (!valid_varname((const char *)di2->di_key)) {
|
||||
break;
|
||||
}
|
||||
// Check the key to be valid when adding to any scope.
|
||||
if (d1->dv_scope != VAR_NO_SCOPE && !valid_varname((const char *)di2->di_key)) {
|
||||
break;
|
||||
}
|
||||
if (di1 == NULL) {
|
||||
dictitem_T *const new_di = tv_dict_item_copy(di2);
|
||||
@@ -2477,6 +2481,10 @@ void tv_dict_extend(dict_T *const d1, dict_T *const d2, const char *const action
|
||||
|| var_check_ro(di1->di_flags, arg_errmsg, arg_errmsg_len)) {
|
||||
break;
|
||||
}
|
||||
// Disallow replacing a builtin function.
|
||||
if (tv_dict_wrong_func_name(d1, &di2->di_tv, (const char *)di2->di_key)) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (watched) {
|
||||
tv_copy(&di1->di_tv, &oldtv);
|
||||
|
@@ -589,7 +589,7 @@ static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr)
|
||||
STRCPY(v->di_key, name);
|
||||
#endif
|
||||
v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
|
||||
tv_dict_add(dp, v);
|
||||
hash_add(&dp->dv_hashtab, v->di_key);
|
||||
v->di_tv.v_type = VAR_NUMBER;
|
||||
v->di_tv.v_lock = VAR_FIXED;
|
||||
v->di_tv.vval.v_number = nr;
|
||||
@@ -885,7 +885,7 @@ void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rett
|
||||
STRCPY(name, "self");
|
||||
#endif
|
||||
v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
|
||||
tv_dict_add(&fc->l_vars, v);
|
||||
hash_add(&fc->l_vars.dv_hashtab, v->di_key);
|
||||
v->di_tv.v_type = VAR_DICT;
|
||||
v->di_tv.v_lock = VAR_UNLOCKED;
|
||||
v->di_tv.vval.v_dict = selfdict;
|
||||
@@ -911,7 +911,7 @@ void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rett
|
||||
STRCPY(name, "000");
|
||||
#endif
|
||||
v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
|
||||
tv_dict_add(&fc->l_avars, v);
|
||||
hash_add(&fc->l_avars.dv_hashtab, v->di_key);
|
||||
v->di_tv.v_type = VAR_LIST;
|
||||
v->di_tv.v_lock = VAR_FIXED;
|
||||
v->di_tv.vval.v_list = &fc->l_varlist;
|
||||
@@ -989,9 +989,9 @@ void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rett
|
||||
// Named arguments can be accessed without the "a:" prefix in lambda
|
||||
// expressions. Add to the l: dict.
|
||||
tv_copy(&v->di_tv, &v->di_tv);
|
||||
tv_dict_add(&fc->l_vars, v);
|
||||
hash_add(&fc->l_vars.dv_hashtab, v->di_key);
|
||||
} else {
|
||||
tv_dict_add(&fc->l_avars, v);
|
||||
hash_add(&fc->l_avars.dv_hashtab, v->di_key);
|
||||
}
|
||||
|
||||
if (ai >= 0 && ai < MAX_FUNC_ARGS) {
|
||||
|
@@ -1331,7 +1331,7 @@ void set_var_const(const char *name, const size_t name_len, typval_T *const tv,
|
||||
|
||||
v = xmalloc(sizeof(dictitem_T) + strlen(varname));
|
||||
STRCPY(v->di_key, varname);
|
||||
if (tv_dict_add(dict, v) == FAIL) {
|
||||
if (hash_add(ht, v->di_key) == FAIL) {
|
||||
xfree(v);
|
||||
return;
|
||||
}
|
||||
|
@@ -2478,4 +2478,46 @@ func Test_default_arg_value()
|
||||
call assert_equal('msg', HasDefault())
|
||||
endfunc
|
||||
|
||||
" Test for gettext()
|
||||
func Test_gettext()
|
||||
call assert_fails('call gettext(1)', 'E475:')
|
||||
endfunc
|
||||
|
||||
func Test_builtin_check()
|
||||
call assert_fails('let g:["trim"] = {x -> " " .. x}', 'E704:')
|
||||
call assert_fails('let g:.trim = {x -> " " .. x}', 'E704:')
|
||||
call assert_fails('let l:["trim"] = {x -> " " .. x}', 'E704:')
|
||||
call assert_fails('let l:.trim = {x -> " " .. x}', 'E704:')
|
||||
let lines =<< trim END
|
||||
vim9script
|
||||
var s:trim = (x) => " " .. x
|
||||
END
|
||||
" call CheckScriptFailure(lines, 'E704:')
|
||||
|
||||
call assert_fails('call extend(g:, #{foo: { -> "foo" }})', 'E704:')
|
||||
let g:bar = 123
|
||||
call extend(g:, #{bar: { -> "foo" }}, "keep")
|
||||
call assert_fails('call extend(g:, #{bar: { -> "foo" }}, "force")', 'E704:')
|
||||
unlet g:bar
|
||||
|
||||
call assert_fails('call extend(l:, #{foo: { -> "foo" }})', 'E704:')
|
||||
let bar = 123
|
||||
call extend(l:, #{bar: { -> "foo" }}, "keep")
|
||||
call assert_fails('call extend(l:, #{bar: { -> "foo" }}, "force")', 'E704:')
|
||||
unlet bar
|
||||
|
||||
call assert_fails('call extend(g:, #{foo: function("extend")})', 'E704:')
|
||||
let g:bar = 123
|
||||
call extend(g:, #{bar: function("extend")}, "keep")
|
||||
call assert_fails('call extend(g:, #{bar: function("extend")}, "force")', 'E704:')
|
||||
unlet g:bar
|
||||
|
||||
call assert_fails('call extend(l:, #{foo: function("extend")})', 'E704:')
|
||||
let bar = 123
|
||||
call extend(l:, #{bar: function("extend")}, "keep")
|
||||
call assert_fails('call extend(l:, #{bar: function("extend")}, "force")', 'E704:')
|
||||
unlet bar
|
||||
endfunc
|
||||
|
||||
|
||||
" vim: shiftwidth=2 sts=2 expandtab
|
||||
|
@@ -293,6 +293,7 @@ func Test_let_errors()
|
||||
call assert_fails('let l += 2', 'E734:')
|
||||
call assert_fails('let g:["a;b"] = 10', 'E461:')
|
||||
call assert_fails('let g:.min = function("max")', 'E704:')
|
||||
call assert_fails('let g:cos = "" | let g:.cos = {-> 42}', 'E704:')
|
||||
if has('channel')
|
||||
let ch = test_null_channel()
|
||||
call assert_fails('let ch += 1', 'E734:')
|
||||
|
Reference in New Issue
Block a user