Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker: fix return type of alias primitive operator overloading #21663

Merged
merged 8 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion vlib/v/checker/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,27 @@ 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add comments for such large code blocks to explain what they are for

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok! will add this in my next PR

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 := right_sym.find_method(node.op.str()) {
return_type = method.return_type
}
}
}
return_sym := c.table.sym(return_type)
if return_sym.info !is ast.Alias {
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
4 changes: 2 additions & 2 deletions vlib/v/gen/native/tests/assign.vv
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ type Integer = int
fn test_alias(a Integer, b Integer) {
e := a + b
assert e == a + b
println(e)
}
println(int(e))
}
70 changes: 70 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,70 @@
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(d).name == 'Alias'

e := a * b
f := a.mul(b)
assert typeof(e).name == 'Alias'
assert typeof(f).name == 'Alias'
}

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)

assert typeof(c).name == 'AF_ARRAY'
assert typeof(y).name == 'AF_ARRAY'
}
Loading