To: vim_dev@googlegroups.com Subject: Patch 8.2.1145 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.1145 Problem: Vim9: "for" only accepts a list at compile time. Solution: Also accept a list at runtime. Files: src/vim9compile.c, src/testdir/test_vim9_script.vim, src/testdir/test_vim9_disassemble.vim *** ../vim-8.2.1144/src/vim9compile.c 2020-07-05 18:18:24.342937456 +0200 --- src/vim9compile.c 2020-07-05 21:19:33.030489321 +0200 *************** *** 6062,6076 **** return NULL; } ! // now we know the type of "var" vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; ! if (vartype->tt_type != VAR_LIST) { - emsg(_("E1024: need a List to iterate over")); drop_scope(cctx); return NULL; } ! if (vartype->tt_member->tt_type != VAR_ANY) var_lvar->lv_type = vartype->tt_member; // "for_end" is set when ":endfor" is found --- 6062,6076 ---- return NULL; } ! // Now that we know the type of "var", check that it is a list, now or at ! // runtime. vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; ! if (need_type(vartype, &t_list_any, -1, cctx) == FAIL) { drop_scope(cctx); return NULL; } ! if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY) var_lvar->lv_type = vartype->tt_member; // "for_end" is set when ":endfor" is found *** ../vim-8.2.1144/src/testdir/test_vim9_script.vim 2020-07-04 17:39:07.510515698 +0200 --- src/testdir/test_vim9_script.vim 2020-07-05 21:18:44.878739277 +0200 *************** *** 1440,1445 **** --- 1440,1451 ---- result ..= cnt .. '_' endfor assert_equal('0_1_3_', result) + + let concat = '' + for str in eval('["one", "two"]') + concat ..= str + endfor + assert_equal('onetwo', concat) enddef def Test_for_loop_fails() *************** *** 1447,1453 **** CheckDefFailure(['for i In range(5)'], 'E690:') CheckDefFailure(['let x = 5', 'for x in range(5)'], 'E1023:') CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:') ! CheckDefFailure(['for i in "text"'], 'E1024:') CheckDefFailure(['for i in xxx'], 'E1001:') CheckDefFailure(['endfor'], 'E588:') CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:') --- 1453,1459 ---- CheckDefFailure(['for i In range(5)'], 'E690:') CheckDefFailure(['let x = 5', 'for x in range(5)'], 'E1023:') CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:') ! CheckDefFailure(['for i in "text"'], 'E1013:') CheckDefFailure(['for i in xxx'], 'E1001:') CheckDefFailure(['endfor'], 'E588:') CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:') *** ../vim-8.2.1144/src/testdir/test_vim9_disassemble.vim 2020-06-20 22:50:44.175608236 +0200 --- src/testdir/test_vim9_disassemble.vim 2020-07-05 21:36:26.373862958 +0200 *************** *** 727,732 **** --- 727,769 ---- instr) enddef + def ForLoopEval(): string + let res = "" + for str in eval('["one", "two"]') + res ..= str + endfor + return res + enddef + + def Test_disassemble_for_loop_eval() + assert_equal('onetwo', ForLoopEval()) + let instr = execute('disassemble ForLoopEval') + assert_match('ForLoopEval\_s*' .. + 'let res = ""\_s*' .. + '\d PUSHS ""\_s*' .. + '\d STORE $0\_s*' .. + 'for str in eval(''\["one", "two"\]'')\_s*' .. + '\d STORE -1 in $1\_s*' .. + '\d PUSHS "\["one", "two"\]"\_s*' .. + '\d BCALL eval(argc 1)\_s*' .. + '\d CHECKTYPE list stack\[-1\]\_s*' .. + '\d FOR $1 -> \d\+\_s*' .. + '\d STORE $2\_s*' .. + 'res ..= str\_s*' .. + '\d\+ LOAD $0\_s*' .. + '\d\+ LOAD $2\_s*' .. + '\d\+ CHECKTYPE string stack\[-1\]\_s*' .. + '\d\+ CONCAT\_s*' .. + '\d\+ STORE $0\_s*' .. + 'endfor\_s*' .. + '\d\+ JUMP -> 6\_s*' .. + '\d\+ DROP\_s*' .. + 'return res\_s*' .. + '\d\+ LOAD $0\_s*' .. + '\d\+ RETURN', + instr) + enddef + let g:number = 42 def Computing() *** ../vim-8.2.1144/src/version.c 2020-07-05 21:10:20.869634742 +0200 --- src/version.c 2020-07-05 21:21:41.501843581 +0200 *************** *** 756,757 **** --- 756,759 ---- { /* Add new patch number below this line */ + /**/ + 1145, /**/ -- Your company is doomed if your primary product is overhead transparencies. (Scott Adams - The Dilbert principle) /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///