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 cgen: implement alias operator overloading for generic struct parent type #23967

Merged
merged 3 commits into from
Mar 18, 2025
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: 15 additions & 7 deletions vlib/v/checker/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -797,20 +797,28 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
if left_sym.kind == .struct && (left_sym.info as ast.Struct).generic_types.len > 0 {
continue
}
if method := left_sym.find_method(extracted_op) {
if method := left_sym.find_method_with_generic_parent(extracted_op) {
if method.return_type != left_type_unwrapped {
c.error('operator `${extracted_op}` must return `${left_name}` to be used as an assignment operator',
node.pos)
}
} else {
if !parent_sym.is_primitive() {
if left_name == right_name {
c.error('undefined operation `${left_name}` ${extracted_op} `${right_name}`',
node.pos)
} else {
c.error('mismatched types `${left_name}` and `${right_name}`',
if method := parent_sym.find_method_with_generic_parent(extracted_op) {
if parent_sym.kind == .alias
&& (parent_sym.info as ast.Alias).parent_type != method.return_type {
c.error('operator `${extracted_op}` must return `${left_name}` to be used as an assignment operator',
node.pos)
}
} else {
if !parent_sym.is_primitive() {
if left_name == right_name {
c.error('undefined operation `${left_name}` ${extracted_op} `${right_name}`',
node.pos)
} else {
c.error('mismatched types `${left_name}` and `${right_name}`',
node.pos)
}
}
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions vlib/v/gen/c/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,19 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
g.write(';')
}
return
} else if left_sym.kind == .alias && g.table.final_sym(var_type).kind == .struct {
struct_info := g.table.final_sym(var_type)
if struct_info.info is ast.Struct && struct_info.info.generic_types.len > 0 {
mut method_name := struct_info.cname + '_' + util.replace_op(extracted_op)
method_name = g.generic_fn_name(struct_info.info.concrete_types,
method_name)
g.write(' = ${method_name}(')
g.expr(left)
g.write(', ')
g.expr(val)
g.writeln(');')
return
}
} else {
if g.table.final_sym(g.unwrap_generic(var_type)).kind == .array_fixed {
g.go_back_to(pos)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import math.vec

type V2 = vec.Vec2[f32]

fn test_alias_generic_parent_operator_overloading() {
a := V2{1, 1}
b := V2{10, 20}

mut c := a + b
assert c.x == 11.0
assert c.y == 21.0
dump(c)

c += a
assert c.x == 12.0
assert c.y == 22.0
dump(c)
}
Loading