From f5a55747c0914b6082315fb81985fb3dfbd9b68e Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 20 Oct 2025 08:20:47 -0300 Subject: [PATCH] cgen: fix match codegen for option match case (fix #25533) (#25537) --- vlib/v/gen/c/match.v | 3 +-- .../options/option_match_case_enum_test.v | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/options/option_match_case_enum_test.v diff --git a/vlib/v/gen/c/match.v b/vlib/v/gen/c/match.v index 7abd1de17..be23181ce 100644 --- a/vlib/v/gen/c/match.v +++ b/vlib/v/gen/c/match.v @@ -431,11 +431,10 @@ fn (mut g Gen) match_expr_classic(node ast.MatchExpr, is_expr bool, cond_var str node_cond_type_unsigned := node.cond_type in [ast.u16_type, ast.u32_type, ast.u64_type] type_sym := g.table.final_sym(node.cond_type) use_ternary := is_expr && tmp_var == '' - mut reset_if := false + mut reset_if := node.branches.any(it.exprs.any(g.match_must_reset_if(it))) mut has_goto := false for j, branch in node.branches { is_last := j == node.branches.len - 1 - reset_if = branch.exprs.any(g.match_must_reset_if(it)) if reset_if { g.writeln('') g.set_current_pos_as_last_stmt_pos() diff --git a/vlib/v/tests/options/option_match_case_enum_test.v b/vlib/v/tests/options/option_match_case_enum_test.v new file mode 100644 index 000000000..24e47a971 --- /dev/null +++ b/vlib/v/tests/options/option_match_case_enum_test.v @@ -0,0 +1,25 @@ +enum A_ { + b + c + d + e +} + +fn test_main() { + mut a := ?A_(none) + a = .c + match true { + a == ?A_(.b) { + assert false + } + a == ?A_(.c) { + assert true + } + a == ?A_(.d) { + assert false + } + else { + assert false + } + } +} -- 2.39.5