# HG changeset patch # User Bram Moolenaar # Date 1622229303 -7200 # Node ID bf8feac8a89a2223a60eb76d0910243bb4fd4157 # Parent 33f991b5be54668346b05470966787020b67de16 patch 8.2.2897: Vim9: can use reserved words at the script level Commit: https://github.com/vim/vim/commit/d0edaf9dc253e619ccc321ceaac321aee11c1ea5 Author: Bram Moolenaar 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) diff --git a/src/eval.c b/src/eval.c --- 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 --- 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 : */ diff --git a/src/testdir/test_vim9_assign.vim b/src/testdir/test_vim9_assign.vim --- 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 --- 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 --- 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 --- 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