changeset 24717:bf8feac8a89a v8.2.2897

patch 8.2.2897: Vim9: can use reserved words at the script level Commit: https://github.com/vim/vim/commit/d0edaf9dc253e619ccc321ceaac321aee11c1ea5 Author: Bram Moolenaar <Bram@vim.org> Date: Fri May 28 21:06:08 2021 +0200 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 https://github.com/vim/vim/issues/8253)
author Bram Moolenaar <Bram@vim.org>
date Fri, 28 May 2021 21:15:03 +0200
parents 33f991b5be54
children 998269fb087b
files src/eval.c src/proto/vim9script.pro src/testdir/test_vim9_assign.vim src/version.c src/vim9compile.c src/vim9script.c
diffstat 6 files changed, 39 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- 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;
--- a/src/proto/vim9script.pro
+++ b/src/proto/vim9script.pro
@@ -18,4 +18,5 @@ void hide_script_var(scriptitem_T *si, i
 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 : */
--- 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 []
--- 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,
--- 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)
--- a/src/vim9script.c
+++ b/src/vim9script.c
@@ -709,10 +709,10 @@ vim9_declare_scriptvar(exarg_T *eap, cha
     }
     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