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 wrong overload operator checking for generic aliased type #22475

Merged
merged 2 commits into from
Oct 10, 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
16 changes: 14 additions & 2 deletions vlib/v/checker/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,24 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
} else {
return_type = right_type
}
} else if right_final_sym.has_method(node.op.str()) {
if method := right_final_sym.find_method(node.op.str()) {
} else if right_final_sym.has_method_with_generic_parent(node.op.str()) {
if method := right_final_sym.find_method_with_generic_parent(node.op.str()) {
return_type = method.return_type
} else {
return_type = right_type
}
} else if left_sym.has_method(node.op.str()) {
if method := left_sym.find_method(node.op.str()) {
return_type = method.return_type
} else {
return_type = left_type
}
} else if left_final_sym.has_method_with_generic_parent(node.op.str()) {
if method := left_final_sym.find_method_with_generic_parent(node.op.str()) {
return_type = method.return_type
} else {
return_type = left_type
}
} else {
left_name := c.table.type_to_str(unwrapped_left_type)
right_name := c.table.type_to_str(unwrapped_right_type)
Expand Down
22 changes: 22 additions & 0 deletions vlib/v/tests/aliases/alias_with_op_overloading_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module main

import math.vec

type Vector3 = vec.Vec3[f32]

pub fn calc(a Vector3, b Vector3) Vector3 {
f := a.normalize()
return f * a + a * f
}

fn test_main() {
a := Vector3{
x: 1
y: 2
z: 3
}
t := calc(a, a)
assert int(t.x) == 0
assert int(t.y) == 2
assert int(t.z) == 4
}
Loading