changeset 25280:ca7a8ea2eb5f v8.2.3177

patch 8.2.3177: Vim9: can not use "for _ in expr" at script level Commit: https://github.com/vim/vim/commit/ad2d4969e1616d3a0354c181e3a09271d5433b3d Author: Bram Moolenaar <Bram@vim.org> Date: Sun Jul 18 17:08:50 2021 +0200 patch 8.2.3177: Vim9: can not use "for _ in expr" at script level Problem: Vim9: can not use "for _ in expr" at script level. Solution: Skip assignment if the loop variable is "_".
author Bram Moolenaar <Bram@vim.org>
date Sun, 18 Jul 2021 17:15:03 +0200
parents a59175d38799
children 1ec4ac687d85
files src/eval.c src/testdir/test_vim9_script.vim src/version.c
diffstat 3 files changed, 20 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/eval.c
+++ b/src/eval.c
@@ -1784,6 +1784,8 @@ next_for_item(void *fi_void, char_u *arg
 			     | ASSIGN_NO_MEMBER_TYPE)
 			 : 0);
     listitem_T	*item;
+    int		skip_assign = in_vim9script() && arg[0] == '_'
+						      && !eval_isnamec(arg[1]);
 
     if (fi->fi_blob != NULL)
     {
@@ -1795,6 +1797,8 @@ next_for_item(void *fi_void, char_u *arg
 	tv.v_lock = VAR_FIXED;
 	tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
 	++fi->fi_bi;
+	if (skip_assign)
+	    return TRUE;
 	return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
 					    fi->fi_varcount, flag, NULL) == OK;
     }
@@ -1812,7 +1816,10 @@ next_for_item(void *fi_void, char_u *arg
 	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,
+	if (skip_assign)
+	    result = TRUE;
+	else
+	    result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
 					    fi->fi_varcount, flag, NULL) == OK;
 	vim_free(tv.vval.v_string);
 	return result;
@@ -1825,7 +1832,10 @@ next_for_item(void *fi_void, char_u *arg
     {
 	fi->fi_lw.lw_item = item->li_next;
 	++fi->fi_bi;
-	result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
+	if (skip_assign)
+	    result = TRUE;
+	else
+	    result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
 					   fi->fi_varcount, flag, NULL) == OK);
     }
     return result;
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -2557,6 +2557,12 @@ def Test_for_loop()
         dd.counter = 12
       endfor
       assert_equal([{a: 'Cat', counter: 12}], foo)
+
+      reslist = []
+      for _ in range(3)
+        reslist->add('x')
+      endfor
+      assert_equal(['x', 'x', 'x'], reslist)
   END
   CheckDefAndScriptSuccess(lines)
 enddef
--- 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 */
 /**/
+    3177,
+/**/
     3176,
 /**/
     3175,