changeset 25153:610e15e8778d v8.2.3113

patch 8.2.3113: no error when for loop variable shadows script variable Commit: https://github.com/vim/vim/commit/442b29c968eff1c8fec1a96418aa47be9cba62ea Author: Bram Moolenaar <Bram@vim.org> Date: Mon Jul 5 22:23:00 2021 +0200 patch 8.2.3113: no error when for loop variable shadows script variable Problem: No error when for loop variable shadows script variable. Solution: Check for the error. (closes https://github.com/vim/vim/issues/8512)
author Bram Moolenaar <Bram@vim.org>
date Mon, 05 Jul 2021 22:30:03 +0200
parents 7da8d1cf23f3
children 99d63a6a2e20
files src/eval.c src/testdir/test_vim9_script.vim src/version.c
diffstat 3 files changed, 14 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/eval.c
+++ b/src/eval.c
@@ -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;
 
@@ -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);
@@ -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);
     }
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -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:')
--- a/src/version.c
+++ b/src/version.c
@@ -756,6 +756,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    3113,
+/**/
     3112,
 /**/
     3111,