From 806c39d46e87636eeb80b64ffb1f564b323d1da4 Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 31 Aug 2022 23:44:12 +0800 Subject: [PATCH] parser: fix error for match sumtype with fntype (#15620) --- vlib/v/parser/if_match.v | 3 ++- vlib/v/tests/inout/sumtype_with_fntype.out | 2 ++ vlib/v/tests/inout/sumtype_with_fntype.vv | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/vlib/v/parser/if_match.v b/vlib/v/parser/if_match.v index e07dcc5e91..5b46fb0cf9 100644 --- a/vlib/v/parser/if_match.v +++ b/vlib/v/parser/if_match.v @@ -237,7 +237,8 @@ fn (mut p Parser) match_expr() ast.MatchExpr { } else if (p.tok.kind == .name && !(p.tok.lit == 'C' && p.peek_tok.kind == .dot) && (((ast.builtin_type_names_matcher.matches(p.tok.lit) || p.tok.lit[0].is_capital()) && p.peek_tok.kind != .lpar) || (p.peek_tok.kind == .dot && p.peek_token(2).lit.len > 0 - && p.peek_token(2).lit[0].is_capital()))) || p.is_only_array_type() { + && p.peek_token(2).lit[0].is_capital()))) || p.is_only_array_type() + || p.tok.lit == 'fn' { mut types := []ast.Type{} for { // Sum type match diff --git a/vlib/v/tests/inout/sumtype_with_fntype.out b/vlib/v/tests/inout/sumtype_with_fntype.out index 719dfb043d..71e15e9480 100644 --- a/vlib/v/tests/inout/sumtype_with_fntype.out +++ b/vlib/v/tests/inout/sumtype_with_fntype.out @@ -2,5 +2,7 @@ Fn(fn () int) Fn(fn (int) int) 123 123 +123 +321 321 321 diff --git a/vlib/v/tests/inout/sumtype_with_fntype.vv b/vlib/v/tests/inout/sumtype_with_fntype.vv index 116fc29c74..a48bc68171 100644 --- a/vlib/v/tests/inout/sumtype_with_fntype.vv +++ b/vlib/v/tests/inout/sumtype_with_fntype.vv @@ -23,10 +23,24 @@ fn main() { println(x1()) } + match x1 { + fn () int { + println(x1()) + } + else {} + } + x2 := abc(2) y2 := x2 as fn (int) int println(y2(321)) if x2 is fn (int) int { println(x2(321)) } + + match x2 { + fn (int) int { + println(x2(321)) + } + else {} + } }