Skip to content

Commit

Permalink
cgen: fix aliases of fixed array infix expression
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Nov 21, 2024
1 parent ca67273 commit ccb7dad
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
26 changes: 21 additions & 5 deletions vlib/v/gen/c/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,20 @@ fn (mut g Gen) infix_expr_eq_op(node ast.InfixExpr) {
if eq_operator_expects_ptr {
g.write('&')
}
g.expr(node.left)
if node.left is ast.ArrayInit && g.table.sym(node.left_type).kind == .array_fixed {
g.fixed_array_init_with_cast(node.left, node.left_type)
} else {
g.expr(node.left)
}
g.write2(', ', '*'.repeat(right.typ.nr_muls()))
if eq_operator_expects_ptr {
g.write('&')
}
g.expr(node.right)
if node.right is ast.ArrayInit && g.table.sym(node.right_type).kind == .array_fixed {
g.fixed_array_init_with_cast(node.right, node.right_type)
} else {
g.expr(node.right)
}
g.write(')')
} else if left.unaliased.idx() == right.unaliased.idx()
&& left.sym.kind in [.array, .array_fixed, .alias, .map, .struct, .sum_type, .interface] {
Expand Down Expand Up @@ -414,7 +422,7 @@ fn (mut g Gen) infix_expr_cmp_op(node ast.InfixExpr) {
g.expr(node.left)
g.write(')')
}
} else if left.sym.kind == right.sym.kind && has_operator_overloading {
} else if left.unaliased_sym.kind == right.unaliased_sym.kind && has_operator_overloading {
if node.op in [.le, .ge] {
g.write('!')
}
Expand All @@ -424,12 +432,20 @@ fn (mut g Gen) infix_expr_cmp_op(node ast.InfixExpr) {
if operator_expects_ptr {
g.write('&')
}
g.expr(node.left)
if node.left is ast.ArrayInit && g.table.sym(node.left_type).kind == .array_fixed {
g.fixed_array_init_with_cast(node.left, node.left_type)
} else {
g.expr(node.left)
}
g.write2(', ', '*'.repeat(right.typ.nr_muls()))
if operator_expects_ptr {
g.write('&')
}
g.expr(node.right)
if node.right is ast.ArrayInit && g.table.sym(node.right_type).kind == .array_fixed {
g.fixed_array_init_with_cast(node.right, node.right_type)
} else {
g.expr(node.right)
}
g.write(')')
} else {
g.write2('(', '*'.repeat(right.typ.nr_muls()))
Expand Down
28 changes: 28 additions & 0 deletions vlib/v/tests/aliases/alias_fixed_array_infix_expr_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import encoding.binary

pub type Addr = [4]u8

pub fn (a Addr) u32() u32 {
return binary.big_endian_u32_fixed(a)
}

pub fn (a Addr) == (oth Addr) bool {
return a.u32() == oth.u32()
}

pub fn (a Addr) < (oth Addr) bool {
return a.u32() < oth.u32()
}

fn test_alias_fixed_array_infix_expr() {
addr := Addr([u8(127), 0, 0, 1]!)

assert addr == Addr([u8(127), 0, 0, 1]!)
assert Addr([u8(127), 0, 0, 1]!) == addr

assert addr == [u8(127), 0, 0, 1]!
assert [u8(127), 0, 0, 1]! == addr

assert addr < Addr([u8(127), 0, 0, 2]!)
assert addr < [u8(127), 0, 0, 2]!
}

0 comments on commit ccb7dad

Please sign in to comment.