From 96bd4e8794e4fd45aebba690518e63a8ea8eff5f Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 5 Jul 2020 22:35:45 +0800 Subject: [PATCH] parser: fix type detection in `match` (#5679) --- vlib/v/parser/if.v | 7 +++---- vlib/v/tests/match_test.v | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/vlib/v/parser/if.v b/vlib/v/parser/if.v index 88d09767b3..54a01e7781 100644 --- a/vlib/v/parser/if.v +++ b/vlib/v/parser/if.v @@ -115,10 +115,9 @@ fn (mut p Parser) match_expr() ast.MatchExpr { if p.tok.kind == .key_else { is_else = true p.next() - } else if p.tok.kind == .name && !(p.tok.lit == 'C' && - p.peek_tok.kind == .dot) && (p.tok.lit in table.builtin_type_names || - (p.tok.lit[0].is_capital() && !p.tok.lit.is_upper()) || - (p.peek_tok.kind == .dot && p.peek_tok2.lit[0].is_capital())) { + } else if p.tok.kind == .name && !(p.tok.lit == 'C' && p.peek_tok.kind == .dot) && + (p.tok.lit in table.builtin_type_names || p.tok.lit[0].is_capital() || + (p.peek_tok.kind == .dot && p.peek_tok2.lit[0].is_capital())) { if var_name.len == 0 { match cond { ast.Ident { diff --git a/vlib/v/tests/match_test.v b/vlib/v/tests/match_test.v index d8eabbae35..e3a2ed39d4 100644 --- a/vlib/v/tests/match_test.v +++ b/vlib/v/tests/match_test.v @@ -82,3 +82,29 @@ fn test_match_enums() { } assert b == .blue } + +type Sum = A1 | B1 + +struct A1 { + pos int +} +struct B1 { + val string +} + +fn f(s Sum) string { + match s { + A1 { + return typeof(s) + } + B1 { + return '' + } + } + return '' +} + +fn test_sum_type_name() { + a := A1{pos: 22} + assert f(a) == 'A1' +}