Skip to content

Commit 8863bda

Browse files
committed
patch 8.2.2617: Vim9: script variable in block not found by function
Problem: Vim9: script variable in a block scope not found by a nested function. Solution: Copy the block scope IDs before compiling the function.
1 parent 3e19169 commit 8863bda

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

src/testdir/test_vim9_disassemble.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ def NestedOuter()
947947
enddef
948948
enddef
949949

950-
def Test_nested_func()
950+
def Test_disassemble_nested_func()
951951
var instr = execute('disassemble NestedOuter')
952952
assert_match('NestedOuter\_s*' ..
953953
'def g:Inner()\_s*' ..
@@ -965,7 +965,7 @@ def NestedDefList()
965965
def /Info/
966966
enddef
967967

968-
def Test_nested_def_list()
968+
def Test_disassemble_nested_def_list()
969969
var instr = execute('disassemble NestedDefList')
970970
assert_match('NestedDefList\_s*' ..
971971
'def\_s*' ..

src/testdir/test_vim9_func.vim

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,6 @@ def Test_nested_function()
393393
CheckDefFailure(lines, 'E1117:')
394394

395395
# nested function inside conditional
396-
# TODO: should it work when "thecount" is inside the "if"?
397396
lines =<< trim END
398397
vim9script
399398
var thecount = 0
@@ -411,6 +410,25 @@ def Test_nested_function()
411410
assert_equal(2, Test())
412411
END
413412
CheckScriptSuccess(lines)
413+
414+
# also works when "thecount" is inside the "if" block
415+
lines =<< trim END
416+
vim9script
417+
if true
418+
var thecount = 0
419+
def Test(): number
420+
def TheFunc(): number
421+
thecount += 1
422+
return thecount
423+
enddef
424+
return TheFunc()
425+
enddef
426+
endif
427+
defcompile
428+
assert_equal(1, Test())
429+
assert_equal(2, Test())
430+
END
431+
CheckScriptSuccess(lines)
414432
enddef
415433

416434
def Test_not_nested_function()

src/version.c

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

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2617,
753755
/**/
754756
2616,
755757
/**/

src/vim9compile.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5183,6 +5183,21 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx)
51835183
r = eap->skip ? OK : FAIL;
51845184
goto theend;
51855185
}
5186+
5187+
// copy over the block scope IDs before compiling
5188+
if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5189+
{
5190+
int block_depth = cctx->ctx_ufunc->uf_block_depth;
5191+
5192+
ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5193+
if (ufunc->uf_block_ids != NULL)
5194+
{
5195+
mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5196+
sizeof(int) * block_depth);
5197+
ufunc->uf_block_depth = block_depth;
5198+
}
5199+
}
5200+
51865201
if (func_needs_compiling(ufunc, PROFILING(ufunc))
51875202
&& compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
51885203
== FAIL)
@@ -5209,25 +5224,12 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx)
52095224
// Define a local variable for the function reference.
52105225
lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
52115226
TRUE, ufunc->uf_func_type);
5212-
int block_depth = cctx->ctx_ufunc->uf_block_depth;
52135227

52145228
if (lvar == NULL)
52155229
goto theend;
52165230
if (generate_FUNCREF(cctx, ufunc) == FAIL)
52175231
goto theend;
52185232
r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5219-
5220-
// copy over the block scope IDs
5221-
if (block_depth > 0)
5222-
{
5223-
ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5224-
if (ufunc->uf_block_ids != NULL)
5225-
{
5226-
mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5227-
sizeof(int) * block_depth);
5228-
ufunc->uf_block_depth = block_depth;
5229-
}
5230-
}
52315233
}
52325234
// TODO: warning for trailing text?
52335235

0 commit comments

Comments
 (0)