0
0
mirror of https://github.com/vim/vim.git synced 2025-09-24 03:44:06 -04:00

patch 8.2.2897: Vim9: can use reserved words at the script level

Problem:    Vim9: can use reserved words at the script level.
Solution:   Check variable names for reserved words. (closes #8253)
This commit is contained in:
Bram Moolenaar
2021-05-28 21:06:08 +02:00
parent dc3275a1ac
commit d0edaf9dc2
6 changed files with 39 additions and 19 deletions

View File

@@ -1309,6 +1309,9 @@ set_var_lval(
{
cc = *endp;
*endp = NUL;
if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
return;
if (lp->ll_blob != NULL)
{
int error = FALSE, val;

View File

@@ -18,4 +18,5 @@ void hide_script_var(scriptitem_T *si, int idx, int func_defined);
void free_all_script_vars(scriptitem_T *si);
svar_T *find_typval_in_script(typval_T *dest);
int check_script_var_type(typval_T *dest, typval_T *value, char_u *name, where_T where);
int check_reserved_name(char_u *name);
/* vim: set ft=c : */

View File

@@ -249,6 +249,13 @@ def Test_assignment()
END
enddef
def Test_reserved_name()
for name in ['true', 'false', 'null']
CheckDefExecAndScriptFailure(['var ' .. name .. ' = 0'], 'E1034:')
CheckDefExecAndScriptFailure(['var ' .. name .. ': bool'], 'E1034:')
endfor
enddef
def Test_skipped_assignment()
var lines =<< trim END
for x in []

View File

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

View File

@@ -5594,14 +5594,6 @@ assignment_len(char_u *p, int *heredoc)
return 0;
}
// words that cannot be used as a variable
static char *reserved[] = {
"true",
"false",
"null",
NULL
};
/*
* Generate the load instruction for "name".
*/
@@ -5995,16 +5987,9 @@ compile_lhs(
}
else
{
int idx;
// No specific kind of variable recognized, just a name.
for (idx = 0; reserved[idx] != NULL; ++idx)
if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
{
semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
return FAIL;
}
if (check_reserved_name(lhs->lhs_name) == FAIL)
return FAIL;
if (lookup_local(var_start, lhs->lhs_varlen,
&lhs->lhs_local_lvar, cctx) == OK)

View File

@@ -709,10 +709,10 @@ vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
}
name = vim_strnsave(arg, p - arg);
// parse type
// parse type, check for reserved name
p = skipwhite(p + 1);
type = parse_type(&p, &si->sn_type_list, TRUE);
if (type == NULL)
if (type == NULL || check_reserved_name(name) == FAIL)
{
vim_free(name);
return p;
@@ -974,4 +974,26 @@ check_script_var_type(
return OK; // not really
}
// words that cannot be used as a variable
static char *reserved[] = {
"true",
"false",
"null",
NULL
};
int
check_reserved_name(char_u *name)
{
int idx;
for (idx = 0; reserved[idx] != NULL; ++idx)
if (STRCMP(reserved[idx], name) == 0)
{
semsg(_(e_cannot_use_reserved_name), name);
return FAIL;
}
return OK;
}
#endif // FEAT_EVAL