patch 8.2.3435: Vim9: dict is not passed to dict function
Problem: Vim9: dict is not passed to dict function.
Solution: Keep the dict used until a function call.
diff --git a/src/vim9compile.c b/src/vim9compile.c
index e7c7c07..4807e3a 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -2878,9 +2878,10 @@
/*
* Compile getting a member from a list/dict/string/blob. Stack has the
* indexable value and the index or the two indexes of a slice.
+ * "keeping_dict" is used for dict[func](arg) to pass dict to func.
*/
static int
-compile_member(int is_slice, cctx_T *cctx)
+compile_member(int is_slice, int *keeping_dict, cctx_T *cctx)
{
type_T **typep;
garray_T *stack = &cctx->ctx_type_stack;
@@ -2935,6 +2936,8 @@
return FAIL;
if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
return FAIL;
+ if (keeping_dict != NULL)
+ *keeping_dict = TRUE;
}
else if (vartype == VAR_STRING)
{
@@ -4314,6 +4317,7 @@
ppconst_T *ppconst)
{
char_u *name_start = *end_leader;
+ int keeping_dict = FALSE;
for (;;)
{
@@ -4360,6 +4364,12 @@
return FAIL;
if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
return FAIL;
+ if (keeping_dict)
+ {
+ keeping_dict = FALSE;
+ if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
+ return FAIL;
+ }
}
else if (*p == '-' && p[1] == '>')
{
@@ -4470,6 +4480,12 @@
if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
return FAIL;
}
+ if (keeping_dict)
+ {
+ keeping_dict = FALSE;
+ if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
+ return FAIL;
+ }
}
else if (**arg == '[')
{
@@ -4537,7 +4553,13 @@
}
*arg = *arg + 1;
- if (compile_member(is_slice, cctx) == FAIL)
+ if (keeping_dict)
+ {
+ keeping_dict = FALSE;
+ if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
+ return FAIL;
+ }
+ if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
return FAIL;
}
else if (*p == '.' && p[1] != '.')
@@ -4562,18 +4584,21 @@
semsg(_(e_syntax_error_at_str), *arg);
return FAIL;
}
+ if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
+ return FAIL;
if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
return FAIL;
+ keeping_dict = TRUE;
*arg = p;
}
else
break;
}
- // TODO - see handle_subscript():
// Turn "dict.Func" into a partial for "Func" bound to "dict".
- // Don't do this when "Func" is already a partial that was bound
- // explicitly (pt_auto is FALSE).
+ // This needs to be done at runtime to be able to check the type.
+ if (keeping_dict && generate_instr(cctx, ISN_USEDICT) == NULL)
+ return FAIL;
return OK;
}
@@ -6661,7 +6686,7 @@
}
// Get the member.
- if (compile_member(FALSE, cctx) == FAIL)
+ if (compile_member(FALSE, NULL, cctx) == FAIL)
return FAIL;
}
return OK;
@@ -10406,6 +10431,7 @@
case ISN_CEXPR_AUCMD:
case ISN_CHECKLEN:
case ISN_CHECKNR:
+ case ISN_CLEARDICT:
case ISN_CMDMOD_REV:
case ISN_COMPAREANY:
case ISN_COMPAREBLOB:
@@ -10482,6 +10508,7 @@
case ISN_UNLETINDEX:
case ISN_UNLETRANGE:
case ISN_UNPACK:
+ case ISN_USEDICT:
// nothing allocated
break;
}