]> granicus.if.org Git - vim/commitdiff
patch 9.0.0460: loop variable can't be found v9.0.0460
authorBram Moolenaar <Bram@vim.org>
Tue, 13 Sep 2022 23:30:51 +0000 (00:30 +0100)
committerBram Moolenaar <Bram@vim.org>
Tue, 13 Sep 2022 23:30:51 +0000 (00:30 +0100)
Problem:    Loop variable can't be found.
Solution:   Adjust block_id of the loop variable each round.

src/eval.c
src/evalvars.c
src/ex_eval.c
src/proto/vim9compile.pro
src/structs.h
src/testdir/dumps/Test_vim9_closure_fails.dump
src/testdir/test_vim9_func.vim
src/testdir/test_vim9_script.vim
src/version.c
src/vim.h
src/vim9compile.c

index 5d93b7e50c1193c3e057890a4589e9665d6fed53..d0957a145b37194d96ec9dcc401d0551765c283e 100644 (file)
  */
 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] == '_'
index 7de785bc4b85d396cf3f89a44e86280f78e8250e..e4cf40945ae053bbd4b29afacf7ed767e9ce6a89 100644 (file)
@@ -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
     {
index 4315c2a62cd4b2d302274eba483697af70da12b5..9afcb56940a74ae818735962c74e9f9500085b7b 100644 (file)
@@ -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;
            }
index e6c6f3eff25b11f354797e4b2444842917ac12a6..13b96c2240c0e028b66b403cb7216d04740839fc 100644 (file)
@@ -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);
index a477dc71fe7907fe852ab5824e318b5059be9e83..581b22eac62fba0ab78d0250ba2aedf8cb541d16 100644 (file)
@@ -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"
index 1189a3a9196caad373368d11fa3a1a9a77a15d37..dd0103cada9afd7597421047f2f0a55b1ef9085e 100644 (file)
@@ -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| 
index a1c58a2048a45e763a0784a814d53609276cc975..4a5546b8ebe9ae97147907ee4f4bf7c518b4ff9c 100644 (file)
@@ -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)
index fa9bb0cc206fec56f204a2e2d4e18b2f0c20f993..0f462e32437d0c20f8bdc804cc75031be3879996 100644 (file)
@@ -2259,7 +2259,21 @@ 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
index dede808bb54903e12f6baef2f4cc894de84f8785..ab132ca0c19f3e290c98affa3aee9fb52cf002eb 100644 (file)
@@ -703,6 +703,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    460,
 /**/
     459,
 /**/
index 54a858a5c94d10737f075c09a051cc5f167208be..8f1bf3335b1f35205d3ae15502c59309b18d4d19 100644 (file)
--- 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
index 02cbb25df50c287e3d67aa9a15617f4340619377..9755d970897114396b17e98c58e5e1e31d9bc9a7 100644 (file)
@@ -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.
@@ -219,6 +222,23 @@ find_script_var(char_u *name, size_t len, cctx_T *cctx, cstack_T *cstack)
     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.
  */