From 32ad8de8a4347b85954f526f4890f0450fd9796e Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 25 Oct 2025 17:24:49 -0300 Subject: [PATCH] checker: fix comptime else branch handling (fix #25586) (#25589) --- vlib/v/checker/if.v | 46 ++++++++++++++----- vlib/v/gen/c/testdata/comptime_if.c.must_have | 9 ++++ vlib/v/gen/c/testdata/comptime_if.vv | 10 ++++ 3 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 vlib/v/gen/c/testdata/comptime_if.c.must_have create mode 100644 vlib/v/gen/c/testdata/comptime_if.vv diff --git a/vlib/v/checker/if.v b/vlib/v/checker/if.v index b61e0b1d9..d387c616c 100644 --- a/vlib/v/checker/if.v +++ b/vlib/v/checker/if.v @@ -308,7 +308,9 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { node.is_expr = true node.typ = c.unwrap_generic(c.expected_type) } - continue + unsafe { + goto end_if + } } if c.expected_expr_type != ast.void_type { c.expected_type = c.expected_expr_type @@ -327,7 +329,9 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { // (e.g. argument expression, variable declaration / assignment) c.error('the final expression in `if` or `match`, must have a value of a non-void type', stmt.pos) - continue + unsafe { + goto end_if + } } if !c.check_types(stmt.typ, node.typ) { if node.typ == ast.void_type { @@ -339,37 +343,51 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { node.typ = stmt.typ } c.expected_expr_type = node.typ - continue + unsafe { + goto end_if + } } else if node.typ in [ast.float_literal_type, ast.int_literal_type] { if node.typ == ast.int_literal_type { if stmt.typ.is_int() || stmt.typ.is_float() { node.typ = stmt.typ - continue + unsafe { + goto end_if + } } } else { // node.typ == float_literal if stmt.typ.is_float() { node.typ = stmt.typ - continue + unsafe { + goto end_if + } } } } if stmt.typ in [ast.float_literal_type, ast.int_literal_type] { if stmt.typ == ast.int_literal_type { if node.typ.is_int() || node.typ.is_float() { - continue + unsafe { + goto end_if + } } } else { // expr_type == float_literal if node.typ.is_float() { - continue + unsafe { + goto end_if + } } } } if node.is_expr && c.table.sym(former_expected_type).kind == .sum_type { node.typ = former_expected_type - continue + unsafe { + goto end_if + } } if is_noreturn_callexpr(stmt.expr) { - continue + unsafe { + goto end_if + } } if (node.typ.has_option_or_result()) && c.table.sym(stmt.typ).kind == .struct @@ -382,7 +400,9 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { pos: node.pos } stmt.typ = ast.error_type - continue + unsafe { + goto end_if + } } if (node.typ == ast.none_type && stmt.typ != ast.none_type) || (stmt.typ == ast.none_type && node.typ != ast.none_type) { @@ -391,7 +411,9 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { } else { node.typ.set_flag(.option) } - continue + unsafe { + goto end_if + } } c.error('mismatched types `${c.table.type_to_str(node.typ)}` and `${c.table.type_to_str(stmt.typ)}`', node.pos) @@ -440,7 +462,7 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { nbranches_without_return++ } } - + end_if: if comptime_remove_curr_branch_stmts && !c.pref.output_cross_c { // remove the branch statements since they may contain OS-specific code. branch.stmts = [] diff --git a/vlib/v/gen/c/testdata/comptime_if.c.must_have b/vlib/v/gen/c/testdata/comptime_if.c.must_have new file mode 100644 index 000000000..761452fe9 --- /dev/null +++ b/vlib/v/gen/c/testdata/comptime_if.c.must_have @@ -0,0 +1,9 @@ +string _t1; +#if defined(CUSTOM_DEFINE_some_custom_define) +#elif defined(CUSTOM_DEFINE_some_other_define) + _t1 = _S("xyz"); + ; +#else +#endif + string platform = _t1; +builtin__println(platform); \ No newline at end of file diff --git a/vlib/v/gen/c/testdata/comptime_if.vv b/vlib/v/gen/c/testdata/comptime_if.vv new file mode 100644 index 000000000..5e69041b7 --- /dev/null +++ b/vlib/v/gen/c/testdata/comptime_if.vv @@ -0,0 +1,10 @@ +// vtest vflags: -d some_other_define +platform := $if some_custom_define ? { + 'abc' +} $else $if some_other_define ? { + 'xyz' +} $else { + eprintln('oh no') + exit(1) +} +println(platform) -- 2.39.5