From 9fcca599a59c2379bdf5a9171b9e4fedafd49781 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Wed, 13 Aug 2025 03:11:01 -0300 Subject: [PATCH] cgen: fix codegen for generic struct field array option (fix #25093) (#25097) --- vlib/v/gen/c/cgen.v | 4 +- .../options/option_generic_array_field_test.v | 39 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/options/option_generic_array_field_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index f45150d7c..50bf954ec 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5444,7 +5444,7 @@ fn (mut g Gen) ident(node ast.Ident) { } if i == 0 && node.obj.ct_type_var != .smartcast && node.obj.is_unwrapped { dot := if (!node.obj.ct_type_unwrapped && !node.obj.orig_type.is_ptr() - && obj_sym.is_heap()) + && !node.obj.orig_type.has_flag(.generic) && obj_sym.is_heap()) || node.obj.orig_type.has_flag(.option_mut_param_t) { '->' } else { @@ -6727,7 +6727,7 @@ fn (mut g Gen) write_types(symbols []&ast.TypeSymbol) { for opt_field in opt_fields { field_sym := g.table.final_sym(opt_field.typ) arr := field_sym.info as ast.ArrayFixed - if !arr.elem_type.has_flag(.option) { + if !arr.elem_type.has_flag(.option) || arr.elem_type.has_flag(.generic) { continue } styp := field_sym.cname diff --git a/vlib/v/tests/options/option_generic_array_field_test.v b/vlib/v/tests/options/option_generic_array_field_test.v new file mode 100644 index 000000000..675d6b52d --- /dev/null +++ b/vlib/v/tests/options/option_generic_array_field_test.v @@ -0,0 +1,39 @@ +module main + +@[heap] +struct FLQueue[T] { +mut: + inner [8]?T +} + +fn (mut f FLQueue[T]) put[T](element T) { + for index, item in f.inner { + if item == none { + f.inner[index] = element + return + } + } + panic('put: No free slot') +} + +fn (mut f FLQueue[T]) get[T]() ?T { + for item in f.inner { + if item != none { + return item + } + } + return none +} + +@[heap] +struct Thread { + stack [1024]u8 +} + +fn test_main() { + mut q := FLQueue[Thread]{} + t := Thread{} + q.put(t) + p := q.get() + println(p) +} -- 2.39.5