mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix gen of >
operator overloading (#8600)
This commit is contained in:
parent
1fcac4098b
commit
9b839b3b7d
@ -3378,14 +3378,18 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
|
||||
g.write(')')
|
||||
} else if node.op in [.ne, .gt, .ge, .le] && ((a && b && e) || c || d) {
|
||||
typ := g.typ(if !d { left_type } else { (left_sym.info as table.Alias).parent_type })
|
||||
if node.op == .gt {
|
||||
g.write('$typ')
|
||||
} else {
|
||||
g.write('!$typ')
|
||||
}
|
||||
g.write('_')
|
||||
if node.op == .ne {
|
||||
g.write('_eq')
|
||||
} else if node.op in [.ge, .le, .gt] {
|
||||
g.write('_lt')
|
||||
}
|
||||
if node.op == .le {
|
||||
if node.op in [.le, .gt] {
|
||||
g.write('(')
|
||||
g.expr(node.right)
|
||||
g.write(', ')
|
||||
|
25
vlib/v/tests/operator_overloading_cmp_test.v
Normal file
25
vlib/v/tests/operator_overloading_cmp_test.v
Normal file
@ -0,0 +1,25 @@
|
||||
struct Foo {
|
||||
i int
|
||||
}
|
||||
|
||||
fn (a Foo) < (b Foo) bool {
|
||||
return a.i < b.i
|
||||
}
|
||||
|
||||
fn (a Foo) == (b Foo) bool {
|
||||
return a.i == b.i
|
||||
}
|
||||
|
||||
fn test_operator_overloading_cmp() {
|
||||
a := Foo{i: 38}
|
||||
b := Foo{i: 38}
|
||||
|
||||
assert (a > b) == false
|
||||
assert (a < b) == false
|
||||
//// /// //
|
||||
assert a >= b
|
||||
assert a <= b
|
||||
//// /// //
|
||||
assert b >= a
|
||||
assert b <= a
|
||||
}
|
Loading…
Reference in New Issue
Block a user