diff --git a/vlib/v/checker/tests/method_op_err.out b/vlib/v/checker/tests/method_op_err.out index c1ee67430df69c..bea0c96be4498d 100644 --- a/vlib/v/checker/tests/method_op_err.out +++ b/vlib/v/checker/tests/method_op_err.out @@ -1,7 +1,7 @@ vlib/v/checker/tests/method_op_err.vv:11:1: error: operator methods should have exactly 1 argument 9 | } 10 | - 11 | fn (u User) + () { + 11 | fn (u User) % () { | ~~~~~~~~~~~~~~~~ 12 | } 13 | @@ -68,7 +68,7 @@ vlib/v/checker/tests/method_op_err.vv:38:5: error: operator %= not defined on le | ^ 39 | u += User{2, 3} 40 | } -vlib/v/checker/tests/method_op_err.vv:38:7: error: undefined operation `User` % `User` +vlib/v/checker/tests/method_op_err.vv:38:7: error: operator `%` must return `User` to be used as an assignment operator 36 | _ = u 37 | u += 12 38 | u %= User{1, 3} diff --git a/vlib/v/checker/tests/method_op_err.vv b/vlib/v/checker/tests/method_op_err.vv index 24a82dd20a86e3..5a389bc7c5720d 100644 --- a/vlib/v/checker/tests/method_op_err.vv +++ b/vlib/v/checker/tests/method_op_err.vv @@ -8,7 +8,7 @@ struct Foo { b int } -fn (u User) + () { +fn (u User) % () { } fn (u User) - (f Foo) User { diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index b1abc016c27799..2a353d365c1743 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -362,6 +362,9 @@ fn (mut p Parser) fn_decl() ast.FnDecl { p.error_with_pos('cannot use operator overloading with normal functions', p.tok.pos()) } + if type_sym.has_method(name) { + p.error_with_pos('cannot duplicate operator overload `${name}`', p.tok.pos()) + } 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 from `==` and`<`', diff --git a/vlib/v/parser/tests/duplicate_operator_overload_err.out b/vlib/v/parser/tests/duplicate_operator_overload_err.out new file mode 100644 index 00000000000000..c83fd64aadadb4 --- /dev/null +++ b/vlib/v/parser/tests/duplicate_operator_overload_err.out @@ -0,0 +1,5 @@ +vlib/v/parser/tests/duplicate_operator_overload_err.vv:3:12: error: cannot duplicate operator overload `*` + 1 | struct Foo{} + 2 | fn (f Foo) * (f2 Foo) Foo{} + 3 | fn (f Foo) * (f2 Foo) Foo{} + | ^ diff --git a/vlib/v/parser/tests/duplicate_operator_overload_err.vv b/vlib/v/parser/tests/duplicate_operator_overload_err.vv new file mode 100644 index 00000000000000..e5671037bd9955 --- /dev/null +++ b/vlib/v/parser/tests/duplicate_operator_overload_err.vv @@ -0,0 +1,3 @@ +struct Foo{} +fn (f Foo) * (f2 Foo) Foo{} +fn (f Foo) * (f2 Foo) Foo{}