Patch 8.2.2897
Problem: Vim9: can use reserved words at the script level.
Solution: Check variable names for reserved words. (closes #8253)
Files: src/vim9compile.c, src/vim9script.c, src/proto/vim9script.pro,
src/eval.c, src/testdir/test_vim9_assign.vim
*** ../vim-8.2.2896/src/vim9compile.c 2021-05-28 17:52:36.908197725 +0200
--- src/vim9compile.c 2021-05-28 20:52:22.859774682 +0200
***************
*** 5594,5607 ****
return 0;
}
- // words that cannot be used as a variable
- static char *reserved[] = {
- "true",
- "false",
- "null",
- NULL
- };
-
/*
* Generate the load instruction for "name".
*/
--- 5594,5599 ----
***************
*** 5995,6010 ****
}
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 (lookup_local(var_start, lhs->lhs_varlen,
&lhs->lhs_local_lvar, cctx) == OK)
--- 5987,5995 ----
}
else
{
// No specific kind of variable recognized, just a name.
! if (check_reserved_name(lhs->lhs_name) == FAIL)
! return FAIL;
if (lookup_local(var_start, lhs->lhs_varlen,
&lhs->lhs_local_lvar, cctx) == OK)
*** ../vim-8.2.2896/src/vim9script.c 2021-04-24 19:08:20.488010321 +0200
--- src/vim9script.c 2021-05-28 21:03:15.021312153 +0200
***************
*** 709,718 ****
}
name = vim_strnsave(arg, p - arg);
! // parse type
p = skipwhite(p + 1);
type = parse_type(&p, &si->sn_type_list, TRUE);
! if (type == NULL)
{
vim_free(name);
return p;
--- 709,718 ----
}
name = vim_strnsave(arg, p - arg);
! // parse type, check for reserved name
p = skipwhite(p + 1);
type = parse_type(&p, &si->sn_type_list, TRUE);
! if (type == NULL || check_reserved_name(name) == FAIL)
{
vim_free(name);
return p;
***************
*** 974,977 ****
--- 974,999 ----
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
*** ../vim-8.2.2896/src/proto/vim9script.pro 2021-04-24 19:08:20.488010321
+0200
--- src/proto/vim9script.pro 2021-05-28 20:52:15.455803580 +0200
***************
*** 18,21 ****
--- 18,22 ----
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 : */
*** ../vim-8.2.2896/src/eval.c 2021-05-28 17:52:36.904197735 +0200
--- src/eval.c 2021-05-28 20:59:53.366060869 +0200
***************
*** 1309,1314 ****
--- 1309,1317 ----
{
cc = *endp;
*endp = NUL;
+ if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
+ return;
+
if (lp->ll_blob != NULL)
{
int error = FALSE, val;
*** ../vim-8.2.2896/src/testdir/test_vim9_assign.vim 2021-04-28
20:40:39.799852470 +0200
--- src/testdir/test_vim9_assign.vim 2021-05-28 21:03:51.045179137 +0200
***************
*** 249,254 ****
--- 249,261 ----
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 []
*** ../vim-8.2.2896/src/version.c 2021-05-28 18:32:08.646074589 +0200
--- src/version.c 2021-05-28 21:05:29.272817314 +0200
***************
*** 752,753 ****
--- 752,755 ----
{ /* Add new patch number below this line */
+ /**/
+ 2897,
/**/
--
There are 10 kinds of people: Those who understand binary and those who don't.
/// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/vim_dev/202105281906.14SJ6Yio1326164%40masaka.moolenaar.net.