0
0
mirror of https://github.com/vim/vim.git synced 2025-09-24 03:44:06 -04:00

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.
This commit is contained in:
Bram Moolenaar
2022-09-14 00:30:51 +01:00
parent 353b68a991
commit 766ae5b252
11 changed files with 86 additions and 25 deletions

View File

@@ -29,22 +29,6 @@
*/ */
static int current_copyID = 0; static int current_copyID = 0;
/*
* Info used by a ":for" loop.
*/
typedef struct
{
int fi_semicolon; // TRUE if ending in '; var]'
int fi_varcount; // nr of variables in the list
int fi_break_count; // nr of line breaks encountered
listwatch_T fi_lw; // keep an eye on the item used.
list_T *fi_list; // list being used
int fi_bi; // index of blob
blob_T *fi_blob; // blob being used
char_u *fi_string; // copy of string being used
int fi_byte_idx; // byte index in fi_string
} forinfo_T;
static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg); static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg); static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg); 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)
? (ASSIGN_FINAL ? (ASSIGN_FINAL
// first round: error if variable exists // first round: error if variable exists
| (fi->fi_bi == 0 ? 0 : ASSIGN_DECL) | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
| ASSIGN_NO_MEMBER_TYPE) | ASSIGN_NO_MEMBER_TYPE
| ASSIGN_UPDATE_BLOCK_ID)
: 0); : 0);
listitem_T *item; listitem_T *item;
int skip_assign = in_vim9script() && arg[0] == '_' int skip_assign = in_vim9script() && arg[0] == '_'

View File

@@ -3851,6 +3851,14 @@ set_var_const(
} }
clear_tv(&di->di_tv); clear_tv(&di->di_tv);
if ((flags & ASSIGN_UPDATE_BLOCK_ID)
&& SCRIPT_ID_VALID(current_sctx.sc_sid))
{
scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
update_script_var_block_id(name, si->sn_current_block_id);
}
} }
else else
{ {

View File

@@ -1208,6 +1208,7 @@ ex_while(exarg_T *eap)
int skip; int skip;
int result; int result;
cstack_T *cstack = eap->cstack; cstack_T *cstack = eap->cstack;
int prev_cs_flags = 0;
if (cstack->cs_idx == CSTACK_LEN - 1) if (cstack->cs_idx == CSTACK_LEN - 1)
eap->errmsg = _(e_while_for_nesting_too_deep); eap->errmsg = _(e_while_for_nesting_too_deep);
@@ -1261,6 +1262,7 @@ ex_while(exarg_T *eap)
si->sn_current_block_id = si->sn_last_block_id; si->sn_current_block_id = si->sn_last_block_id;
} }
} }
prev_cs_flags = cstack->cs_flags[cstack->cs_idx];
cstack->cs_flags[cstack->cs_idx] = cstack->cs_flags[cstack->cs_idx] =
eap->cmdidx == CMD_while ? CSF_WHILE : CSF_FOR; eap->cmdidx == CMD_while ? CSF_WHILE : CSF_FOR;
@@ -1279,7 +1281,7 @@ ex_while(exarg_T *eap)
} }
else else
{ {
void *fi; forinfo_T *fi;
evalarg_T evalarg; evalarg_T evalarg;
/* /*
@@ -1313,9 +1315,18 @@ ex_while(exarg_T *eap)
result = next_for_item(fi, eap->arg); result = next_for_item(fi, eap->arg);
else else
result = FALSE; result = FALSE;
if (fi != NULL)
// OR all the cs_flags together, if a function was defined in
// any round then the loop variable may have been used.
fi->fi_cs_flags |= prev_cs_flags;
if (!result) if (!result)
{ {
// If a function was defined in any round then set the
// CSF_FUNC_DEF flag now, so that it's seen by leave_block().
if (fi != NULL && (fi->fi_cs_flags & CSF_FUNC_DEF))
cstack->cs_flags[cstack->cs_idx] |= CSF_FUNC_DEF;
free_for_info(fi); free_for_info(fi);
cstack->cs_forinfo[cstack->cs_idx] = NULL; cstack->cs_forinfo[cstack->cs_idx] = NULL;
} }

View File

@@ -1,6 +1,7 @@
/* vim9compile.c */ /* vim9compile.c */
int lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx); int lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx);
int arg_exists(char_u *name, size_t len, int *idxp, type_T **type, int *gen_load_outer, cctx_T *cctx); int arg_exists(char_u *name, size_t len, int *idxp, type_T **type, int *gen_load_outer, cctx_T *cctx);
void update_script_var_block_id(char_u *name, int block_id);
int script_is_vim9(void); int script_is_vim9(void);
int script_var_exists(char_u *name, size_t len, cctx_T *cctx, cstack_T *cstack); int script_var_exists(char_u *name, size_t len, cctx_T *cctx, cstack_T *cstack);
int check_defined(char_u *p, size_t len, cctx_T *cctx, cstack_T *cstack, int is_arg); int check_defined(char_u *p, size_t len, cctx_T *cctx, cstack_T *cstack, int is_arg);

View File

@@ -1626,6 +1626,23 @@ typedef enum {
typedef struct svar_S svar_T; typedef struct svar_S svar_T;
#if defined(FEAT_EVAL) || defined(PROTO) #if defined(FEAT_EVAL) || defined(PROTO)
/*
* Info used by a ":for" loop.
*/
typedef struct
{
int fi_semicolon; // TRUE if ending in '; var]'
int fi_varcount; // nr of variables in the list
int fi_break_count; // nr of line breaks encountered
listwatch_T fi_lw; // keep an eye on the item used.
list_T *fi_list; // list being used
int fi_bi; // index of blob
blob_T *fi_blob; // blob being used
char_u *fi_string; // copy of string being used
int fi_byte_idx; // byte index in fi_string
int fi_cs_flags; // cs_flags or'ed together
} forinfo_T;
typedef struct funccall_S funccall_T; typedef struct funccall_S funccall_T;
// values used for "uf_def_status" // values used for "uf_def_status"

View File

@@ -1,6 +1,6 @@
|~+0#4040ff13#ffffff0| @73 > +0&#ffffff0@74
|~+0#4040ff13&| @73
|~| @73 |~| @73
|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 |~| @73
|l+0#af5f00255&|i|n|e| @3|1|:| +0#0000000&@64 |~| @73
|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 |0+0#0000000&| @55|0|,|0|-|1| @8|A|l@1|
|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

View File

@@ -2930,8 +2930,10 @@ enddef
def Run_Test_closure_in_for_loop_fails() def Run_Test_closure_in_for_loop_fails()
var lines =<< trim END var lines =<< trim END
vim9script vim9script
redraw
for n in [0] for n in [0]
timer_start(10, (_) => { # time should be enough for startup to finish
timer_start(200, (_) => {
echo n echo n
}) })
endfor endfor
@@ -2940,7 +2942,7 @@ def Run_Test_closure_in_for_loop_fails()
# Check that an error shows # Check that an error shows
var buf = g:RunVimInTerminal('-S XTest_closure_fails', {rows: 6, wait_for_ruler: 0}) var buf = g:RunVimInTerminal('-S XTest_closure_fails', {rows: 6, wait_for_ruler: 0})
g:VerifyScreenDump(buf, 'Test_vim9_closure_fails', {}) g:VerifyScreenDump(buf, 'Test_vim9_closure_fails', {wait: 3000})
# clean up # clean up
g:StopVimInTerminal(buf) g:StopVimInTerminal(buf)

View File

@@ -2259,7 +2259,21 @@ def Test_for_loop()
enddef enddef
def Test_for_loop_with_closure() def Test_for_loop_with_closure()
# using the loop variable in a closure results in the last used value
var lines =<< trim END var lines =<< trim END
var flist: list<func>
for i in range(5)
flist[i] = () => i
endfor
for i in range(5)
assert_equal(4, flist[i]())
endfor
END
v9.CheckDefAndScriptSuccess(lines)
# using a local variable set to the loop variable in a closure results in the
# value at that moment
lines =<< trim END
var flist: list<func> var flist: list<func>
for i in range(5) for i in range(5)
var inloop = i var inloop = i

View File

@@ -703,6 +703,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 */
/**/
460,
/**/ /**/
459, 459,
/**/ /**/

View File

@@ -2258,6 +2258,7 @@ typedef enum {
#define ASSIGN_NO_MEMBER_TYPE 0x20 // use "any" for list and dict member type #define ASSIGN_NO_MEMBER_TYPE 0x20 // use "any" for list and dict member type
#define ASSIGN_FOR_LOOP 0x40 // assigning to loop variable #define ASSIGN_FOR_LOOP 0x40 // assigning to loop variable
#define ASSIGN_INIT 0x80 // not assigning a value, just a declaration #define ASSIGN_INIT 0x80 // not assigning a value, just a declaration
#define ASSIGN_UPDATE_BLOCK_ID 0x100 // update sav_block_id
#include "ex_cmds.h" // Ex command defines #include "ex_cmds.h" // Ex command defines
#include "spell.h" // spell checking stuff #include "spell.h" // spell checking stuff

View File

@@ -183,6 +183,9 @@ find_script_var(char_u *name, size_t len, cctx_T *cctx, cstack_T *cstack)
if (cctx == NULL) if (cctx == NULL)
{ {
if (cstack == NULL)
return NULL;
// Not in a function scope, find variable with block ID equal to or // Not in a function scope, find variable with block ID equal to or
// smaller than the current block id. Use "cstack" to go up the block // smaller than the current block id. Use "cstack" to go up the block
// scopes. // scopes.
@@ -219,6 +222,23 @@ find_script_var(char_u *name, size_t len, cctx_T *cctx, cstack_T *cstack)
return NULL; return NULL;
} }
/*
* If "name" can be found in the current script set it's "block_id".
*/
void
update_script_var_block_id(char_u *name, int block_id)
{
scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
hashitem_T *hi;
sallvar_T *sav;
hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
if (HASHITEM_EMPTY(hi))
return;
sav = HI2SAV(hi);
sav->sav_block_id = block_id;
}
/* /*
* Return TRUE if the script context is Vim9 script. * Return TRUE if the script context is Vim9 script.
*/ */