From 19a7fbcf28b7ab9cf87a4ab7e45f0b08014cbe83 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Thu, 10 Oct 2024 15:33:17 -0300 Subject: [PATCH] checker: fix wrong overload operator checking for generic aliased type (fix #22471) (#22475) --- vlib/v/checker/infix.v | 16 ++++++++++++-- .../aliases/alias_with_op_overloading_test.v | 22 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/aliases/alias_with_op_overloading_test.v diff --git a/vlib/v/checker/infix.v b/vlib/v/checker/infix.v index 24e4f97b338200..ac518b95b69aaa 100644 --- a/vlib/v/checker/infix.v +++ b/vlib/v/checker/infix.v @@ -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) diff --git a/vlib/v/tests/aliases/alias_with_op_overloading_test.v b/vlib/v/tests/aliases/alias_with_op_overloading_test.v new file mode 100644 index 00000000000000..af80256e9d9ef1 --- /dev/null +++ b/vlib/v/tests/aliases/alias_with_op_overloading_test.v @@ -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 +}