checker: fix wrong overload operator checking for generic aliased type (fix #22471) (#22475)

This commit is contained in:
Felipe Pena 2024-10-10 15:33:17 -03:00 committed by GitHub
parent f2cb462d26
commit 19a7fbcf28
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 2 deletions

View File

@ -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)

View File

@ -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
}