Skip to content

Commit 71e8fc8

Browse files
authored
checker: fix comptime if branch checking (#16938)
1 parent 80cd9f8 commit 71e8fc8

5 files changed

+44
-2
lines changed

vlib/v/checker/if.v

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,9 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
131131
} else if skip_state == .skip {
132132
c.skip_flags = true
133133
skip_state = .unknown // Reset the value of `skip_state` for the next branch
134-
} else if !is_comptime_type_is_expr && skip_state == .eval {
134+
} else if skip_state == .eval {
135135
found_branch = true // If a branch wasn't skipped, the rest must be
136+
c.skip_flags = skip_state == .skip
136137
}
137138
if c.fn_level == 0 && c.pref.output_cross_c {
138139
// do not skip any of the branches for top level `$if OS {`

vlib/v/checker/return.v

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,9 @@ fn has_top_return(stmts []ast.Stmt) bool {
270270
}
271271
ast.ExprStmt {
272272
if stmt.expr is ast.CallExpr {
273-
if stmt.expr.is_noreturn {
273+
// do not ignore panic() calls on non checked stmts
274+
if stmt.expr.is_noreturn
275+
|| (stmt.expr.is_method == false && stmt.expr.name == 'panic') {
274276
return true
275277
}
276278
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vlib/v/checker/tests/comptime_branching_working_with_a_custom_compile_error.vv:8:3: error: 11
2+
6 | return Value(val)
3+
7 | } $else {
4+
8 | $compile_error('11')
5+
| ~~~~~~~~~~~~~~~~~~~~
6+
9 | println('not bool ${val}')
7+
10 | }
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
type Value = bool | voidptr
2+
3+
pub fn create[T](val T) Value {
4+
$if T is bool {
5+
println('bool ${val}')
6+
return Value(val)
7+
} $else {
8+
$compile_error('11')
9+
println('not bool ${val}')
10+
}
11+
return Value(voidptr(123))
12+
}
13+
14+
fn main() {
15+
println(create(123))
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
type Value = bool | voidptr
2+
3+
pub fn create[T](val T) Value {
4+
$if T is bool {
5+
println('bool ${val}')
6+
return Value(val)
7+
} $else {
8+
$compile_error('11')
9+
println('not bool ${val}')
10+
}
11+
return Value(voidptr(123))
12+
}
13+
14+
fn test_calling_generic_function_that_has_inside_a_comptime_compile_error_directive() {
15+
assert create(true) == Value(true)
16+
}

0 commit comments

Comments
 (0)