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/vim9compile.c b/src/vim9compile.c
index 9105dcf..0d980ba 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -451,6 +451,7 @@
need_type_where(
type_T *actual,
type_T *expected,
+ int number_ok, // expect VAR_FLOAT but VAR_NUMBER is OK
int offset,
where_T where,
cctx_T *cctx,
@@ -480,7 +481,7 @@
// If the actual type can be the expected type add a runtime check.
if (!actual_is_const && ret == MAYBE && use_typecheck(actual, expected))
{
- generate_TYPECHECK(cctx, expected, offset,
+ generate_TYPECHECK(cctx, expected, number_ok, offset,
where.wt_variable, where.wt_index);
return OK;
}
@@ -494,6 +495,7 @@
need_type(
type_T *actual,
type_T *expected,
+ int number_ok, // when expected is float number is also OK
int offset,
int arg_idx,
cctx_T *cctx,
@@ -503,7 +505,7 @@
where_T where = WHERE_INIT;
where.wt_index = arg_idx;
- return need_type_where(actual, expected, offset, where,
+ return need_type_where(actual, expected, number_ok, offset, where,
cctx, silent, actual_is_const);
}
@@ -2000,8 +2002,8 @@
// now we can properly check the type
if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
&& rhs_type != &t_void
- && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
- FALSE, FALSE) == FAIL)
+ && need_type(rhs_type, lhs->lhs_type->tt_member, FALSE,
+ -2, 0, cctx, FALSE, FALSE) == FAIL)
return FAIL;
}
else
@@ -2090,13 +2092,13 @@
if (range)
{
type = get_type_on_stack(cctx, 1);
- if (need_type(type, &t_number,
+ if (need_type(type, &t_number, FALSE,
-2, 0, cctx, FALSE, FALSE) == FAIL)
return FAIL;
}
type = get_type_on_stack(cctx, 0);
if ((dest_type != VAR_BLOB && type->tt_type != VAR_SPECIAL)
- && need_type(type, &t_number,
+ && need_type(type, &t_number, FALSE,
-1, 0, cctx, FALSE, FALSE) == FAIL)
return FAIL;
}
@@ -2357,7 +2359,7 @@
emsg(_(e_cannot_use_void_value));
goto theend;
}
- if (need_type(stacktype, &t_list_any, -1, 0, cctx,
+ if (need_type(stacktype, &t_list_any, FALSE, -1, 0, cctx,
FALSE, FALSE) == FAIL)
goto theend;
// If a constant list was used we can check the length right here.
@@ -2424,7 +2426,7 @@
{
SOURCING_LNUM = start_lnum;
if (lhs.lhs_has_type
- && need_type(&t_list_string, lhs.lhs_type,
+ && need_type(&t_list_string, lhs.lhs_type, FALSE,
-1, 0, cctx, FALSE, FALSE) == FAIL)
goto theend;
}
@@ -2549,8 +2551,8 @@
&& !has_list_index(var_start + lhs.lhs_varlen,
cctx))
use_type = lhs.lhs_member_type;
- if (need_type_where(rhs_type, use_type, -1, where,
- cctx, FALSE, is_const) == FAIL)
+ if (need_type_where(rhs_type, use_type, FALSE, -1,
+ where, cctx, FALSE, is_const) == FAIL)
goto theend;
}
}
@@ -2565,7 +2567,7 @@
|| lhs_type == &t_float)
&& rhs_type->tt_type == VAR_NUMBER)
lhs_type = &t_number;
- if (*p != '=' && need_type(rhs_type, lhs_type,
+ if (*p != '=' && need_type(rhs_type, lhs_type, FALSE,
-1, 0, cctx, FALSE, FALSE) == FAIL)
goto theend;
}
@@ -2622,8 +2624,8 @@
if (
// If variable is float operation with number is OK.
!(expected == &t_float && (stacktype == &t_number
- || stacktype == &t_number_bool)) &&
- need_type(stacktype, expected, -1, 0, cctx,
+ || stacktype == &t_number_bool))
+ && need_type(stacktype, expected, TRUE, -1, 0, cctx,
FALSE, FALSE) == FAIL)
goto theend;
}
@@ -3104,7 +3106,7 @@
ufunc->uf_arg_types[arg_idx] = val_type;
}
else if (need_type_where(val_type, ufunc->uf_arg_types[arg_idx],
- -1, where, &cctx, FALSE, FALSE) == FAIL)
+ FALSE, -1, where, &cctx, FALSE, FALSE) == FAIL)
goto erret;
if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)