checker: comptime match only eval true branch

This commit is contained in:
kbkpbot 2025-09-03 08:06:27 +08:00
parent f6b60e4d9f
commit b442fc6349
2 changed files with 178 additions and 158 deletions

View file

@ -221,6 +221,8 @@ fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type {
} }
} }
} }
if !node.is_comptime || (node.is_comptime && comptime_match_branch_result) {
if node.is_expr { if node.is_expr {
c.stmts_ending_with_expression(mut branch.stmts, c.expected_or_type) c.stmts_ending_with_expression(mut branch.stmts, c.expected_or_type)
} else { } else {
@ -409,6 +411,7 @@ fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type {
} }
} }
} }
}
first_iteration = false first_iteration = false
if has_return := c.has_return(branch.stmts) { if has_return := c.has_return(branch.stmts) {
if has_return { if has_return {

View file

@ -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]()
}