Patch 8.2.3222
Problem:    Vim9: cannot used loop variable later as lambda argument.
Solution:   When not in function context check the current block ID.
            (closes #8637)
Files:      src/vim9compile.c, src/testdir/test_vim9_func.vim


*** ../vim-8.2.3221/src/vim9compile.c   2021-07-24 15:44:26.877784139 +0200
--- src/vim9compile.c   2021-07-25 17:55:33.381047872 +0200
***************
*** 355,364 ****
        return NULL;
  
      sav = HI2SAV(hi);
!     if (sav->sav_block_id == 0 || cctx == NULL)
!       // variable defined in the script scope or not in a function.
        return sav;
  
      // Go over the variables with this name and find one that was visible
      // from the function.
      ufunc = cctx->ctx_ufunc;
--- 355,377 ----
        return NULL;
  
      sav = HI2SAV(hi);
!     if (sav->sav_block_id == 0)
!       // variable defined in the top script scope is always visible
        return sav;
  
+     if (cctx == NULL)
+     {
+       // Not in a function scope, find variable with block id equal to or
+       // smaller than the current block id.
+       while (sav != NULL)
+       {
+           if (sav->sav_block_id <= si->sn_current_block_id)
+               break;
+           sav = sav->sav_next;
+       }
+       return sav;
+     }
+ 
      // Go over the variables with this name and find one that was visible
      // from the function.
      ufunc = cctx->ctx_ufunc;
*** ../vim-8.2.3221/src/testdir/test_vim9_func.vim      2021-07-24 
14:14:42.178904987 +0200
--- src/testdir/test_vim9_func.vim      2021-07-25 17:58:25.940588641 +0200
***************
*** 2352,2358 ****
    assert_match('def <lambda>\d\+(_: any): number\n1  return 0\n   enddef', 
body)
  enddef
  
! def Test_lamba_block_variable()
    var lines =<< trim END
        vim9script
        var flist: list<func>
--- 2352,2358 ----
    assert_match('def <lambda>\d\+(_: any): number\n1  return 0\n   enddef', 
body)
  enddef
  
! def Test_lambda_block_variable()
    var lines =<< trim END
        vim9script
        var flist: list<func>
***************
*** 2386,2391 ****
--- 2386,2400 ----
        endfor
    END
    CheckScriptFailure(lines, 'E1001: Variable not found: outloop', 1)
+ 
+   lines =<< trim END
+       vim9script
+       for i in range(10)
+         var Ref = () => 0
+       endfor
+       assert_equal(0, ((i) => 0)(0))
+   END
+   CheckScriptSuccess(lines)
  enddef
  
  def Test_legacy_lambda()
*** ../vim-8.2.3221/src/version.c       2021-07-25 15:57:29.218219920 +0200
--- src/version.c       2021-07-25 18:00:07.688320969 +0200
***************
*** 757,758 ****
--- 757,760 ----
  {   /* Add new patch number below this line */
+ /**/
+     3222,
  /**/

-- 
Google is kind of like Dr. Who's Tardis; it's weirder on the
inside than on the outside...

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/202107251608.16PG8RYD1112307%40masaka.moolenaar.net.

Raspunde prin e-mail lui