summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2021-05-28 21:06:08 +0200
committerBram Moolenaar <Bram@vim.org>2021-05-28 21:06:08 +0200
commitd0edaf9dc253e619ccc321ceaac321aee11c1ea5 (patch)
tree27559339d69ea3bcfaa8cc8e6a87fd6e752c0d4b
parentdc3275a1ac73b6c4d0c9d2e238ea80b477705b6f (diff)
patch 8.2.2897: Vim9: can use reserved words at the script levelv8.2.2897
Problem: Vim9: can use reserved words at the script level. Solution: Check variable names for reserved words. (closes #8253)
-rw-r--r--src/eval.c3
-rw-r--r--src/proto/vim9script.pro1
-rw-r--r--src/testdir/test_vim9_assign.vim7
-rw-r--r--src/version.c2
-rw-r--r--src/vim9compile.c19
-rw-r--r--src/vim9script.c26
6 files changed, 39 insertions, 19 deletions
diff --git a/src/eval.c b/src/eval.c
index 008b032cef..8e55435496 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -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;
diff --git a/src/proto/vim9script.pro b/src/proto/vim9script.pro
index 2c4bd79606..41bf1287a1 100644
--- a/src/proto/vim9script.pro
+++ b/src/proto/vim9script.pro
@@ -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 : */
diff --git a/src/testdir/test_vim9_assign.vim b/src/testdir/test_vim9_assign.vim
index fa418b2ce2..b8c6433638 100644
--- a/src/testdir/test_vim9_assign.vim
+++ b/src/testdir/test_vim9_assign.vim
@@ -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 []
diff --git a/src/version.c b/src/version.c
index b20a149fa7..da315bf138 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 */
/**/
+ 2897,
+/**/
2896,
/**/
2895,
diff --git a/src/vim9compile.c b/src/vim9compile.c
index 995c1d7491..2ea487de78 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -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)
diff --git a/src/vim9script.c b/src/vim9script.c
index 02c04e2ea7..fbb815c28e 100644
--- a/src/vim9script.c
+++ b/src/vim9script.c
@@ -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