Skip to content

Commit e15d8fc

Browse files
authored
checker: comptime match only eval true branch (fix #25223) (#25225)
1 parent f17e0fd commit e15d8fc

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

vlib/v/checker/match.v

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,13 @@ fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type {
221221
}
222222
}
223223
}
224-
if node.is_expr {
225-
c.stmts_ending_with_expression(mut branch.stmts, c.expected_or_type)
226-
} else {
227-
c.stmts(mut branch.stmts)
224+
225+
if !node.is_comptime || (node.is_comptime && comptime_match_branch_result) {
226+
if node.is_expr {
227+
c.stmts_ending_with_expression(mut branch.stmts, c.expected_or_type)
228+
} else {
229+
c.stmts(mut branch.stmts)
230+
}
228231
}
229232
c.smartcast_mut_pos = token.Pos{}
230233
c.smartcast_cond_pos = token.Pos{}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module main
2+
3+
fn func[T]() bool {
4+
$match T {
5+
u8, u16 {
6+
return true
7+
}
8+
$else {
9+
// return false
10+
$compile_error('fail')
11+
}
12+
}
13+
}
14+
15+
fn test_comptime_match_eval_only_true_branch() {
16+
assert func[u8]()
17+
}

0 commit comments

Comments
 (0)