Skip to content

Commit 766ae5b

Browse files
committed
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.
1 parent 353b68a commit 766ae5b

File tree

11 files changed

+86
-25
lines changed

11 files changed

+86
-25
lines changed

src/eval.c

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,6 @@
2929
*/
3030
static int current_copyID = 0;
3131

32-
/*
33-
* Info used by a ":for" loop.
34-
*/
35-
typedef struct
36-
{
37-
int fi_semicolon; // TRUE if ending in '; var]'
38-
int fi_varcount; // nr of variables in the list
39-
int fi_break_count; // nr of line breaks encountered
40-
listwatch_T fi_lw; // keep an eye on the item used.
41-
list_T *fi_list; // list being used
42-
int fi_bi; // index of blob
43-
blob_T *fi_blob; // blob being used
44-
char_u *fi_string; // copy of string being used
45-
int fi_byte_idx; // byte index in fi_string
46-
} forinfo_T;
47-
4832
static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
4933
static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
5034
static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
@@ -1914,7 +1898,8 @@ next_for_item(void *fi_void, char_u *arg)
19141898
? (ASSIGN_FINAL
19151899
// first round: error if variable exists
19161900
| (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1917-
| ASSIGN_NO_MEMBER_TYPE)
1901+
| ASSIGN_NO_MEMBER_TYPE
1902+
| ASSIGN_UPDATE_BLOCK_ID)
19181903
: 0);
19191904
listitem_T *item;
19201905
int skip_assign = in_vim9script() && arg[0] == '_'

src/evalvars.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3851,6 +3851,14 @@ set_var_const(
38513851
}
38523852

38533853
clear_tv(&di->di_tv);
3854+
3855+
if ((flags & ASSIGN_UPDATE_BLOCK_ID)
3856+
&& SCRIPT_ID_VALID(current_sctx.sc_sid))
3857+
{
3858+
scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
3859+
3860+
update_script_var_block_id(name, si->sn_current_block_id);
3861+
}
38543862
}
38553863
else
38563864
{

src/ex_eval.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,7 @@ ex_while(exarg_T *eap)
12081208
int skip;
12091209
int result;
12101210
cstack_T *cstack = eap->cstack;
1211+
int prev_cs_flags = 0;
12111212

12121213
if (cstack->cs_idx == CSTACK_LEN - 1)
12131214
eap->errmsg = _(e_while_for_nesting_too_deep);
@@ -1261,6 +1262,7 @@ ex_while(exarg_T *eap)
12611262
si->sn_current_block_id = si->sn_last_block_id;
12621263
}
12631264
}
1265+
prev_cs_flags = cstack->cs_flags[cstack->cs_idx];
12641266
cstack->cs_flags[cstack->cs_idx] =
12651267
eap->cmdidx == CMD_while ? CSF_WHILE : CSF_FOR;
12661268

@@ -1279,7 +1281,7 @@ ex_while(exarg_T *eap)
12791281
}
12801282
else
12811283
{
1282-
void *fi;
1284+
forinfo_T *fi;
12831285
evalarg_T evalarg;
12841286

12851287
/*
@@ -1313,9 +1315,18 @@ ex_while(exarg_T *eap)
13131315
result = next_for_item(fi, eap->arg);
13141316
else
13151317
result = FALSE;
1318+
if (fi != NULL)
1319+
// OR all the cs_flags together, if a function was defined in
1320+
// any round then the loop variable may have been used.
1321+
fi->fi_cs_flags |= prev_cs_flags;
13161322

13171323
if (!result)
13181324
{
1325+
// If a function was defined in any round then set the
1326+
// CSF_FUNC_DEF flag now, so that it's seen by leave_block().
1327+
if (fi != NULL && (fi->fi_cs_flags & CSF_FUNC_DEF))
1328+
cstack->cs_flags[cstack->cs_idx] |= CSF_FUNC_DEF;
1329+
13191330
free_for_info(fi);
13201331
cstack->cs_forinfo[cstack->cs_idx] = NULL;
13211332
}

src/proto/vim9compile.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* vim9compile.c */
22
int lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx);
33
int arg_exists(char_u *name, size_t len, int *idxp, type_T **type, int *gen_load_outer, cctx_T *cctx);
4+
void update_script_var_block_id(char_u *name, int block_id);
45
int script_is_vim9(void);
56
int script_var_exists(char_u *name, size_t len, cctx_T *cctx, cstack_T *cstack);
67
int check_defined(char_u *p, size_t len, cctx_T *cctx, cstack_T *cstack, int is_arg);

src/structs.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,6 +1626,23 @@ typedef enum {
16261626
typedef struct svar_S svar_T;
16271627

16281628
#if defined(FEAT_EVAL) || defined(PROTO)
1629+
/*
1630+
* Info used by a ":for" loop.
1631+
*/
1632+
typedef struct
1633+
{
1634+
int fi_semicolon; // TRUE if ending in '; var]'
1635+
int fi_varcount; // nr of variables in the list
1636+
int fi_break_count; // nr of line breaks encountered
1637+
listwatch_T fi_lw; // keep an eye on the item used.
1638+
list_T *fi_list; // list being used
1639+
int fi_bi; // index of blob
1640+
blob_T *fi_blob; // blob being used
1641+
char_u *fi_string; // copy of string being used
1642+
int fi_byte_idx; // byte index in fi_string
1643+
int fi_cs_flags; // cs_flags or'ed together
1644+
} forinfo_T;
1645+
16291646
typedef struct funccall_S funccall_T;
16301647

16311648
// values used for "uf_def_status"
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
|~+0#4040ff13#ffffff0| @73
1+
> +0&#ffffff0@74
2+
|~+0#4040ff13&| @73
23
|~| @73
3-
|E+0#ffffff16#e000002|r@1|o|r| |d|e|t|e|c|t|e|d| |w|h|i|l|e| |p|r|o|c|e|s@1|i|n|g| |f|u|n|c|t|i|o|n| |<|l|a|m|b|d|a|>|1|:| +0#0000000#ffffff0@23
4-
|l+0#af5f00255&|i|n|e| @3|1|:| +0#0000000&@64
5-
|E+0#ffffff16#e000002|1|3|0|2|:| |S|c|r|i|p|t| |v|a|r|i|a|b|l|e| |w|a|s| |d|e|l|e|t|e|d| +0#0000000#ffffff0@40
6-
|P+0#00e0003&|r|e|s@1| |E|N|T|E|R| |o|r| |t|y|p|e| |c|o|m@1|a|n|d| |t|o| |c|o|n|t|i|n|u|e> +0#0000000&@35
4+
|~| @73
5+
|~| @73
6+
|0+0#0000000&| @55|0|,|0|-|1| @8|A|l@1|

src/testdir/test_vim9_func.vim

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2930,8 +2930,10 @@ enddef
29302930
def Run_Test_closure_in_for_loop_fails()
29312931
var lines =<< trim END
29322932
vim9script
2933+
redraw
29332934
for n in [0]
2934-
timer_start(10, (_) => {
2935+
# time should be enough for startup to finish
2936+
timer_start(200, (_) => {
29352937
echo n
29362938
})
29372939
endfor
@@ -2940,7 +2942,7 @@ def Run_Test_closure_in_for_loop_fails()
29402942

29412943
# Check that an error shows
29422944
var buf = g:RunVimInTerminal('-S XTest_closure_fails', {rows: 6, wait_for_ruler: 0})
2943-
g:VerifyScreenDump(buf, 'Test_vim9_closure_fails', {})
2945+
g:VerifyScreenDump(buf, 'Test_vim9_closure_fails', {wait: 3000})
29442946

29452947
# clean up
29462948
g:StopVimInTerminal(buf)

src/testdir/test_vim9_script.vim

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,7 +2259,21 @@ def Test_for_loop()
22592259
enddef
22602260

22612261
def Test_for_loop_with_closure()
2262+
# using the loop variable in a closure results in the last used value
22622263
var lines =<< trim END
2264+
var flist: list<func>
2265+
for i in range(5)
2266+
flist[i] = () => i
2267+
endfor
2268+
for i in range(5)
2269+
assert_equal(4, flist[i]())
2270+
endfor
2271+
END
2272+
v9.CheckDefAndScriptSuccess(lines)
2273+
2274+
# using a local variable set to the loop variable in a closure results in the
2275+
# value at that moment
2276+
lines =<< trim END
22632277
var flist: list<func>
22642278
for i in range(5)
22652279
var inloop = i

src/version.c

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

704704
static int included_patches[] =
705705
{ /* Add new patch number below this line */
706+
/**/
707+
460,
706708
/**/
707709
459,
708710
/**/

src/vim.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2258,6 +2258,7 @@ typedef enum {
22582258
#define ASSIGN_NO_MEMBER_TYPE 0x20 // use "any" for list and dict member type
22592259
#define ASSIGN_FOR_LOOP 0x40 // assigning to loop variable
22602260
#define ASSIGN_INIT 0x80 // not assigning a value, just a declaration
2261+
#define ASSIGN_UPDATE_BLOCK_ID 0x100 // update sav_block_id
22612262

22622263
#include "ex_cmds.h" // Ex command defines
22632264
#include "spell.h" // spell checking stuff

0 commit comments

Comments
 (0)