0
0
mirror of https://github.com/vim/vim.git synced 2025-07-26 11:04:33 -04:00

patch 8.2.2760: Vim9: no error for changing a for loop variable

Problem:    Vim9: no error for changing a for loop variable.
Solution:   Make the loop variable read-only. (issue #8102)
This commit is contained in:
Bram Moolenaar 2021-04-13 21:48:03 +02:00
parent f2253963c2
commit f6a8d420a8
6 changed files with 41 additions and 12 deletions

View File

@ -1351,7 +1351,8 @@ set_var_lval(
{ {
typval_T tv; typval_T tv;
if (flags & (ASSIGN_CONST | ASSIGN_FINAL)) if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
&& (flags & ASSIGN_FOR_LOOP) == 0)
{ {
emsg(_(e_cannot_mod)); emsg(_(e_cannot_mod));
*endp = cc; *endp = cc;
@ -1390,7 +1391,8 @@ set_var_lval(
listitem_T *ll_li = lp->ll_li; listitem_T *ll_li = lp->ll_li;
int ll_n1 = lp->ll_n1; int ll_n1 = lp->ll_n1;
if (flags & (ASSIGN_CONST | ASSIGN_FINAL)) if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
&& (flags & ASSIGN_FOR_LOOP) == 0)
{ {
emsg(_("E996: Cannot lock a range")); emsg(_("E996: Cannot lock a range"));
return; return;
@ -1449,7 +1451,8 @@ set_var_lval(
/* /*
* Assign to a List or Dictionary item. * Assign to a List or Dictionary item.
*/ */
if (flags & (ASSIGN_CONST | ASSIGN_FINAL)) if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
&& (flags & ASSIGN_FOR_LOOP) == 0)
{ {
emsg(_("E996: Cannot lock a list or dict")); emsg(_("E996: Cannot lock a list or dict"));
return; return;
@ -1775,7 +1778,9 @@ next_for_item(void *fi_void, char_u *arg)
{ {
forinfo_T *fi = (forinfo_T *)fi_void; forinfo_T *fi = (forinfo_T *)fi_void;
int result; int result;
int flag = in_vim9script() ? ASSIGN_DECL : 0; int flag = ASSIGN_FOR_LOOP | (in_vim9script()
? (ASSIGN_FINAL | ASSIGN_DECL | ASSIGN_NO_MEMBER_TYPE)
: 0);
listitem_T *item; listitem_T *item;
if (fi->fi_blob != NULL) if (fi->fi_blob != NULL)

View File

@ -1315,7 +1315,8 @@ ex_let_one(
// ":let $VAR = expr": Set environment variable. // ":let $VAR = expr": Set environment variable.
if (*arg == '$') if (*arg == '$')
{ {
if (flags & (ASSIGN_CONST | ASSIGN_FINAL)) if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
&& (flags & ASSIGN_FOR_LOOP) == 0)
{ {
emsg(_("E996: Cannot lock an environment variable")); emsg(_("E996: Cannot lock an environment variable"));
return NULL; return NULL;
@ -1365,9 +1366,11 @@ ex_let_one(
// ":let &option = expr": Set option value. // ":let &option = expr": Set option value.
// ":let &l:option = expr": Set local option value. // ":let &l:option = expr": Set local option value.
// ":let &g:option = expr": Set global option value. // ":let &g:option = expr": Set global option value.
// ":for &ts in range(8)": Set option value for for loop
else if (*arg == '&') else if (*arg == '&')
{ {
if (flags & (ASSIGN_CONST | ASSIGN_FINAL)) if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
&& (flags & ASSIGN_FOR_LOOP) == 0)
{ {
emsg(_(e_const_option)); emsg(_(e_const_option));
return NULL; return NULL;
@ -1466,7 +1469,8 @@ ex_let_one(
// ":let @r = expr": Set register contents. // ":let @r = expr": Set register contents.
else if (*arg == '@') else if (*arg == '@')
{ {
if (flags & (ASSIGN_CONST | ASSIGN_FINAL)) if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
&& (flags & ASSIGN_FOR_LOOP) == 0)
{ {
emsg(_("E996: Cannot lock a register")); emsg(_("E996: Cannot lock a register"));
return NULL; return NULL;
@ -3158,7 +3162,7 @@ set_var_const(
type_T *type, type_T *type,
typval_T *tv_arg, typval_T *tv_arg,
int copy, // make copy of value in "tv" int copy, // make copy of value in "tv"
int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc. int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
int var_idx) // index for ":let [a, b] = list" int var_idx) // index for ":let [a, b] = list"
{ {
typval_T *tv = tv_arg; typval_T *tv = tv_arg;
@ -3169,6 +3173,7 @@ set_var_const(
int is_script_local; int is_script_local;
int vim9script = in_vim9script(); int vim9script = in_vim9script();
int var_in_vim9script; int var_in_vim9script;
int flags = flags_arg;
ht = find_var_ht(name, &varname); ht = find_var_ht(name, &varname);
if (ht == NULL || *varname == NUL) if (ht == NULL || *varname == NUL)
@ -3187,6 +3192,11 @@ set_var_const(
vim9_declare_error(name); vim9_declare_error(name);
goto failed; goto failed;
} }
if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
&& vim_strchr((char_u *)"gwbt", name[0]) != NULL)
// Do not make g:var, w:var, b:var or t:var final.
flags &= ~ASSIGN_FINAL;
var_in_vim9script = is_script_local && current_script_is_vim9(); var_in_vim9script = is_script_local && current_script_is_vim9();
if (var_in_vim9script && name[0] == '_' && name[1] == NUL) if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
{ {
@ -3220,7 +3230,8 @@ set_var_const(
// Item already exists. Allowed to replace when reloading. // Item already exists. Allowed to replace when reloading.
if ((di->di_flags & DI_FLAGS_RELOAD) == 0) if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
{ {
if (flags & (ASSIGN_CONST | ASSIGN_FINAL)) if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
&& (flags & ASSIGN_FOR_LOOP) == 0)
{ {
emsg(_(e_cannot_mod)); emsg(_(e_cannot_mod));
goto failed; goto failed;
@ -3255,7 +3266,8 @@ set_var_const(
// A Vim9 script-local variable is also present in sn_all_vars and // A Vim9 script-local variable is also present in sn_all_vars and
// sn_var_vals. It may set "type" from "tv". // sn_var_vals. It may set "type" from "tv".
if (var_in_vim9script) if (var_in_vim9script)
update_vim9_script_var(FALSE, di, flags, tv, &type); update_vim9_script_var(FALSE, di, flags, tv, &type,
(flags & ASSIGN_NO_MEMBER_TYPE) == 0);
} }
// existing variable, need to clear the value // existing variable, need to clear the value
@ -3353,7 +3365,8 @@ set_var_const(
// A Vim9 script-local variable is also added to sn_all_vars and // A Vim9 script-local variable is also added to sn_all_vars and
// sn_var_vals. It may set "type" from "tv". // sn_var_vals. It may set "type" from "tv".
if (var_in_vim9script) if (var_in_vim9script)
update_vim9_script_var(TRUE, di, flags, tv, &type); update_vim9_script_var(TRUE, di, flags, tv, &type,
(flags & ASSIGN_NO_MEMBER_TYPE) == 0);
} }
if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT) if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)

View File

@ -2393,6 +2393,14 @@ def Test_for_loop_fails()
g:adict = {a: 1} g:adict = {a: 1}
CheckDefExecFailure(['for i in g:adict', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported') CheckDefExecFailure(['for i in g:adict', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
unlet g:adict unlet g:adict
var lines =<< trim END
var d: list<dict<any>> = [{a: 0}]
for e in d
e = {a: 0, b: ''}
endfor
END
CheckDefAndScriptFailure2(lines, 'E1018:', 'E46:', 3)
enddef enddef
def Test_for_loop_script_var() def Test_for_loop_script_var()

View File

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

View File

@ -2158,6 +2158,7 @@ typedef enum {
#define ASSIGN_DECL 0x08 // may declare variable if it does not exist #define ASSIGN_DECL 0x08 // may declare variable if it does not exist
#define ASSIGN_UNPACK 0x10 // using [a, b] = list #define ASSIGN_UNPACK 0x10 // using [a, b] = list
#define ASSIGN_NO_MEMBER_TYPE 0x20 // use "any" for list and dict member type #define ASSIGN_NO_MEMBER_TYPE 0x20 // use "any" for list and dict member type
#define ASSIGN_FOR_LOOP 0x40 // assigning to loop variable
#include "ex_cmds.h" // Ex command defines #include "ex_cmds.h" // Ex command defines
#include "spell.h" // spell checking stuff #include "spell.h" // spell checking stuff

View File

@ -7590,7 +7590,7 @@ compile_for(char_u *arg_start, cctx_T *cctx)
// Reserve a variable to store "var". // Reserve a variable to store "var".
// TODO: check for type // TODO: check for type
var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); var_lvar = reserve_local(cctx, arg, varlen, TRUE, &t_any);
if (var_lvar == NULL) if (var_lvar == NULL)
// out of memory or used as an argument // out of memory or used as an argument
goto failed; goto failed;