From 9aa1a654890e4f54f6ef8fbbf03e25de423a16a3 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Mon, 4 May 2020 21:03:18 +0200 Subject: [PATCH] checker: check match condition --- vlib/json/json_test.v | 2 +- vlib/v/checker/checker.v | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/vlib/json/json_test.v b/vlib/json/json_test.v index 21c7f04b25..0ad8c0fb1a 100644 --- a/vlib/json/json_test.v +++ b/vlib/json/json_test.v @@ -8,7 +8,7 @@ struct Employee { fn test_simple() { x := Employee{'Peter', 28} s := json.encode(x) - assert s == '{"name":"Peter","age":28}' + assert s == '{"name":"Peter","age":28}' y := json.decode(Employee, s) or { assert false } diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index bba7501dfb..65a8158b44 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -658,16 +658,13 @@ pub fn (mut c Checker) call_method(call_expr mut ast.CallExpr) table.Type { // call_expr.args << method.args[0].typ // call_expr.exp_arg_types << method.args[0].typ for i, arg in call_expr.args { - exp_arg_typ := if method.is_variadic && i >= method.args.len - 1 { - method.args[method.args.len - 1].typ - } else { - method.args[i + 1].typ - } + exp_arg_typ := if method.is_variadic && i >= method.args.len - 1 { method.args[method.args.len - + 1].typ } else { method.args[i + 1].typ } c.expected_type = exp_arg_typ got_arg_typ := c.expr(arg.expr) call_expr.args[i].typ = got_arg_typ - if method.is_variadic && got_arg_typ.flag_is(.variadic) && call_expr.args.len - 1 > - i { + if method.is_variadic && got_arg_typ.flag_is(.variadic) && call_expr.args.len - + 1 > i { c.error('when forwarding a varg variable, it must be the final argument', call_expr.pos) } if !c.table.check(got_arg_typ, exp_arg_typ) { @@ -1752,6 +1749,10 @@ pub fn (mut c Checker) match_expr(node mut ast.MatchExpr) table.Type { c.expected_type = cond_type typ := c.expr(expr) typ_sym := c.table.get_type_symbol(typ) + if !node.is_sum_type && !c.table.check(typ, cond_type) { + exp_sym := c.table.get_type_symbol(cond_type) + c.error('cannot use `$typ_sym.name` as `$exp_sym.name` in `match`', node.pos) + } // TODO: if typ_sym.kind == .sum_type { } @@ -1787,7 +1788,7 @@ pub fn (mut c Checker) match_expr(node mut ast.MatchExpr) table.Type { fn (mut c Checker) match_exprs(node mut ast.MatchExpr, type_sym table.TypeSymbol) { // branch_exprs is a histogram of how many times // an expr was used in the match - mut branch_exprs := map[string]int + mut branch_exprs := map[string]int{} for branch in node.branches { for expr in branch.exprs { mut key := ''