From 58c5f189949305f77e8a5f7c36b0640bdfc1d17d Mon Sep 17 00:00:00 2001 From: CreeperFace <165158232+dy-tea@users.noreply.github.com> Date: Mon, 20 Oct 2025 22:14:05 +0100 Subject: [PATCH] cgen: ensure option none/ok state is also compared (#25532) --- vlib/v/gen/c/infix.v | 20 +++++++++++++++---- .../options/option_enum_compare_none_test.v | 17 ++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 vlib/v/tests/options/option_enum_compare_none_test.v diff --git a/vlib/v/gen/c/infix.v b/vlib/v/gen/c/infix.v index 7af143523..0361a94f6 100644 --- a/vlib/v/gen/c/infix.v +++ b/vlib/v/gen/c/infix.v @@ -403,13 +403,25 @@ fn (mut g Gen) infix_expr_eq_op(node ast.InfixExpr) { old_inside_opt_or_res := g.inside_opt_or_res g.inside_opt_or_res = true if node.op == .eq { - g.write('!') + g.write('(') + } else { + g.write('!(') } - g.write('memcmp(') + g.write('(') + g.expr(node.left) + g.write('.state == 2 && ') + g.expr(node.right) + g.write('.state == 2) || (') + g.expr(node.left) + g.write('.state == ') + g.expr(node.right) + g.write('.state && ') + g.expr(node.left) + g.write('.state != 2 && !memcmp(&') g.expr(node.left) - g.write('.data, ') + g.write('.data, &') g.expr(node.right) - g.write('.data, sizeof(${g.base_type(left_type)}))') + g.write('.data, sizeof(${g.base_type(left_type)}))))') g.inside_opt_or_res = old_inside_opt_or_res } else { g.gen_plain_infix_expr(node) diff --git a/vlib/v/tests/options/option_enum_compare_none_test.v b/vlib/v/tests/options/option_enum_compare_none_test.v new file mode 100644 index 000000000..4be305aba --- /dev/null +++ b/vlib/v/tests/options/option_enum_compare_none_test.v @@ -0,0 +1,17 @@ +enum A_ { + b +} + +fn test_main() { + mut a := ?A_(none) + a = .b + b := ?A_(none) + match true { + a == ?A_(.b) && a != b { + assert true + } + else { + assert false + } + } +} -- 2.39.5