mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker, cgen: fix generic arrays sum() (#11502)
This commit is contained in:
parent
09ded16e3d
commit
d5e00b0920
@ -4274,9 +4274,9 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
|
||||
if node.op in [.plus_assign, .minus_assign, .mod_assign, .mult_assign, .div_assign]
|
||||
&& ((left_sym.kind == .struct_ && right_sym.kind == .struct_)
|
||||
|| left_sym.kind == .alias) {
|
||||
left_name := c.table.type_to_str(left_type)
|
||||
right_name := c.table.type_to_str(right_type)
|
||||
parent_sym := c.table.get_final_type_symbol(left_type)
|
||||
left_name := c.table.type_to_str(left_type_unwrapped)
|
||||
right_name := c.table.type_to_str(right_type_unwrapped)
|
||||
parent_sym := c.table.get_final_type_symbol(left_type_unwrapped)
|
||||
if left_sym.kind == .alias && right_sym.kind != .alias {
|
||||
c.error('mismatched types `$left_name` and `$right_name`', node.pos)
|
||||
}
|
||||
@ -4292,7 +4292,7 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
|
||||
continue
|
||||
}
|
||||
if method := left_sym.find_method(extracted_op) {
|
||||
if method.return_type != left_type {
|
||||
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)
|
||||
}
|
||||
|
@ -2647,7 +2647,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
|
||||
mut ident := ast.Ident{
|
||||
scope: 0
|
||||
}
|
||||
left_sym := g.table.get_type_symbol(var_type)
|
||||
left_sym := g.table.get_type_symbol(g.unwrap_generic(var_type))
|
||||
if left is ast.Ident {
|
||||
ident = left
|
||||
// id_info := ident.var_info()
|
||||
|
23
vlib/v/tests/generic_arrays_sum_test.v
Normal file
23
vlib/v/tests/generic_arrays_sum_test.v
Normal file
@ -0,0 +1,23 @@
|
||||
import arrays
|
||||
|
||||
struct Point {
|
||||
pub mut:
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
fn (p1 Point) + (p2 Point) Point {
|
||||
return Point{
|
||||
x: p1.x + p2.x
|
||||
y: p1.y + p2.y
|
||||
}
|
||||
}
|
||||
|
||||
fn test_generic_arrays_sum() {
|
||||
ret := arrays.sum<Point>([Point{ x: 1, y: 1 }, Point{
|
||||
x: 2
|
||||
y: 2
|
||||
}]) or { Point{} }
|
||||
println(ret)
|
||||
assert ret == Point{3, 3}
|
||||
}
|
Loading…
Reference in New Issue
Block a user