Skip to content

Commit d9158e7

Browse files
authored
transformer: fix struct init comparison turning into boolean (#25724)
1 parent c510779 commit d9158e7

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
struct MyInt {
2+
x int
3+
}
4+
5+
fn (i1 MyInt) == (i2 MyInt) bool {
6+
return i1.x == i2.x
7+
}
8+
9+
fn test_main() {
10+
c := MyInt{30} == MyInt{2}
11+
assert !c
12+
}

vlib/v/transformer/transformer.v

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,8 @@ pub fn (mut t Transformer) infix_expr(mut node ast.InfixExpr) ast.Expr {
11371137
// for `a == a`, `a != a`, `struct.f != struct.f`
11381138
// Note: can't compare `f32` or `f64` here, as `NaN != NaN` will return true in IEEE 754
11391139
if node.left.type_name() == node.right.type_name()
1140-
&& node.left_type !in [ast.f32_type, ast.f64_type] && node.op in [.eq, .ne] {
1140+
&& node.left_type !in [ast.f32_type, ast.f64_type] && node.op in [.eq, .ne]
1141+
&& node.left !is ast.StructInit && node.right !is ast.StructInit {
11411142
left_name := '${node.left}'
11421143
right_name := '${node.right}'
11431144
if left_name == right_name {

0 commit comments

Comments
 (0)