parser: fix type detection in `match` (#5679)

pull/5681/head
yuyi 2020-07-05 22:35:45 +08:00 committed by GitHub
parent 9d7f1a236a
commit 96bd4e8794
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 4 deletions

View File

@ -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 {

View File

@ -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'
}