1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix gen of .sort method for > operator and improve grammar of error (#8615)

This commit is contained in:
Swastik Baranwal 2021-02-08 20:49:54 +05:30 committed by GitHub
parent e2ff2a5405
commit e2ff26a066
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 2 deletions

View File

@ -252,7 +252,7 @@ fn (mut g Gen) gen_array_sort(node ast.CallExpr) {
if !is_reverse && sym.has_method('<') && infix_expr.left.str().len == 1 {
g.definitions.writeln('\tif (${styp}__lt(*a, *b)) { return -1; } else { return 1; }}')
} else if is_reverse && sym.has_method('<') && infix_expr.left.str().len == 1 {
g.definitions.writeln('\tif (!${styp}__lt(*a, *b)) { return -1; } else { return 1; }}')
g.definitions.writeln('\tif (${styp}__lt(*b, *a)) { return -1; } else { return 1; }}')
} else {
field_type := g.typ(infix_expr.left_type)
mut left_expr_str := g.write_expr_to_string(infix_expr.left)

View File

@ -291,7 +291,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
}
p.next()
} else if p.tok.kind in [.ne, .gt, .ge, .le] && p.peek_tok.kind == .lpar {
p.error_with_pos('cannot overload `!=`, `>`, `<=` and `>=` as they are auto generated with `==` and`<`',
p.error_with_pos('cannot overload `!=`, `>`, `<=` and `>=` as they are auto generated from `==` and`<`',
p.tok.position())
} else {
pos := p.tok.position()

View File

@ -13,6 +13,7 @@ fn (a Foo) == (b Foo) bool {
fn test_operator_overloading_cmp() {
a := Foo{i: 38}
b := Foo{i: 38}
mut arr := [a, b]
assert (a > b) == false
assert (a < b) == false
@ -22,4 +23,9 @@ fn test_operator_overloading_cmp() {
//// /// //
assert b >= a
assert b <= a
//// /// //
arr.sort(a > b)
assert arr[0].i == 38
arr.sort(a < b)
assert arr[0].i == 38
}