From 9b839b3b7dc50e3f741d6a66c79ca85e20b18ba6 Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Sat, 6 Feb 2021 16:29:20 +0530 Subject: [PATCH] cgen: fix gen of `>` operator overloading (#8600) --- vlib/v/gen/c/cgen.v | 8 +++++-- vlib/v/tests/operator_overloading_cmp_test.v | 25 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/operator_overloading_cmp_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index be17006b9a..d0c5e35bca 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -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(', ') diff --git a/vlib/v/tests/operator_overloading_cmp_test.v b/vlib/v/tests/operator_overloading_cmp_test.v new file mode 100644 index 0000000000..1c94d45747 --- /dev/null +++ b/vlib/v/tests/operator_overloading_cmp_test.v @@ -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 +}