Skip to content

Commit 193f620

Browse files
committed
patch 8.2.1996: Vim9: invalid error for argument of extend()
Problem: Vim9: invalid error for argument of extend(). Solution: Check if the type could match. (closes #7299)
1 parent 714cbe5 commit 193f620

7 files changed

Lines changed: 36 additions & 5 deletions

File tree

src/evalfunc.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ arg_float_or_nr(type_T *type, argcontext_T *context)
295295
static int
296296
arg_number(type_T *type, argcontext_T *context)
297297
{
298-
return check_type(&t_number, type, TRUE, context->arg_idx + 1);
298+
return check_arg_type(&t_number, type, context->arg_idx + 1);
299299
}
300300

301301
/*
@@ -304,7 +304,7 @@ arg_number(type_T *type, argcontext_T *context)
304304
static int
305305
arg_string(type_T *type, argcontext_T *context)
306306
{
307-
return check_type(&t_string, type, TRUE, context->arg_idx + 1);
307+
return check_arg_type(&t_string, type, context->arg_idx + 1);
308308
}
309309

310310
/*
@@ -342,7 +342,7 @@ arg_same_as_prev(type_T *type, argcontext_T *context)
342342
{
343343
type_T *prev_type = context->arg_types[context->arg_idx - 1];
344344

345-
return check_type(prev_type, type, TRUE, context->arg_idx + 1);
345+
return check_arg_type(prev_type, type, context->arg_idx + 1);
346346
}
347347

348348
/*
@@ -363,7 +363,7 @@ arg_item_of_prev(type_T *type, argcontext_T *context)
363363
// probably VAR_ANY, can't check
364364
return OK;
365365

366-
return check_type(expected, type, TRUE, context->arg_idx + 1);
366+
return check_arg_type(expected, type, context->arg_idx + 1);
367367
}
368368

369369
/*

src/proto/vim9compile.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* vim9compile.c */
22
int check_defined(char_u *p, size_t len, cctx_T *cctx);
33
int check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2);
4+
int use_typecheck(type_T *actual, type_T *expected);
45
int get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx);
56
imported_T *find_imported(char_u *name, size_t len, cctx_T *cctx);
67
imported_T *find_imported_in_script(char_u *name, size_t len, int sid);

src/proto/vim9type.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ int check_typval_type(type_T *expected, typval_T *actual_tv, int argidx);
1515
void type_mismatch(type_T *expected, type_T *actual);
1616
void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
1717
int check_type(type_T *expected, type_T *actual, int give_msg, int argidx);
18+
int check_arg_type(type_T *expected, type_T *actual, int argidx);
1819
char_u *skip_type(char_u *start, int optional);
1920
type_T *parse_type(char_u **arg, garray_T *type_gap);
2021
void common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap);

src/testdir/test_vim9_builtin.vim

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,16 @@ def Test_extend_arg_types()
195195
assert_equal([1, 2, 3], extend([1, 2], [3]))
196196
assert_equal([3, 1, 2], extend([1, 2], [3], 0))
197197
assert_equal([1, 3, 2], extend([1, 2], [3], 1))
198+
assert_equal([1, 3, 2], extend([1, 2], [3], s:number_one))
198199

199200
assert_equal(#{a: 1, b: 2, c: 3}, extend(#{a: 1, b: 2}, #{c: 3}))
200201
assert_equal(#{a: 1, b: 4}, extend(#{a: 1, b: 2}, #{b: 4}))
201202
assert_equal(#{a: 1, b: 2}, extend(#{a: 1, b: 2}, #{b: 4}, 'keep'))
203+
assert_equal(#{a: 1, b: 2}, extend(#{a: 1, b: 2}, #{b: 4}, s:string_keep))
204+
205+
var res: list<dict<any>>
206+
extend(res, map([1, 2], {_, v -> {}}))
207+
assert_equal([{}, {}], res)
202208

203209
CheckDefFailure(['extend([1, 2], 3)'], 'E1013: Argument 2: type mismatch, expected list<number> but got number')
204210
CheckDefFailure(['extend([1, 2], ["x"])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>')
@@ -338,6 +344,10 @@ def Test_index()
338344
index(['a', 'b', 'a', 'B'], 'b', 2, true)->assert_equal(3)
339345
enddef
340346

347+
let s:number_one = 1
348+
let s:number_two = 2
349+
let s:string_keep = 'keep'
350+
341351
def Test_insert()
342352
var l = insert([2, 1], 3)
343353
var res = 0
@@ -347,9 +357,12 @@ def Test_insert()
347357
res->assert_equal(6)
348358

349359
assert_equal([1, 2, 3], insert([2, 3], 1))
360+
assert_equal([1, 2, 3], insert([2, 3], s:number_one))
350361
assert_equal([1, 2, 3], insert([1, 2], 3, 2))
362+
assert_equal([1, 2, 3], insert([1, 2], 3, s:number_two))
351363
assert_equal(['a', 'b', 'c'], insert(['b', 'c'], 'a'))
352364
assert_equal(0z1234, insert(0z34, 0x12))
365+
353366
CheckDefFailure(['insert([2, 3], "a")'], 'E1013: Argument 2: type mismatch, expected number but got string', 1)
354367
CheckDefFailure(['insert([2, 3], 1, "x")'], 'E1013: Argument 3: type mismatch, expected number but got string', 1)
355368
enddef

src/version.c

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

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
1996,
753755
/**/
754756
1995,
755757
/**/

src/vim9compile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ generate_TYPECHECK(
820820
* Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
821821
* used. Return FALSE if the types will never match.
822822
*/
823-
static int
823+
int
824824
use_typecheck(type_T *actual, type_T *expected)
825825
{
826826
if (actual->tt_type == VAR_ANY

src/vim9type.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,20 @@ check_type(type_T *expected, type_T *actual, int give_msg, int argidx)
516516
return ret;
517517
}
518518

519+
/*
520+
* Like check_type() but also allow for a runtime type check. E.g. "any" can be
521+
* used for "number".
522+
*/
523+
int
524+
check_arg_type(type_T *expected, type_T *actual, int argidx)
525+
{
526+
if (check_type(expected, actual, FALSE, 0) == OK
527+
|| use_typecheck(actual, expected))
528+
return OK;
529+
// TODO: should generate a TYPECHECK instruction.
530+
return check_type(expected, actual, TRUE, argidx);
531+
}
532+
519533
/*
520534
* Skip over a type definition and return a pointer to just after it.
521535
* When "optional" is TRUE then a leading "?" is accepted.

0 commit comments

Comments
 (0)