Skip to content

Commit 3ab660b

Browse files
authored
comptime: fix $if var.return_type == 1 { (fix #24391) (#24393)
1 parent 875c165 commit 3ab660b

3 files changed

Lines changed: 42 additions & 5 deletions

File tree

‎vlib/v/checker/if.v‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,10 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
182182
} else {
183183
.skip
184184
}
185-
} else if comptime_field_name == c.comptime.comptime_for_method_var {
186-
if left.field_name == 'return_type' {
187-
skip_state = c.check_compatible_types(c.unwrap_generic(c.comptime.comptime_for_method_ret_type),
188-
right as ast.TypeNode)
189-
}
185+
} else if comptime_field_name == c.comptime.comptime_for_method_var
186+
&& left.field_name == 'return_type' {
187+
skip_state = c.check_compatible_types(c.unwrap_generic(c.comptime.comptime_for_method_ret_type),
188+
right as ast.TypeNode)
190189
} else if comptime_field_name in [
191190
c.comptime.comptime_for_variant_var,
192191
c.comptime.comptime_for_enum_var,
@@ -286,6 +285,14 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
286285
}
287286
}
288287
}
288+
} else if comptime_field_name == c.comptime.comptime_for_method_var {
289+
if left.field_name == 'return_type' {
290+
skip_state = if c.unwrap_generic(c.comptime.comptime_for_method_ret_type).idx() == right.val.i64() {
291+
ComptimeBranchSkipState.eval
292+
} else {
293+
ComptimeBranchSkipState.skip
294+
}
295+
}
289296
} else if left.expr is ast.TypeOf {
290297
skip_state = if left.expr.typ.nr_muls() == right.val.i64() {
291298
ComptimeBranchSkipState.eval

‎vlib/v/gen/c/comptime.v‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,21 @@ fn (mut g Gen) comptime_if_cond(cond ast.Expr, pkg_exist bool) (bool, bool) {
655655
g.write('0')
656656
}
657657
return is_true, true
658+
} else if g.comptime.comptime_for_method_var != ''
659+
&& cond.left.expr is ast.Ident
660+
&& cond.left.expr.name == g.comptime.comptime_for_method_var
661+
&& cond.left.field_name == 'return_type' {
662+
is_true := match cond.op {
663+
.eq { g.comptime.comptime_for_method_ret_type.idx() == cond.right.val.i64() }
664+
.ne { g.comptime.comptime_for_method_ret_type.idx() != cond.right.val.i64() }
665+
else { false }
666+
}
667+
if is_true {
668+
g.write('1')
669+
} else {
670+
g.write('0')
671+
}
672+
return is_true, true
658673
}
659674
}
660675
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
struct Struct {}
2+
3+
fn (s Struct) func() {}
4+
5+
fn test_main() {
6+
$for method in Struct.methods {
7+
println('${method.name}: ${method.return_type}')
8+
$if method.return_type == 1 {
9+
assert true
10+
}
11+
$if method.return_type == 11 {
12+
assert false
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)