Skip to content

Commit

Permalink
patch 8.2.3113: no error when for loop variable shadows script variable
Browse files Browse the repository at this point in the history
Problem:    No error when for loop variable shadows script variable.
Solution:   Check for the error. (closes #8512)
  • Loading branch information
brammool committed Jul 5, 2021
1 parent 5cb0962 commit 442b29c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/eval.c
Expand Up @@ -1777,7 +1777,10 @@ next_for_item(void *fi_void, char_u *arg)
forinfo_T *fi = (forinfo_T *)fi_void;
int result;
int flag = ASSIGN_FOR_LOOP | (in_vim9script()
? (ASSIGN_FINAL | ASSIGN_DECL | ASSIGN_NO_MEMBER_TYPE)
? (ASSIGN_FINAL
// first round: error if variable exists
| (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
| ASSIGN_NO_MEMBER_TYPE)
: 0);
listitem_T *item;

Expand Down Expand Up @@ -1807,6 +1810,7 @@ next_for_item(void *fi_void, char_u *arg)
tv.v_lock = VAR_FIXED;
tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
fi->fi_byte_idx += len;
++fi->fi_bi;
result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
fi->fi_varcount, flag, NULL) == OK;
vim_free(tv.vval.v_string);
Expand All @@ -1819,6 +1823,7 @@ next_for_item(void *fi_void, char_u *arg)
else
{
fi->fi_lw.lw_item = item->li_next;
++fi->fi_bi;
result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
fi->fi_varcount, flag, NULL) == OK);
}
Expand Down
12 changes: 6 additions & 6 deletions src/testdir/test_vim9_script.vim
Expand Up @@ -2533,12 +2533,12 @@ def Test_for_loop()
enddef

def Test_for_loop_fails()
CheckDefFailure(['for '], 'E1097:')
CheckDefFailure(['for x'], 'E1097:')
CheckDefFailure(['for x in'], 'E1097:')
CheckDefFailure(['for # in range(5)'], 'E690:')
CheckDefFailure(['for i In range(5)'], 'E690:')
CheckDefFailure(['var x = 5', 'for x in range(5)'], 'E1017:')
CheckDefAndScriptFailure2(['for '], 'E1097:', 'E690:')
CheckDefAndScriptFailure2(['for x'], 'E1097:', 'E690:')
CheckDefAndScriptFailure2(['for x in'], 'E1097:', 'E15:')
CheckDefAndScriptFailure(['for # in range(5)'], 'E690:')
CheckDefAndScriptFailure(['for i In range(5)'], 'E690:')
CheckDefAndScriptFailure2(['var x = 5', 'for x in range(5)', 'endfor'], 'E1017:', 'E1041:')
CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:')
delfunc! g:Func
CheckDefFailure(['for i in xxx'], 'E1001:')
Expand Down
2 changes: 2 additions & 0 deletions src/version.c
Expand Up @@ -755,6 +755,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
/**/
3113,
/**/
3112,
/**/
Expand Down

0 comments on commit 442b29c

Please sign in to comment.