Skip to content

Commit 4270d8b

Browse files
committed
patch 8.2.3310: Vim9: unpack assignment does not mention source of type error
Problem: Vim9: unpack assignment does not mention source of type error. Solution: Mention the argument number. (closes #8719)
1 parent fbeefb1 commit 4270d8b

4 files changed

Lines changed: 47 additions & 12 deletions

File tree

src/testdir/test_vim9_assign.vim

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,22 @@ def Test_assign_unpack()
414414
[x, y] = g:values
415415
END
416416
CheckDefExecAndScriptFailure(lines, 'E1163: Variable 2: type mismatch, expected string but got number')
417+
418+
lines =<< trim END
419+
var x: number
420+
var y: number
421+
var z: string
422+
[x, y, z] = [1, 2, 3]
423+
END
424+
CheckDefAndScriptFailure(lines, 'E1163: Variable 3: type mismatch, expected string but got number')
425+
426+
lines =<< trim END
427+
var x: number
428+
var y: string
429+
var z: string
430+
[x, y, z] = [1, '2', 3]
431+
END
432+
CheckDefExecAndScriptFailure(lines, 'E1163: Variable 3: type mismatch, expected string but got number')
417433
enddef
418434

419435
def Test_assign_linebreak()

src/testdir/test_vim9_disassemble.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,10 @@ def Test_disassemble_list_assign()
441441
'\d CHECKTYPE list<any> stack\[-1\]\_s*' ..
442442
'\d CHECKLEN >= 2\_s*' ..
443443
'\d\+ ITEM 0\_s*' ..
444-
'\d\+ CHECKTYPE string stack\[-1\]\_s*' ..
444+
'\d\+ CHECKTYPE string stack\[-1\] arg 1\_s*' ..
445445
'\d\+ STORE $0\_s*' ..
446446
'\d\+ ITEM 1\_s*' ..
447-
'\d\+ CHECKTYPE string stack\[-1\]\_s*' ..
447+
'\d\+ CHECKTYPE string stack\[-1\] arg 2\_s*' ..
448448
'\d\+ STORE $1\_s*' ..
449449
'\d\+ SLICE 2\_s*' ..
450450
'\d\+ STORE $2\_s*' ..

src/version.c

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

756756
static int included_patches[] =
757757
{ /* Add new patch number below this line */
758+
/**/
759+
3310,
758760
/**/
759761
3309,
760762
/**/

src/vim9compile.c

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,18 +1039,16 @@ use_typecheck(type_T *actual, type_T *expected)
10391039
* If "actual_is_const" is TRUE then the type won't change at runtime, do not
10401040
* generate a TYPECHECK.
10411041
*/
1042-
int
1043-
need_type(
1042+
static int
1043+
need_type_where(
10441044
type_T *actual,
10451045
type_T *expected,
10461046
int offset,
1047-
int arg_idx,
1047+
where_T where,
10481048
cctx_T *cctx,
10491049
int silent,
10501050
int actual_is_const)
10511051
{
1052-
where_T where = WHERE_INIT;
1053-
10541052
if (expected == &t_bool && actual != &t_bool
10551053
&& (actual->tt_flags & TTFLAG_BOOL_OK))
10561054
{
@@ -1060,7 +1058,6 @@ need_type(
10601058
return OK;
10611059
}
10621060

1063-
where.wt_index = arg_idx;
10641061
if (check_type(expected, actual, FALSE, where) == OK)
10651062
return OK;
10661063

@@ -1069,15 +1066,32 @@ need_type(
10691066
if ((!actual_is_const || actual == &t_any)
10701067
&& use_typecheck(actual, expected))
10711068
{
1072-
generate_TYPECHECK(cctx, expected, offset, arg_idx);
1069+
generate_TYPECHECK(cctx, expected, offset, where.wt_index);
10731070
return OK;
10741071
}
10751072

10761073
if (!silent)
1077-
arg_type_mismatch(expected, actual, arg_idx);
1074+
type_mismatch_where(expected, actual, where);
10781075
return FAIL;
10791076
}
10801077

1078+
int
1079+
need_type(
1080+
type_T *actual,
1081+
type_T *expected,
1082+
int offset,
1083+
int arg_idx,
1084+
cctx_T *cctx,
1085+
int silent,
1086+
int actual_is_const)
1087+
{
1088+
where_T where = WHERE_INIT;
1089+
1090+
where.wt_index = arg_idx;
1091+
return need_type_where(actual, expected, offset, where,
1092+
cctx, silent, actual_is_const);
1093+
}
1094+
10811095
/*
10821096
* Check that the top of the type stack has a type that can be used as a
10831097
* condition. Give an error and return FAIL if not.
@@ -7004,14 +7018,17 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
70047018
else if (*op == '=')
70057019
{
70067020
type_T *use_type = lhs.lhs_lvar->lv_type;
7021+
where_T where = WHERE_INIT;
70077022

70087023
// Without operator check type here, otherwise below.
70097024
// Use the line number of the assignment.
70107025
SOURCING_LNUM = start_lnum;
7026+
where.wt_index = var_count > 0 ? var_idx + 1 : 0;
7027+
where.wt_variable = var_count > 0;
70117028
if (lhs.lhs_has_index)
70127029
use_type = lhs.lhs_member_type;
7013-
if (need_type(rhs_type, use_type, -1, 0, cctx,
7014-
FALSE, is_const) == FAIL)
7030+
if (need_type_where(rhs_type, use_type, -1, where,
7031+
cctx, FALSE, is_const) == FAIL)
70157032
goto theend;
70167033
}
70177034
}

0 commit comments

Comments
 (0)