cgen: fix gen of `>` operator overloading (#8600)

pull/8606/head^2
Swastik Baranwal 2021-02-06 16:29:20 +05:30 committed by GitHub
parent 1fcac4098b
commit 9b839b3b7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 2 deletions

View File

@ -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 })
g.write('!$typ')
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(', ')

View 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
}