summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2022-09-14 00:30:51 +0100
committerBram Moolenaar <Bram@vim.org>2022-09-14 00:30:51 +0100
commit766ae5b252eaa6ee2bff70f1913d1cbfb51101bd (patch)
treec8e34f8e495b00c7e36e9310ab5becb5f9924ecc
parent353b68a99189875a8460124d44fc33eae6def74e (diff)
downloadvim-git-766ae5b252eaa6ee2bff70f1913d1cbfb51101bd.tar.gz
patch 9.0.0460: loop variable can't be foundv9.0.0460
Problem: Loop variable can't be found. Solution: Adjust block_id of the loop variable each round.
-rw-r--r--src/eval.c19
-rw-r--r--src/evalvars.c8
-rw-r--r--src/ex_eval.c13
-rw-r--r--src/proto/vim9compile.pro1
-rw-r--r--src/structs.h17
-rw-r--r--src/testdir/dumps/Test_vim9_closure_fails.dump10
-rw-r--r--src/testdir/test_vim9_func.vim6
-rw-r--r--src/testdir/test_vim9_script.vim14
-rw-r--r--src/version.c2
-rw-r--r--src/vim.h1
-rw-r--r--src/vim9compile.c20
11 files changed, 86 insertions, 25 deletions
diff --git a/src/eval.c b/src/eval.c
index 5d93b7e50..d0957a145 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -29,22 +29,6 @@
*/
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 eval3(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
// first round: error if variable exists
| (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
- | ASSIGN_NO_MEMBER_TYPE)
+ | ASSIGN_NO_MEMBER_TYPE
+ | ASSIGN_UPDATE_BLOCK_ID)
: 0);
listitem_T *item;
int skip_assign = in_vim9script() && arg[0] == '_'
diff --git a/src/evalvars.c b/src/evalvars.c
index 7de785bc4..e4cf40945 100644
--- a/src/evalvars.c
+++ b/src/evalvars.c
@@ -3851,6 +3851,14 @@ set_var_const(
}
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
{
diff --git a/src/ex_eval.c b/src/ex_eval.c
index 4315c2a62..9afcb5694 100644
--- a/src/ex_eval.c
+++ b/src/ex_eval.c
@@ -1208,6 +1208,7 @@ ex_while(exarg_T *eap)
int skip;
int result;
cstack_T *cstack = eap->cstack;
+ int prev_cs_flags = 0;
if (cstack->cs_idx == CSTACK_LEN - 1)
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;
}
}
+ prev_cs_flags = cstack->cs_flags[cstack->cs_idx];
cstack->cs_flags[cstack->cs_idx] =
eap->cmdidx == CMD_while ? CSF_WHILE : CSF_FOR;
@@ -1279,7 +1281,7 @@ ex_while(exarg_T *eap)
}
else
{
- void *fi;
+ forinfo_T *fi;
evalarg_T evalarg;
/*
@@ -1313,9 +1315,18 @@ ex_while(exarg_T *eap)
result = next_for_item(fi, eap->arg);
else
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 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);
cstack->cs_forinfo[cstack->cs_idx] = NULL;
}
diff --git a/src/proto/vim9compile.pro b/src/proto/vim9compile.pro
index e6c6f3eff..13b96c224 100644
--- a/src/proto/vim9compile.pro
+++ b/src/proto/vim9compile.pro
@@ -1,6 +1,7 @@
/* vim9compile.c */
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);
+void update_script_var_block_id(char_u *name, int block_id);
int script_is_vim9(void);
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);
diff --git a/src/structs.h b/src/structs.h
index a477dc71f..581b22eac 100644
--- a/src/structs.h
+++ b/src/structs.h
@@ -1626,6 +1626,23 @@ typedef enum {
typedef struct svar_S svar_T;
#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;
// values used for "uf_def_status"
diff --git a/src/testdir/dumps/Test_vim9_closure_fails.dump b/src/testdir/dumps/Test_vim9_closure_fails.dump
index 1189a3a91..dd0103cad 100644
--- a/src/testdir/dumps/Test_vim9_closure_fails.dump
+++ b/src/testdir/dumps/Test_vim9_closure_fails.dump
@@ -1,6 +1,6 @@
-|~+0#4040ff13#ffffff0| @73
+> +0&#ffffff0@74
+|~+0#4040ff13&| @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
-|l+0#af5f00255&|i|n|e| @3|1|:| +0#0000000&@64
-|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
-|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
+|~| @73
+|~| @73
+|0+0#0000000&| @55|0|,|0|-|1| @8|A|l@1|
diff --git a/src/testdir/test_vim9_func.vim b/src/testdir/test_vim9_func.vim
index a1c58a204..4a5546b8e 100644
--- a/src/testdir/test_vim9_func.vim
+++ b/src/testdir/test_vim9_func.vim
@@ -2930,8 +2930,10 @@ enddef
def Run_Test_closure_in_for_loop_fails()
var lines =<< trim END
vim9script
+ redraw
for n in [0]
- timer_start(10, (_) => {
+ # time should be enough for startup to finish
+ timer_start(200, (_) => {
echo n
})
endfor
@@ -2940,7 +2942,7 @@ def Run_Test_closure_in_for_loop_fails()
# Check that an error shows
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
g:StopVimInTerminal(buf)
diff --git a/src/testdir/test_vim9_script.vim b/src/testdir/test_vim9_script.vim
index fa9bb0cc2..0f462e324 100644
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -2259,9 +2259,23 @@ def Test_for_loop()
enddef
def Test_for_loop_with_closure()
+ # using the loop variable in a closure results in the last used value
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>
+ for i in range(5)
var inloop = i
flist[i] = () => inloop
endfor
diff --git a/src/version.c b/src/version.c
index dede808bb..ab132ca0c 100644
--- a/src/version.c
+++ b/src/version.c
@@ -704,6 +704,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 460,
+/**/
459,
/**/
458,
diff --git a/src/vim.h b/src/vim.h
index 54a858a5c..8f1bf3335 100644
--- a/src/vim.h
+++ b/src/vim.h
@@ -2258,6 +2258,7 @@ typedef enum {
#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_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 "spell.h" // spell checking stuff
diff --git a/src/vim9compile.c b/src/vim9compile.c
index 02cbb25df..9755d9708 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -183,6 +183,9 @@ find_script_var(char_u *name, size_t len, cctx_T *cctx, cstack_T *cstack)
if (cctx == NULL)
{
+ if (cstack == NULL)
+ return NULL;
+
// 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
// scopes.
@@ -220,6 +223,23 @@ find_script_var(char_u *name, size_t len, cctx_T *cctx, cstack_T *cstack)
}
/*
+ * 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.
*/
int