mirror of
https://github.com/vim/vim.git
synced 2025-09-25 03:54:15 -04:00
patch 9.0.0495: closure doesn't work properly in nested loop
Problem: Closure doesn't work properly in nested loop. Solution: Save variables up to the outer loop.
This commit is contained in:
@@ -2300,6 +2300,32 @@ def Test_for_loop_with_closure()
|
|||||||
endfor
|
endfor
|
||||||
END
|
END
|
||||||
v9.CheckDefAndScriptSuccess(lines)
|
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
|
enddef
|
||||||
|
|
||||||
def Test_for_loop_fails()
|
def Test_for_loop_fails()
|
||||||
|
@@ -699,6 +699,8 @@ static char *(features[]) =
|
|||||||
|
|
||||||
static int included_patches[] =
|
static int included_patches[] =
|
||||||
{ /* Add new patch number below this line */
|
{ /* Add new patch number below this line */
|
||||||
|
/**/
|
||||||
|
495,
|
||||||
/**/
|
/**/
|
||||||
494,
|
494,
|
||||||
/**/
|
/**/
|
||||||
|
@@ -2671,7 +2671,7 @@ execute_endloop(isn_T *iptr, ectx_T *ectx)
|
|||||||
{
|
{
|
||||||
partial_T *pt = ((partial_T **)gap->ga_data)[idx];
|
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 refcount = pt->pt_refcount;
|
||||||
int i;
|
int i;
|
||||||
@@ -2727,7 +2727,7 @@ execute_endloop(isn_T *iptr, ectx_T *ectx)
|
|||||||
{
|
{
|
||||||
partial_T *pt = ((partial_T **)gap->ga_data)[idx];
|
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;
|
++loopvars->lvs_refcount;
|
||||||
pt->pt_loopvars = loopvars;
|
pt->pt_loopvars = loopvars;
|
||||||
|
Reference in New Issue
Block a user