From f6336fc0ecbd4ce3e91c24a67c01f207d16674d5 Mon Sep 17 00:00:00 2001 From: CreeperFace <165158232+dy-tea@users.noreply.github.com> Date: Tue, 4 Nov 2025 17:53:53 +0000 Subject: [PATCH] cgen: fix passing sumtype child to generic function taking sumtype parent (fix #25660) (#25664) --- vlib/v/gen/c/fn.v | 34 +++++++++++++++++++ .../generic_array_of_sumtype_push_test.v | 28 +++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 36ca46614..3e74c6151 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -2411,6 +2411,40 @@ fn (mut g Gen) call_args(node ast.CallExpr) { } } } + if node.is_method { + left_sym := g.table.sym(node.left_type) + if left_sym.info is ast.Struct && left_sym.info.generic_types.len > 0 + && left_sym.info.concrete_types.len > 0 { + base_type_idx := g.table.find_type_idx(left_sym.name.all_before('[')) + mut func := ast.Fn{} + mut found := false + for { + if base_type_idx > 0 { + base_sym := g.table.sym(ast.idx_to_type(base_type_idx)) + if base_func := g.table.find_method(base_sym, node.name) { + func = base_func + found = true + break + } + } + if base_func := g.table.find_method(left_sym, node.name) { + func = base_func + found = true + } + break + } + if found && func.generic_names.len > 0 { + for i in 0 .. expected_types.len { + mut muttable := unsafe { &ast.Table(g.table) } + if utyp := muttable.convert_generic_type(node.expected_arg_types[i], + func.generic_names, left_sym.info.concrete_types) + { + expected_types[i] = utyp + } + } + } + } + } // only v variadic, C variadic args will be appended like normal args is_variadic := node.language == .v && node.is_variadic && expected_types.len > 0 && expected_types.last().has_flag(.variadic) diff --git a/vlib/v/tests/generics/generic_array_of_sumtype_push_test.v b/vlib/v/tests/generics/generic_array_of_sumtype_push_test.v index 1975ffaea..0402746b3 100644 --- a/vlib/v/tests/generics/generic_array_of_sumtype_push_test.v +++ b/vlib/v/tests/generics/generic_array_of_sumtype_push_test.v @@ -17,3 +17,31 @@ fn test_generic_array_of_sumtype_push() { assert ret2.len == 1 assert ret2[0] == SumType('hello') } + +struct EventA { + a u32 +} + +struct EventB { + b u32 +} + +type Event = EventA | EventB + +struct Queue[T] { +mut: + data []T +} + +fn (mut q Queue[T]) push(val T) { + q.data << val +} + +fn test_generic_queue_sumtype_structs() { + mut queue := Queue[Event]{} + queue.push(EventA{ a: 10 }) + assert queue.data.len == 1 + assert queue.data[0] == Event(EventA{ + a: 10 + }) +} -- 2.39.5