0
0
mirror of https://github.com/vim/vim.git synced 2025-09-23 03:43:49 -04:00

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)
This commit is contained in:
Bram Moolenaar
2020-11-16 20:08:35 +01:00
parent 714cbe5b21
commit 193f6201b4
7 changed files with 36 additions and 5 deletions

View File

@@ -295,7 +295,7 @@ arg_float_or_nr(type_T *type, argcontext_T *context)
static int
arg_number(type_T *type, argcontext_T *context)
{
return check_type(&t_number, type, TRUE, context->arg_idx + 1);
return check_arg_type(&t_number, type, context->arg_idx + 1);
}
/*
@@ -304,7 +304,7 @@ arg_number(type_T *type, argcontext_T *context)
static int
arg_string(type_T *type, argcontext_T *context)
{
return check_type(&t_string, type, TRUE, context->arg_idx + 1);
return check_arg_type(&t_string, type, context->arg_idx + 1);
}
/*
@@ -342,7 +342,7 @@ arg_same_as_prev(type_T *type, argcontext_T *context)
{
type_T *prev_type = context->arg_types[context->arg_idx - 1];
return check_type(prev_type, type, TRUE, context->arg_idx + 1);
return check_arg_type(prev_type, type, context->arg_idx + 1);
}
/*
@@ -363,7 +363,7 @@ arg_item_of_prev(type_T *type, argcontext_T *context)
// probably VAR_ANY, can't check
return OK;
return check_type(expected, type, TRUE, context->arg_idx + 1);
return check_arg_type(expected, type, context->arg_idx + 1);
}
/*

View File

@@ -1,6 +1,7 @@
/* vim9compile.c */
int check_defined(char_u *p, size_t len, cctx_T *cctx);
int check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2);
int use_typecheck(type_T *actual, type_T *expected);
int get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx);
imported_T *find_imported(char_u *name, size_t len, cctx_T *cctx);
imported_T *find_imported_in_script(char_u *name, size_t len, int sid);

View File

@@ -15,6 +15,7 @@ int check_typval_type(type_T *expected, typval_T *actual_tv, int argidx);
void type_mismatch(type_T *expected, type_T *actual);
void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
int check_type(type_T *expected, type_T *actual, int give_msg, int argidx);
int check_arg_type(type_T *expected, type_T *actual, int argidx);
char_u *skip_type(char_u *start, int optional);
type_T *parse_type(char_u **arg, garray_T *type_gap);
void common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap);

View File

@@ -195,10 +195,16 @@ def Test_extend_arg_types()
assert_equal([1, 2, 3], extend([1, 2], [3]))
assert_equal([3, 1, 2], extend([1, 2], [3], 0))
assert_equal([1, 3, 2], extend([1, 2], [3], 1))
assert_equal([1, 3, 2], extend([1, 2], [3], s:number_one))
assert_equal(#{a: 1, b: 2, c: 3}, extend(#{a: 1, b: 2}, #{c: 3}))
assert_equal(#{a: 1, b: 4}, extend(#{a: 1, b: 2}, #{b: 4}))
assert_equal(#{a: 1, b: 2}, extend(#{a: 1, b: 2}, #{b: 4}, 'keep'))
assert_equal(#{a: 1, b: 2}, extend(#{a: 1, b: 2}, #{b: 4}, s:string_keep))
var res: list<dict<any>>
extend(res, map([1, 2], {_, v -> {}}))
assert_equal([{}, {}], res)
CheckDefFailure(['extend([1, 2], 3)'], 'E1013: Argument 2: type mismatch, expected list<number> but got number')
CheckDefFailure(['extend([1, 2], ["x"])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>')
@@ -338,6 +344,10 @@ def Test_index()
index(['a', 'b', 'a', 'B'], 'b', 2, true)->assert_equal(3)
enddef
let s:number_one = 1
let s:number_two = 2
let s:string_keep = 'keep'
def Test_insert()
var l = insert([2, 1], 3)
var res = 0
@@ -347,9 +357,12 @@ def Test_insert()
res->assert_equal(6)
assert_equal([1, 2, 3], insert([2, 3], 1))
assert_equal([1, 2, 3], insert([2, 3], s:number_one))
assert_equal([1, 2, 3], insert([1, 2], 3, 2))
assert_equal([1, 2, 3], insert([1, 2], 3, s:number_two))
assert_equal(['a', 'b', 'c'], insert(['b', 'c'], 'a'))
assert_equal(0z1234, insert(0z34, 0x12))
CheckDefFailure(['insert([2, 3], "a")'], 'E1013: Argument 2: type mismatch, expected number but got string', 1)
CheckDefFailure(['insert([2, 3], 1, "x")'], 'E1013: Argument 3: type mismatch, expected number but got string', 1)
enddef

View File

@@ -750,6 +750,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
1996,
/**/
1995,
/**/

View File

@@ -820,7 +820,7 @@ generate_TYPECHECK(
* Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
* used. Return FALSE if the types will never match.
*/
static int
int
use_typecheck(type_T *actual, type_T *expected)
{
if (actual->tt_type == VAR_ANY

View File

@@ -516,6 +516,20 @@ check_type(type_T *expected, type_T *actual, int give_msg, int argidx)
return ret;
}
/*
* Like check_type() but also allow for a runtime type check. E.g. "any" can be
* used for "number".
*/
int
check_arg_type(type_T *expected, type_T *actual, int argidx)
{
if (check_type(expected, actual, FALSE, 0) == OK
|| use_typecheck(actual, expected))
return OK;
// TODO: should generate a TYPECHECK instruction.
return check_type(expected, actual, TRUE, argidx);
}
/*
* Skip over a type definition and return a pointer to just after it.
* When "optional" is TRUE then a leading "?" is accepted.