Skip to content

Commit

Permalink
parser: add checks for duplicate operator overload
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Jun 13, 2024
1 parent 68051a7 commit 5a03753
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions vlib/v/checker/tests/method_op_err.out
Original file line number Diff line number Diff line change
@@ -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 |
Expand Down Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/checker/tests/method_op_err.vv
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct Foo {
b int
}

fn (u User) + () {
fn (u User) % () {
}

fn (u User) - (f Foo) User {
Expand Down
3 changes: 3 additions & 0 deletions vlib/v/parser/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -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`<`',
Expand Down
5 changes: 5 additions & 0 deletions vlib/v/parser/tests/duplicate_operator_overload_err.out
Original file line number Diff line number Diff line change
@@ -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{}
| ^
3 changes: 3 additions & 0 deletions vlib/v/parser/tests/duplicate_operator_overload_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
struct Foo{}
fn (f Foo) * (f2 Foo) Foo{}
fn (f Foo) * (f2 Foo) Foo{}

0 comments on commit 5a03753

Please sign in to comment.