From 1dd172e0809aad749b6e3e701220ce12ffcfa55c Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 27 Sep 2025 13:58:54 -0300 Subject: [PATCH] cgen: fix option match case (fix #25360) (#25400) --- vlib/v/gen/c/infix.v | 3 +++ vlib/v/tests/options/option_match_eq_test.v | 23 +++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 vlib/v/tests/options/option_match_eq_test.v diff --git a/vlib/v/gen/c/infix.v b/vlib/v/gen/c/infix.v index e85beca4b..f58ce7931 100644 --- a/vlib/v/gen/c/infix.v +++ b/vlib/v/gen/c/infix.v @@ -344,6 +344,8 @@ fn (mut g Gen) infix_expr_eq_op(node ast.InfixExpr) { if node.op == .ne { g.write('!') } + tmp_left_is_opt := g.left_is_opt + g.left_is_opt = true g.write('${ptr_typ}_sumtype_eq(') if left.typ.is_ptr() { g.write('*'.repeat(left.typ.nr_muls())) @@ -355,6 +357,7 @@ fn (mut g Gen) infix_expr_eq_op(node ast.InfixExpr) { } g.expr(node.right) g.write(')') + g.left_is_opt = tmp_left_is_opt } .interface { ptr_typ := g.equality_fn(left.unaliased) diff --git a/vlib/v/tests/options/option_match_eq_test.v b/vlib/v/tests/options/option_match_eq_test.v new file mode 100644 index 000000000..d8a9f3b44 --- /dev/null +++ b/vlib/v/tests/options/option_match_eq_test.v @@ -0,0 +1,23 @@ +module main + +enum Test { + a + b +} + +type Sumtype = Test | int + +fn test_main() { + t := ?Sumtype(Test.a) + match true { + t == ?Sumtype(5) { + assert false + } + t == ?Sumtype(Test.a) { + assert true + } + else { + assert false + } + } +} -- 2.39.5