v9.CheckScriptFailure(lines, 'E1411: Missing dot after object "o"')
enddef
+" Test for using a compound operator with a dict
+def Test_dict_compoundop_check()
+ for op in ['+=', '-=', '*=', '/=', '%=']
+ v9.CheckSourceDefAndScriptFailure(['var d: dict<number> = {a: 1}', $'d {op} 1'], $'E734: Wrong variable type for {op}')
+ endfor
+ var lines =<< trim END
+ var d: dict<number> = {a: 1}
+ d['a'] += 2
+ assert_equal({a: 3}, d)
+ END
+ v9.CheckSourceDefAndScriptSuccess(lines)
+enddef
+
" Test for checking the argument type of a def function
def Test_func_argtype_check()
var lines =<< trim END
}
/*
- * Compile a compound op assignment statement (+=, -=, *=, %=, etc.)
+ * Returns OK if "type" supports compound operator "op_arg" (e.g. +=, -=, %=,
+ * etc.). Compound operators are not supported with a tuple and a dict.
+ * Returns FAIL if compound operator is not supported.
*/
static int
-compile_assign_compound_op(cctx_T *cctx, cac_T *cac)
+check_type_supports_compound_op(type_T *type, char_u op_arg)
{
- lhs_T *lhs = &cac->cac_lhs;
- type_T *expected;
- type_T *stacktype = NULL;
-
- if (cac->cac_lhs.lhs_type->tt_type == VAR_TUPLE)
+ if (type->tt_type == VAR_TUPLE || type->tt_type == VAR_DICT)
{
- // compound operators are not supported with a tuple
char_u op[2];
- op[0] = *cac->cac_op;
+ op[0] = op_arg;
op[1] = NUL;
semsg(_(e_wrong_variable_type_for_str_equal), op);
return FAIL;
}
+ return OK;
+}
+
+/*
+ * Compile a compound op assignment statement (+=, -=, *=, %=, etc.)
+ */
+ static int
+compile_assign_compound_op(cctx_T *cctx, cac_T *cac)
+{
+ lhs_T *lhs = &cac->cac_lhs;
+ type_T *expected;
+ type_T *stacktype = NULL;
+
+ if (cac->cac_lhs.lhs_type->tt_type == VAR_TUPLE
+ && check_type_supports_compound_op(cac->cac_lhs.lhs_type,
+ *cac->cac_op) == FAIL)
+ return FAIL;
+
if (*cac->cac_op == '.')
{
expected = lhs->lhs_member_type;
{
expected = lhs->lhs_member_type;
stacktype = get_type_on_stack(cctx, 0);
+
+ if (check_type_supports_compound_op(expected, *cac->cac_op) == FAIL)
+ return FAIL;
+
if (
// If variable is float operation with number is OK.
!(expected == &t_float && (stacktype == &t_number