diff --git a/vlib/v/checker/match.v b/vlib/v/checker/match.v index 6d3530d00d..3fb651d6d9 100644 --- a/vlib/v/checker/match.v +++ b/vlib/v/checker/match.v @@ -221,10 +221,13 @@ fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type { } } } - if node.is_expr { - c.stmts_ending_with_expression(mut branch.stmts, c.expected_or_type) - } else { - c.stmts(mut branch.stmts) + + if !node.is_comptime || (node.is_comptime && comptime_match_branch_result) { + if node.is_expr { + c.stmts_ending_with_expression(mut branch.stmts, c.expected_or_type) + } else { + c.stmts(mut branch.stmts) + } } c.smartcast_mut_pos = token.Pos{} c.smartcast_cond_pos = token.Pos{} diff --git a/vlib/v/tests/comptime/comptime_match_eval_only_true_branch_test.v b/vlib/v/tests/comptime/comptime_match_eval_only_true_branch_test.v new file mode 100644 index 0000000000..74ca289ed2 --- /dev/null +++ b/vlib/v/tests/comptime/comptime_match_eval_only_true_branch_test.v @@ -0,0 +1,17 @@ +module main + +fn func[T]() bool { + $match T { + u8, u16 { + return true + } + $else { + // return false + $compile_error('fail') + } + } +} + +fn test_comptime_match_eval_only_true_branch() { + assert func[u8]() +}