Skip to content

Commit

Permalink
checker: fix wrong overload operator checking for generic aliased type (
Browse files Browse the repository at this point in the history
fix #22471) (#22475)
  • Loading branch information
felipensp authored Oct 10, 2024
1 parent f2cb462 commit 19a7fbc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
16 changes: 14 additions & 2 deletions vlib/v/checker/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,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
}

0 comments on commit 19a7fbc

Please sign in to comment.