From d9158e788546e3d5dcf6895f4eb16a14e367cab5 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Thu, 13 Nov 2025 18:51:44 -0300 Subject: [PATCH] transformer: fix struct init comparison turning into boolean (#25724) --- vlib/v/tests/structs/struct_eq_op_test.v | 12 ++++++++++++ vlib/v/transformer/transformer.v | 3 ++- 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/structs/struct_eq_op_test.v diff --git a/vlib/v/tests/structs/struct_eq_op_test.v b/vlib/v/tests/structs/struct_eq_op_test.v new file mode 100644 index 000000000..6993a33af --- /dev/null +++ b/vlib/v/tests/structs/struct_eq_op_test.v @@ -0,0 +1,12 @@ +struct MyInt { + x int +} + +fn (i1 MyInt) == (i2 MyInt) bool { + return i1.x == i2.x +} + +fn test_main() { + c := MyInt{30} == MyInt{2} + assert !c +} diff --git a/vlib/v/transformer/transformer.v b/vlib/v/transformer/transformer.v index c2c0a32fd..f85f71555 100644 --- a/vlib/v/transformer/transformer.v +++ b/vlib/v/transformer/transformer.v @@ -1137,7 +1137,8 @@ pub fn (mut t Transformer) infix_expr(mut node ast.InfixExpr) ast.Expr { // for `a == a`, `a != a`, `struct.f != struct.f` // Note: can't compare `f32` or `f64` here, as `NaN != NaN` will return true in IEEE 754 if node.left.type_name() == node.right.type_name() - && node.left_type !in [ast.f32_type, ast.f64_type] && node.op in [.eq, .ne] { + && node.left_type !in [ast.f32_type, ast.f64_type] && node.op in [.eq, .ne] + && node.left !is ast.StructInit && node.right !is ast.StructInit { left_name := '${node.left}' right_name := '${node.right}' if left_name == right_name { -- 2.39.5