patch 9.0.1108: type error when using "any" type and adding to float
Problem: Type error when using "any" type and adding a number to a float.
Solution: Accept both a number and a float. (closes #11753)
diff --git a/src/vim9instr.c b/src/vim9instr.c
index 4a22d18..b4c66e3 100644
--- a/src/vim9instr.c
+++ b/src/vim9instr.c
@@ -576,6 +576,7 @@
generate_TYPECHECK(
cctx_T *cctx,
type_T *expected,
+ int number_ok, // add TTFLAG_NUMBER_OK flag
int offset,
int is_var,
int argidx)
@@ -585,7 +586,21 @@
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
return FAIL;
- isn->isn_arg.type.ct_type = alloc_type(expected);
+ type_T *tt;
+ if (expected->tt_type == VAR_FLOAT && number_ok)
+ {
+ // always allocate, also for static types
+ tt = ALLOC_ONE(type_T);
+ if (tt != NULL)
+ {
+ *tt = *expected;
+ tt->tt_flags |= TTFLAG_NUMBER_OK;
+ }
+ }
+ else
+ tt = alloc_type(expected);
+
+ isn->isn_arg.type.ct_type = tt;
isn->isn_arg.type.ct_off = (int8_T)offset;
isn->isn_arg.type.ct_is_var = is_var;
isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
@@ -1601,7 +1616,7 @@
if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
&& maptype[0].type_decl->tt_member != &t_any)
// Check that map() didn't change the item types.
- generate_TYPECHECK(cctx, maptype[0].type_decl, -1, FALSE, 1);
+ generate_TYPECHECK(cctx, maptype[0].type_decl, FALSE, -1, FALSE, 1);
return OK;
}
@@ -1625,7 +1640,7 @@
return FAIL;
item_type = get_type_on_stack(cctx, 0);
expected = list_type->tt_member;
- if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
+ if (need_type(item_type, expected, FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
return FAIL;
if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
@@ -1648,7 +1663,8 @@
if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL)
return FAIL;
item_type = get_type_on_stack(cctx, 0);
- if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
+ if (need_type(item_type, &t_number, FALSE,
+ -1, 0, cctx, FALSE, FALSE) == FAIL)
return FAIL;
if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
@@ -1713,8 +1729,8 @@
expected = &t_any;
else
expected = ufunc->uf_va_type->tt_member;
- if (need_type(actual, expected, -argcount + i, i + 1, cctx,
- TRUE, FALSE) == FAIL)
+ if (need_type(actual, expected, FALSE,
+ -argcount + i, i + 1, cctx, TRUE, FALSE) == FAIL)
{
arg_type_mismatch(expected, actual, i + 1);
return FAIL;
@@ -1821,8 +1837,8 @@
expected = &t_any;
else
expected = type->tt_args[i];
- if (need_type(actual, expected, offset, i + 1,
- cctx, TRUE, FALSE) == FAIL)
+ if (need_type(actual, expected, FALSE,
+ offset, i + 1, cctx, TRUE, FALSE) == FAIL)
{
arg_type_mismatch(expected, actual, i + 1);
return FAIL;