summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2020-07-05 21:38:11 +0200
committerBram Moolenaar <Bram@vim.org>2020-07-05 21:38:11 +0200
commit0ad3e894d75236915e67dfbbcc821b6bb3c05d91 (patch)
treef63abe7b6ceb48e23e4dc7e07c5ab7e8f1c181d5
parent67627355accff4af4f2a7e727c77ea8df675636e (diff)
patch 8.2.1145: Vim9: "for" only accepts a list at compile timev8.2.1145
Problem: Vim9: "for" only accepts a list at compile time. Solution: Also accept a list at runtime.
-rw-r--r--src/testdir/test_vim9_disassemble.vim37
-rw-r--r--src/testdir/test_vim9_script.vim8
-rw-r--r--src/version.c2
-rw-r--r--src/vim9compile.c8
4 files changed, 50 insertions, 5 deletions
diff --git a/src/testdir/test_vim9_disassemble.vim b/src/testdir/test_vim9_disassemble.vim
index 2862289e40..27d16a5802 100644
--- a/src/testdir/test_vim9_disassemble.vim
+++ b/src/testdir/test_vim9_disassemble.vim
@@ -727,6 +727,43 @@ def Test_disassemble_for_loop()
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()
diff --git a/src/testdir/test_vim9_script.vim b/src/testdir/test_vim9_script.vim
index e548717986..726ca9af6d 100644
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -1440,6 +1440,12 @@ def Test_for_loop()
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,7 +1453,7 @@ def Test_for_loop_fails()
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 "text"'], 'E1013:')
CheckDefFailure(['for i in xxx'], 'E1001:')
CheckDefFailure(['endfor'], 'E588:')
CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:')
diff --git a/src/version.c b/src/version.c
index f93678388b..b4b8b1dd43 100644
--- a/src/version.c
+++ b/src/version.c
@@ -755,6 +755,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 1145,
+/**/
1144,
/**/
1143,
diff --git a/src/vim9compile.c b/src/vim9compile.c
index ed069b740a..db24eaead6 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -6062,15 +6062,15 @@ compile_for(char_u *arg, cctx_T *cctx)
return NULL;
}
- // now we know the type of "var"
+ // 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 (vartype->tt_type != VAR_LIST)
+ if (need_type(vartype, &t_list_any, -1, cctx) == FAIL)
{
- emsg(_("E1024: need a List to iterate over"));
drop_scope(cctx);
return NULL;
}
- if (vartype->tt_member->tt_type != VAR_ANY)
+ 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