diff --git a/vlib/v/checker/match.v b/vlib/v/checker/match.v index 3fb651d6d9..56f51e0cb9 100644 --- a/vlib/v/checker/match.v +++ b/vlib/v/checker/match.v @@ -145,10 +145,18 @@ fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type { node.cond) c_str = '${expr} == ${c.table.type_to_str(branch_type)}' } else { - // $match a { $int {} - comptime_match_branch_result = c.check_compatible_types(node.cond_type, - '${node.cond}', expr) - c_str = '${c.table.type_to_str(node.cond_type)} == ${expr}' + is_function := c.table.final_sym(node.cond_type).kind == .function + if !is_function { + // $match a { $int {} + comptime_match_branch_result = c.check_compatible_types(node.cond_type, + '${node.cond}', expr) + c_str = '${c.table.type_to_str(node.cond_type)} == ${expr}' + } else { + // $match T { FnType {} } + branch_type := c.get_expr_type(expr) + comptime_match_branch_result = c.table.type_to_str(node.cond_type) == c.table.type_to_str(branch_type) + c_str = '${comptime_match_branch_result} == true' + } } if comptime_match_branch_result { break diff --git a/vlib/v/tests/comptime/comptime_match_fntype_test.v b/vlib/v/tests/comptime/comptime_match_fntype_test.v new file mode 100644 index 0000000000..4b15aa63b5 --- /dev/null +++ b/vlib/v/tests/comptime/comptime_match_fntype_test.v @@ -0,0 +1,31 @@ +type FN1 = fn (a int) int + +type FN2 = fn (a int, b int) int + +type FN3 = FN1 | FN2 + +fn invoke[T](cb T) int { + $match T { + FN1 { + return cb(1) + } + FN2 { + return cb(1, 2) + } + $else { + return 0 + } + } +} + +fn test_main() { + ret1 := invoke(fn (a int) int { + return a + }) + assert ret1 == 1 + + ret2 := invoke(fn (a int, b int) int { + return a + b + }) + assert ret2 == 3 +}