Skip to content

Commit 88421d6

Browse files
committed
patch 8.2.3209: Vim9: lambda doesn't find block-local variable
Problem: Vim9: lambda doesn't find block-local variable. Solution: Adjust how a script-local variable is found. (closes #8614)
1 parent 1a3e574 commit 88421d6

3 files changed

Lines changed: 42 additions & 1 deletion

File tree

src/testdir/test_vim9_func.vim

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2352,6 +2352,42 @@ def Test_list_lambda()
23522352
assert_match('def <lambda>\d\+(_: any): number\n1 return 0\n enddef', body)
23532353
enddef
23542354

2355+
def Test_lamba_block_variable()
2356+
var lines =<< trim END
2357+
vim9script
2358+
var flist: list<func>
2359+
for i in range(10)
2360+
var inloop = i
2361+
flist[i] = () => inloop
2362+
endfor
2363+
END
2364+
CheckScriptSuccess(lines)
2365+
2366+
lines =<< trim END
2367+
vim9script
2368+
if true
2369+
var outloop = 5
2370+
var flist: list<func>
2371+
for i in range(10)
2372+
flist[i] = () => outloop
2373+
endfor
2374+
endif
2375+
END
2376+
CheckScriptSuccess(lines)
2377+
2378+
lines =<< trim END
2379+
vim9script
2380+
if true
2381+
var outloop = 5
2382+
endif
2383+
var flist: list<func>
2384+
for i in range(10)
2385+
flist[i] = () => outloop
2386+
endfor
2387+
END
2388+
CheckScriptFailure(lines, 'E1001: Variable not found: outloop', 1)
2389+
enddef
2390+
23552391
def Test_legacy_lambda()
23562392
legacy echo {x -> 'hello ' .. x}('foo')
23572393

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,8 @@ static char *(features[]) =
755755

756756
static int included_patches[] =
757757
{ /* Add new patch number below this line */
758+
/**/
759+
3209,
758760
/**/
759761
3208,
760762
/**/

src/vim9compile.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ find_script_var(char_u *name, size_t len, cctx_T *cctx)
339339
hashitem_T *hi;
340340
int cc;
341341
sallvar_T *sav;
342+
sallvar_T *found_sav;
342343
ufunc_T *ufunc;
343344

344345
// Find the list of all script variables with the right name.
@@ -361,6 +362,7 @@ find_script_var(char_u *name, size_t len, cctx_T *cctx)
361362
// Go over the variables with this name and find one that was visible
362363
// from the function.
363364
ufunc = cctx->ctx_ufunc;
365+
found_sav = sav;
364366
while (sav != NULL)
365367
{
366368
int idx;
@@ -373,7 +375,8 @@ find_script_var(char_u *name, size_t len, cctx_T *cctx)
373375
sav = sav->sav_next;
374376
}
375377

376-
return NULL;
378+
// Not found, assume variable at script level was visible.
379+
return found_sav;
377380
}
378381

379382
/*

0 commit comments

Comments
 (0)