diff options
author | Bram Moolenaar <Bram@vim.org> | 2022-10-11 21:41:25 +0100 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2022-10-11 21:41:25 +0100 |
commit | 159b2d5bfc4edc013024e8c996141d37542ebc15 (patch) | |
tree | 80b8aa817564b1cc1c45cd418b247f09d99933c5 | |
parent | 7e017461e21ac534f8a0d59c1165d11358c69d87 (diff) | |
download | vim-git-159b2d5bfc4edc013024e8c996141d37542ebc15.tar.gz |
patch 9.0.0726: looping over list of lists works in script, not in functionv9.0.0726
Problem: Looping over list of lists and changing the list contents works in
Vim9 script, not in a compiled function.
Solution: Mark the loop variable final instead of const. (closes #11347)
-rw-r--r-- | src/testdir/test_vim9_script.vim | 14 | ||||
-rw-r--r-- | src/version.c | 2 | ||||
-rw-r--r-- | src/vim9cmds.c | 2 |
3 files changed, 17 insertions, 1 deletions
diff --git a/src/testdir/test_vim9_script.vim b/src/testdir/test_vim9_script.vim index 05fd2fb57..a0b8352f3 100644 --- a/src/testdir/test_vim9_script.vim +++ b/src/testdir/test_vim9_script.vim @@ -2275,6 +2275,20 @@ def Test_for_loop() v9.CheckDefAndScriptSuccess(lines) enddef +def Test_for_loop_list_of_lists() + # loop variable is final, not const + var lines =<< trim END + # Filter out all odd numbers in each sublist + var list: list<list<number>> = [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4]] + for i in list + filter(i, (_, n: number): bool => n % 2 == 0) + endfor + + assert_equal([[], [2], [2], [2, 4]], list) + END + v9.CheckDefAndScriptSuccess(lines) +enddef + def Test_for_loop_with_closure() # using the loop variable in a closure results in the last used value var lines =<< trim END diff --git a/src/version.c b/src/version.c index aa5ca6794..251ece71d 100644 --- a/src/version.c +++ b/src/version.c @@ -700,6 +700,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 726, +/**/ 725, /**/ 724, diff --git a/src/vim9cmds.c b/src/vim9cmds.c index a1e8ac18c..73f95b26d 100644 --- a/src/vim9cmds.c +++ b/src/vim9cmds.c @@ -1048,7 +1048,7 @@ compile_for(char_u *arg_start, cctx_T *cctx) && need_type_where(item_type, lhs_type, -1, where, cctx, FALSE, FALSE) == FAIL) goto failed; - var_lvar = reserve_local(cctx, arg, varlen, ASSIGN_CONST, + var_lvar = reserve_local(cctx, arg, varlen, ASSIGN_FINAL, lhs_type); if (var_lvar == NULL) // out of memory or used as an argument |