]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.0.0726: looping over list of lists works in script, not in function v9.0.0726
authorBram Moolenaar <Bram@vim.org>
Tue, 11 Oct 2022 20:41:25 +0000 (21:41 +0100)
committerBram Moolenaar <Bram@vim.org>
Tue, 11 Oct 2022 20:41:25 +0000 (21:41 +0100)
Problem:    Looping over list of lists and changing the list contents works in
            Vim9 script, not in a compiled function.
Solution:   Mark the loop variable final instead of const. (closes #11347)

src/testdir/test_vim9_script.vim
src/version.c
src/vim9cmds.c

index 05fd2fb57f059f7e252c6e68fc32412aa4968d02..a0b8352f3f727311a7a24bdf89c06f4e3cbc557a 100644 (file)
@@ -2275,6 +2275,20 @@ def Test_for_loop()
   v9.CheckDefAndScriptSuccess(lines)
 enddef
 
+def Test_for_loop_list_of_lists()
+  # loop variable is final, not const
+  var lines =<< trim END
+      # Filter out all odd numbers in each sublist
+      var list: list<list<number>> = [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4]]
+      for i in list
+          filter(i, (_, n: number): bool => n % 2 == 0)
+      endfor
+
+      assert_equal([[], [2], [2], [2, 4]], list)
+  END
+  v9.CheckDefAndScriptSuccess(lines)
+enddef
+
 def Test_for_loop_with_closure()
   # using the loop variable in a closure results in the last used value
   var lines =<< trim END
index aa5ca6794dbd744a0f9655466d8f18b5a811e62c..251ece71d00e37f09b8201201c235d6ca0e33816 100644 (file)
@@ -699,6 +699,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    726,
 /**/
     725,
 /**/
index a1e8ac18c01f9771a0bb1cb0658203c5d0e6eeda..73f95b26dfe52bb76c9e3f2df7f1149b681f02ac 100644 (file)
@@ -1048,7 +1048,7 @@ compile_for(char_u *arg_start, cctx_T *cctx)
                        && need_type_where(item_type, lhs_type, -1,
                                            where, cctx, FALSE, FALSE) == FAIL)
                    goto failed;
-               var_lvar = reserve_local(cctx, arg, varlen, ASSIGN_CONST,
+               var_lvar = reserve_local(cctx, arg, varlen, ASSIGN_FINAL,
                                                                     lhs_type);
                if (var_lvar == NULL)
                    // out of memory or used as an argument