To: vim_dev@googlegroups.com Subject: Patch 8.2.3222 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ 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 \d\+(_: any): number\n1 return 0\n enddef', body) enddef ! def Test_lamba_block_variable() var lines =<< trim END vim9script var flist: list --- 2352,2358 ---- assert_match('def \d\+(_: any): number\n1 return 0\n enddef', body) enddef ! def Test_lambda_block_variable() var lines =<< trim END vim9script var flist: list *************** *** 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 -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// \\\ \\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///