patch 8.2.1865: Vim9: add() does not check type of argument
Problem: Vim9: add() does not check type of argument.
Solution: Inline the add() call. (closes #7160)
diff --git a/src/vim9compile.c b/src/vim9compile.c
index a891006..bb4807e 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -1495,6 +1495,32 @@
}
/*
+ * Generate an ISN_LISTAPPEND instruction. Works like add().
+ * Argument count is already checked.
+ */
+ static int
+generate_LISTAPPEND(cctx_T *cctx)
+{
+ garray_T *stack = &cctx->ctx_type_stack;
+ type_T *list_type;
+ type_T *item_type;
+ type_T *expected;
+
+ // Caller already checked that list_type is a list.
+ list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
+ item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
+ expected = list_type->tt_member;
+ if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
+ return FAIL;
+
+ if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
+ return FAIL;
+
+ --stack->ga_len; // drop the argument
+ return OK;
+}
+
+/*
* Generate an ISN_DCALL or ISN_UCALL instruction.
* Return FAIL if the number of arguments is wrong.
*/
@@ -2537,7 +2563,25 @@
// builtin function
idx = find_internal_func(name);
if (idx >= 0)
- res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
+ {
+ if (STRCMP(name, "add") == 0 && argcount == 2)
+ {
+ garray_T *stack = &cctx->ctx_type_stack;
+ type_T *type = ((type_T **)stack->ga_data)[
+ stack->ga_len - 2];
+
+ // TODO: also check for VAR_BLOB
+ if (type->tt_type == VAR_LIST)
+ {
+ // inline "add(list, item)" so that the type can be checked
+ res = generate_LISTAPPEND(cctx);
+ idx = -1;
+ }
+ }
+
+ if (idx >= 0)
+ res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
+ }
else
semsg(_(e_unknownfunc), namebuf);
goto theend;
@@ -7656,6 +7700,7 @@
case ISN_FOR:
case ISN_GETITEM:
case ISN_JUMP:
+ case ISN_LISTAPPEND:
case ISN_LISTINDEX:
case ISN_LISTSLICE:
case ISN_LOAD: