summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2022-09-18 12:00:21 +0100
committerBram Moolenaar <Bram@vim.org>2022-09-18 12:00:21 +0100
commitdbbb02bc770646776a04122ff58313d170873e67 (patch)
treeaf52a5dc2c436ac3bb60da3db6cf2007cce246ae
parent96caa557f92f7e1fd61d4b401fefd30c91eb44f0 (diff)
downloadvim-git-9.0.0495.tar.gz
patch 9.0.0495: closure doesn't work properly in nested loopv9.0.0495
Problem: Closure doesn't work properly in nested loop. Solution: Save variables up to the outer loop.
-rw-r--r--src/testdir/test_vim9_script.vim26
-rw-r--r--src/version.c2
-rw-r--r--src/vim9execute.c4
3 files changed, 30 insertions, 2 deletions
diff --git a/src/testdir/test_vim9_script.vim b/src/testdir/test_vim9_script.vim
index 85700b6c3..c4f02ab28 100644
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -2300,6 +2300,32 @@ def Test_for_loop_with_closure()
endfor
END
v9.CheckDefAndScriptSuccess(lines)
+
+ # Also works for a nested loop
+ lines =<< trim END
+ var flist: list<func>
+ var n = 0
+ for i in range(3)
+ var ii = i
+ for a in ['a', 'b', 'c']
+ var aa = a
+ flist[n] = () => ii .. aa
+ ++n
+ endfor
+ endfor
+
+ n = 0
+ for i in range(3)
+ for a in ['a', 'b', 'c']
+ assert_equal(i .. a, flist[n]())
+ ++n
+ endfor
+ endfor
+ END
+ v9.CheckScriptSuccess(['vim9script'] + lines)
+ # FIXME: not yet right for :def
+ lines[14] = 'assert_equal(2 .. a, flist[n]())'
+ v9.CheckDefSuccess(lines)
enddef
def Test_for_loop_fails()
diff --git a/src/version.c b/src/version.c
index 88888d430..c361f97b6 100644
--- a/src/version.c
+++ b/src/version.c
@@ -700,6 +700,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 495,
+/**/
494,
/**/
493,
diff --git a/src/vim9execute.c b/src/vim9execute.c
index b6252612a..22dfaaa52 100644
--- a/src/vim9execute.c
+++ b/src/vim9execute.c
@@ -2671,7 +2671,7 @@ execute_endloop(isn_T *iptr, ectx_T *ectx)
{
partial_T *pt = ((partial_T **)gap->ga_data)[idx];
- if (pt->pt_refcount > 1)
+ if (pt->pt_refcount > 1 && pt->pt_loopvars == NULL)
{
int refcount = pt->pt_refcount;
int i;
@@ -2727,7 +2727,7 @@ execute_endloop(isn_T *iptr, ectx_T *ectx)
{
partial_T *pt = ((partial_T **)gap->ga_data)[idx];
- if (pt->pt_refcount > 1)
+ if (pt->pt_refcount > 1 && pt->pt_loopvars == NULL)
{
++loopvars->lvs_refcount;
pt->pt_loopvars = loopvars;