Skip to content

Commit db9ff9a

Browse files
committed
patch 8.2.3717: Vim9: error for constant list size is only given at runtime
Problem: Vim9: error for constant list size is only given at runtime. Solution: Give the error at compile time if possible.
1 parent e4eed8c commit db9ff9a

File tree

5 files changed

+49
-6
lines changed

5 files changed

+49
-6
lines changed

src/testdir/test_vim9_assign.vim

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,27 @@ def Test_assign_unpack()
383383
END
384384
CheckDefAndScriptSuccess(lines)
385385

386+
lines =<< trim END
387+
var v1: number
388+
var v2: number
389+
[v1, v2] = [1, 2, 3]
390+
END
391+
CheckDefFailure(lines, 'E1093: Expected 2 items but got 3', 3)
392+
393+
lines =<< trim END
394+
var v1: number
395+
var v2: number
396+
[v1, v2] = [1]
397+
END
398+
CheckDefFailure(lines, 'E1093: Expected 2 items but got 1', 3)
399+
400+
lines =<< trim END
401+
var v1: number
402+
var v2: number
403+
[v1, v2; _] = [1]
404+
END
405+
CheckDefFailure(lines, 'E1093: Expected 2 items but got 1', 3)
406+
386407
lines =<< trim END
387408
var v1: number
388409
var v2: number

src/testdir/test_vim9_disassemble.vim

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,6 @@ def Test_disassemble_list_assign_with_op()
471471
'\d\+ PUSHNR 4\_s*' ..
472472
'\d\+ PUSHNR 5\_s*' ..
473473
'\d\+ NEWLIST size 2\_s*' ..
474-
'\d\+ CHECKLEN 2\_s*' ..
475474
'\d\+ LOAD $0\_s*' ..
476475
'\d\+ ITEM 0 with op\_s*' ..
477476
'\d\+ OPNR +\_s*' ..

src/testdir/test_vim9_script.vim

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,8 +496,9 @@ def Test_try_catch_throw()
496496
endtry
497497
assert_equal(266, n)
498498

499+
l = [1, 2, 3]
499500
try
500-
[n] = [1, 2, 3]
501+
[n] = l
501502
catch /E1093:/
502503
n = 277
503504
endtry
@@ -4327,7 +4328,8 @@ def Test_catch_exception_in_callback()
43274328
var x: string
43284329
var y: string
43294330
# this error should be caught with CHECKLEN
4330-
[x, y] = ['']
4331+
var sl = ['']
4332+
[x, y] = sl
43314333
catch
43324334
g:caught = 'yes'
43334335
endtry

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,8 @@ static char *(features[]) =
753753

754754
static int included_patches[] =
755755
{ /* Add new patch number below this line */
756+
/**/
757+
3717,
756758
/**/
757759
3716,
758760
/**/

src/vim9compile.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6999,6 +6999,8 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
69996999
if (cctx->ctx_skip != SKIP_YES)
70007000
{
70017001
type_T *stacktype;
7002+
int needed_list_len;
7003+
int did_check = FALSE;
70027004

70037005
stacktype = stack->ga_len == 0 ? &t_void
70047006
: ((type_T **)stack->ga_data)[stack->ga_len - 1];
@@ -7010,9 +7012,26 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
70107012
if (need_type(stacktype, &t_list_any, -1, 0, cctx,
70117013
FALSE, FALSE) == FAIL)
70127014
goto theend;
7013-
// TODO: check the length of a constant list here
7014-
generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
7015-
semicolon);
7015+
// If a constant list was used we can check the length right here.
7016+
needed_list_len = semicolon ? var_count - 1 : var_count;
7017+
if (instr->ga_len > 0)
7018+
{
7019+
isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
7020+
7021+
if (isn->isn_type == ISN_NEWLIST)
7022+
{
7023+
did_check = TRUE;
7024+
if (semicolon ? isn->isn_arg.number < needed_list_len
7025+
: isn->isn_arg.number != needed_list_len)
7026+
{
7027+
semsg(_(e_expected_nr_items_but_got_nr),
7028+
needed_list_len, isn->isn_arg.number);
7029+
goto theend;
7030+
}
7031+
}
7032+
}
7033+
if (!did_check)
7034+
generate_CHECKLEN(cctx, needed_list_len, semicolon);
70167035
if (stacktype->tt_member != NULL)
70177036
rhs_type = stacktype->tt_member;
70187037
}

0 commit comments

Comments
 (0)