Problem: Vim9: type not properly checked in for loop.
Solution: Have items() return a list of lists. Add runtime type checks.
(closes #8515)
return &t_list_dict_any;
}
static type_T *
+ret_list_items(int argcount, type_T **argtypes UNUSED)
+{
+ return &t_list_list_any;
+}
+ static type_T *
ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
{
return &t_dict_any;
{"isnan", 1, 1, FEARG_1, arg1_float_or_nr,
ret_number_bool, MATH_FUNC(f_isnan)},
{"items", 1, 1, FEARG_1, arg1_dict,
- ret_list_any, f_items},
+ ret_list_items, f_items},
{"job_getchannel", 1, 1, FEARG_1, NULL,
ret_channel, JOB_FUNC(f_job_getchannel)},
{"job_info", 0, 1, FEARG_1, NULL,
{
if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
return &t_func_any;
+ // Need to check the type at runtime, the function may be defined later.
return &t_func_unknown;
}
EXTERN type_T t_list_string INIT6(VAR_LIST, 0, 0, TTFLAG_STATIC, &t_string, NULL);
EXTERN type_T t_list_job INIT6(VAR_LIST, 0, 0, TTFLAG_STATIC, &t_job, NULL);
EXTERN type_T t_list_dict_any INIT6(VAR_LIST, 0, 0, TTFLAG_STATIC, &t_dict_any, NULL);
+EXTERN type_T t_list_list_any INIT6(VAR_LIST, 0, 0, TTFLAG_STATIC, &t_list_any, NULL);
EXTERN type_T t_dict_bool INIT6(VAR_DICT, 0, 0, TTFLAG_STATIC, &t_bool, NULL);
EXTERN type_T t_dict_number INIT6(VAR_DICT, 0, 0, TTFLAG_STATIC, &t_number, NULL);
endfor
END
CheckDefAndScriptFailure(lines, 'E1059:', 1)
+
+ lines =<< trim END
+ var d: dict<number> = {a: 1, b: 2}
+ for [k: job, v: job] in d->items()
+ echo k v
+ endfor
+ END
+ CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected job but got string', 2)
enddef
def Test_for_loop_script_var()
static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 3117,
/**/
3116,
/**/
if (lhs_type == &t_any)
lhs_type = item_type;
else if (item_type != &t_unknown
- && !(var_list && item_type == &t_any)
- && check_type(lhs_type, item_type, TRUE, where) == FAIL)
+ && ((var_list && item_type == &t_any)
+ ? need_type(item_type, lhs_type,
+ -1, 0, cctx, FALSE, FALSE)
+ : check_type(lhs_type, item_type, TRUE, where))
+ == FAIL)
goto failed;
var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
if (var_lvar == NULL)