From 3ab660b4c67725135218c6cc25b1a9db21557d3b Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 3 May 2025 07:01:14 -0300 Subject: [PATCH] comptime: fix `$if var.return_type == 1 {` (fix #24391) (#24393) --- vlib/v/checker/if.v | 17 ++++++++++++----- vlib/v/gen/c/comptime.v | 15 +++++++++++++++ vlib/v/tests/comptime/comptime_call_void_test.v | 15 +++++++++++++++ 3 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 vlib/v/tests/comptime/comptime_call_void_test.v diff --git a/vlib/v/checker/if.v b/vlib/v/checker/if.v index e33801af5..faf305c6f 100644 --- a/vlib/v/checker/if.v +++ b/vlib/v/checker/if.v @@ -182,11 +182,10 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { } else { .skip } - } else if comptime_field_name == c.comptime.comptime_for_method_var { - if left.field_name == 'return_type' { - skip_state = c.check_compatible_types(c.unwrap_generic(c.comptime.comptime_for_method_ret_type), - right as ast.TypeNode) - } + } else if comptime_field_name == c.comptime.comptime_for_method_var + && left.field_name == 'return_type' { + skip_state = c.check_compatible_types(c.unwrap_generic(c.comptime.comptime_for_method_ret_type), + right as ast.TypeNode) } else if comptime_field_name in [ c.comptime.comptime_for_variant_var, c.comptime.comptime_for_enum_var, @@ -286,6 +285,14 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { } } } + } else if comptime_field_name == c.comptime.comptime_for_method_var { + if left.field_name == 'return_type' { + skip_state = if c.unwrap_generic(c.comptime.comptime_for_method_ret_type).idx() == right.val.i64() { + ComptimeBranchSkipState.eval + } else { + ComptimeBranchSkipState.skip + } + } } else if left.expr is ast.TypeOf { skip_state = if left.expr.typ.nr_muls() == right.val.i64() { ComptimeBranchSkipState.eval diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index bd0acf3d0..cdee0a155 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -655,6 +655,21 @@ fn (mut g Gen) comptime_if_cond(cond ast.Expr, pkg_exist bool) (bool, bool) { g.write('0') } return is_true, true + } else if g.comptime.comptime_for_method_var != '' + && cond.left.expr is ast.Ident + && cond.left.expr.name == g.comptime.comptime_for_method_var + && cond.left.field_name == 'return_type' { + is_true := match cond.op { + .eq { g.comptime.comptime_for_method_ret_type.idx() == cond.right.val.i64() } + .ne { g.comptime.comptime_for_method_ret_type.idx() != cond.right.val.i64() } + else { false } + } + if is_true { + g.write('1') + } else { + g.write('0') + } + return is_true, true } } } diff --git a/vlib/v/tests/comptime/comptime_call_void_test.v b/vlib/v/tests/comptime/comptime_call_void_test.v new file mode 100644 index 000000000..8ef58869b --- /dev/null +++ b/vlib/v/tests/comptime/comptime_call_void_test.v @@ -0,0 +1,15 @@ +struct Struct {} + +fn (s Struct) func() {} + +fn test_main() { + $for method in Struct.methods { + println('${method.name}: ${method.return_type}') + $if method.return_type == 1 { + assert true + } + $if method.return_type == 11 { + assert false + } + } +} -- 2.39.5