alex

/

v Public
0 Issues 1 Contributor 0 Releases 4 Branches
Additions: 75 Deletions: 3 View patch
1 p.fn_language = language
2 }
3 mut name := ''
4+ mut type_sym := p.table.sym(rec.typ)
5 name_pos := p.tok.pos()
6 if p.tok.kind == .name {
7 // TODO high order fn
8 scope: 0
9 }
10 }
11- type_sym := p.table.sym(rec.typ)
12 if is_method {
13 mut is_duplicate := type_sym.has_method(name)
14 // make sure this is a normal method and not an interface method
15 if type_sym.kind == .interface_ && is_duplicate {
16- if type_sym.info is ast.Interface {
17+ if mut type_sym.info is ast.Interface {
18 // if the method is in info then its an interface method
19 is_duplicate = !type_sym.info.has_method(name)
20 }
21 } else if p.tok.kind in [.ne, .gt, .ge, .le] && p.peek_tok.kind == .lpar {
22 p.error_with_pos('cannot overload `!=`, `>`, `<=` and `>=` as they are auto generated from `==` and`<`',
23 p.tok.pos())
24+ } else if p.tok.kind in [.plus_assign, .minus_assign, .div_assign, .mult_assign, .mod_assign] {
25+ extracted_op := match p.tok.kind {
26+ .plus_assign { '+' }
27+ .minus_assign { '-' }
28+ .div_assign { '/' }
29+ .mod_assign { '%' }
30+ .mult_assign { '*' }
31+ else { 'unknown op' }
32+ }
33+ if type_sym.has_method(extracted_op) {
34+ p.error('cannot overload `${p.tok.kind}`, operator is implicitly overloaded because the `${extracted_op}` operator is overloaded')
35+ }
36+ p.error('cannot overload `${p.tok.kind}`, overload `${extracted_op}` and `${p.tok.kind}` will be automatically generated')
37 } else {
38 p.error_with_pos('expecting method name', p.tok.pos())
39 return ast.FnDecl{
40 }
41 // Register
42 if is_method {
43- mut type_sym := p.table.sym(rec.typ)
44 // Do not allow to modify / add methods to types from other modules
45 // arrays/maps dont belong to a module only their element types do
46 // we could also check if kind is .array, .array_fixed, .map instead of mod.len
47
1new file mode 100644
2+vlib/v/parser/tests/argumented_op_overloading_fn_decl_err.vv:10:14: error: cannot overload `+=`, overload `+` and `+=` will be automatically generated
3+ 8 | }
4+ 9 |
5+ 10 | fn (p Point) += (q Point) Point {
6+ | ~~
7+ 11 | return Point{p.x + q.x, p.y + q.y}
8+ 12 | }
9
1new file mode 100644
2+struct Point {
3+ x i64
4+ y i64
5+}
6+
7+fn (p Point) foo() {
8+ println(p.x)
9+}
10+
11+fn (p Point) += (q Point) Point {
12+ return Point{p.x + q.x, p.y + q.y}
13+}
14+
15+fn main() {
16+ mut p := Point{1, 2}
17+ q := Point{3, 4}
18+ p += q
19+ p -= q
20+ println(p)
21+}
22+
23
1new file mode 100644
2+vlib/v/parser/tests/argumented_op_overloading_fn_op_overloaded_decl_err.vv:14:14: error: cannot overload `+=`, operator is implicitly overloaded because the `+` operator is overloaded
3+ 12 | }
4+ 13 |
5+ 14 | fn (p Point) += (q Point) Point {
6+ | ~~
7+ 15 | return Point{p.x + q.x, p.y + q.y}
8+ 16 | }
9
1new file mode 100644
2+struct Point {
3+ x i64
4+ y i64
5+}
6+
7+fn (p Point) foo() {
8+ println(p.x)
9+}
10+
11+fn (p Point) + (q Point) Point {
12+ return Point{p.x + q.x, p.y + q.y}
13+}
14+
15+fn (p Point) += (q Point) Point {
16+ return Point{p.x + q.x, p.y + q.y}
17+}
18+
19+fn main() {
20+ mut p := Point{1, 2}
21+ q := Point{3, 4}
22+ p += q
23+ p -= q
24+ println(p)
25+}
26+
27