Skip to content

Commit

Permalink
checker: fix return type of alias primitive operator overloading
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Jun 10, 2024
1 parent 0d1113c commit 97a10ac
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
20 changes: 19 additions & 1 deletion vlib/v/checker/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,25 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
c.check_div_mod_by_zero(node.right, node.op)
}

return_type = promoted_type
left_sym = c.table.sym(unwrapped_left_type)
right_sym = c.table.sym(unwrapped_right_type)
if left_sym.info is ast.Alias
&& c.table.sym(left_sym.info.parent_type).is_primitive() {
if left_sym.has_method(node.op.str()) {
if method := left_sym.find_method(node.op.str()) {
return_type = method.return_type
}
}
} else if right_sym.info is ast.Alias
&& c.table.sym(right_sym.info.parent_type).is_primitive() {
if right_sym.has_method(node.op.str()) {
if method := left_sym.find_method(node.op.str()) {
return_type = method.return_type
}
}
} else {
return_type = promoted_type
}
}
}
.gt, .lt, .ge, .le {
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/checker/tests/method_op_alias_err.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ vlib/v/checker/tests/method_op_alias_err.vv:4:18: error: expected `Foo` not `Foo
| ~~~~
5 | return Foo2(f + f1)
6 | }
vlib/v/checker/tests/method_op_alias_err.vv:5:17: error: infix expr: cannot use `string` (right expression) as `string`
vlib/v/checker/tests/method_op_alias_err.vv:5:17: error: infix expr: cannot use `Foo2` (right expression) as `Foo`
3 |
4 | fn (f Foo) + (f1 Foo2) Foo2 {
5 | return Foo2(f + f1)
Expand Down
31 changes: 31 additions & 0 deletions vlib/v/tests/alias_primitive_operator_overloading_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
type Alias = u8

fn new_alias() Alias {
return 0
}

fn (a Alias) add(b Alias) Alias {
return new_alias()
}

fn (a Alias) mul(b Alias) Alias {
return new_alias()
}

fn (a Alias) + (b Alias) Alias {
return a.add(b)
}

fn (a Alias) * (b Alias) Alias {
return a.mul(b)
}

fn test_alias_primitive_operator_overloading() {
a := new_alias()
b := new_alias()

c := a + b
d := a.add(b)
assert typeof(c).name == 'Alias'
assert typeof(c).name == 'Alias'
}
30 changes: 30 additions & 0 deletions vlib/v/tests/alias_voidptr_operator_overloading_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
type AF_ARRAY = voidptr

fn (a AF_ARRAY) add(b AF_ARRAY) AF_ARRAY {
mut y := AF_ARRAY(0)
return y
}

fn (a AF_ARRAY) mul(b AF_ARRAY) AF_ARRAY {
mut y := AF_ARRAY(0)
return y
}

fn (a AF_ARRAY) + (b AF_ARRAY) AF_ARRAY {
return a.add(b)
}

fn (a AF_ARRAY) * (b AF_ARRAY) AF_ARRAY {
return a.mul(b)
}

fn test_alias_voidptr_operator_overloading() {
a := AF_ARRAY(0)
b := AF_ARRAY(0)

c := a + b
y := a * a

assert c == a.add(b)
assert y == a.mul(a)
}

0 comments on commit 97a10ac

Please sign in to comment.