From af289da844757c48ec231dbae7a8937298da74af Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 18 Mar 2020 12:23:32 +0100 Subject: [PATCH] ast: MatchExpr: expr_type => cond_type --- vlib/v/ast/ast.v | 2 +- vlib/v/checker/checker.v | 12 ++++++------ vlib/v/gen/cgen.v | 7 ++++--- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 94aeac1663..8d66a0779d 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -354,7 +354,7 @@ pub: mut: is_expr bool // returns a value return_type table.Type - expr_type table.Type // type of `x` in `match x {` + cond_type table.Type // type of `x` in `match x {` is_sum_type bool } diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 0920cfef70..196f503178 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -790,15 +790,15 @@ pub fn (c mut Checker) ident(ident mut ast.Ident) table.Type { pub fn (c mut Checker) match_expr(node mut ast.MatchExpr) table.Type { node.is_expr = c.expected_type != table.void_type - expr_type := c.expr(node.cond) - if expr_type == 0 { - c.error('match 0 expr type', node.pos) + cond_type := c.expr(node.cond) + if cond_type == 0 { + c.error('match 0 cond type', node.pos) } - c.expected_type = expr_type + c.expected_type = cond_type mut ret_type := table.void_type for branch in node.branches { for expr in branch.exprs { - c.expected_type = expr_type + c.expected_type = cond_type typ := c.expr(expr) typ_sym := c.table.get_type_symbol(typ) // TODO: @@ -826,7 +826,7 @@ pub fn (c mut Checker) match_expr(node mut ast.MatchExpr) table.Type { // node.expected_type = c.expected_type // } node.return_type = ret_type - node.expr_type = expr_type + node.cond_type = cond_type // println('!m $expr_type') return ret_type } diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 8c89eb0f49..667bc93b5a 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -970,7 +970,7 @@ fn (g mut Gen) infix_expr(node ast.InfixExpr) { fn (g mut Gen) match_expr(node ast.MatchExpr) { // println('match expr typ=$it.expr_type') // TODO - if node.expr_type == 0 { + if node.cond_type == 0 { g.writeln('// match 0') return } @@ -979,7 +979,7 @@ fn (g mut Gen) match_expr(node ast.MatchExpr) { g.inside_ternary = true // g.write('/* EM ret type=${g.typ(node.return_type)} */') } - type_sym := g.table.get_type_symbol(node.expr_type) + type_sym := g.table.get_type_symbol(node.cond_type) mut tmp := '' if type_sym.kind != .void { tmp = g.new_tmp_var() @@ -1278,7 +1278,8 @@ fn (g mut Gen) return_statement(it ast.Return) { if table.type_is_optional(g.fn_decl.return_type) { mut is_none := false mut is_error := false - match it.exprs[0] { + expr0 := it.exprs[0] + match expr0 { ast.None { is_none = true }