diff src/testdir/test_vim9_script.vim @ 30249:c0f0118b6790 v9.0.0460

patch 9.0.0460: loop variable can't be found Commit: https://github.com/vim/vim/commit/766ae5b252eaa6ee2bff70f1913d1cbfb51101bd Author: Bram Moolenaar <Bram@vim.org> Date: Wed Sep 14 00:30:51 2022 +0100 patch 9.0.0460: loop variable can't be found Problem: Loop variable can't be found. Solution: Adjust block_id of the loop variable each round.
author Bram Moolenaar <Bram@vim.org>
date Wed, 14 Sep 2022 01:45:04 +0200
parents 327bca7b70ea
children 8c50f70b9a22
line wrap: on
line diff
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -2259,9 +2259,23 @@ def Test_for_loop()
 enddef
 
 def Test_for_loop_with_closure()
+  # using the loop variable in a closure results in the last used value
   var lines =<< trim END
       var flist: list<func>
       for i in range(5)
+        flist[i] = () => i
+      endfor
+      for i in range(5)
+        assert_equal(4, flist[i]())
+      endfor
+  END
+  v9.CheckDefAndScriptSuccess(lines)
+
+  # using a local variable set to the loop variable in a closure results in the
+  # value at that moment
+  lines =<< trim END
+      var flist: list<func>
+      for i in range(5)
         var inloop = i
         flist[i] = () => inloop
       endfor