From 6436d9a8273216adf486dca158cfb198e63a5c59 Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 7 Jul 2021 17:42:53 +0800 Subject: [PATCH] cgen: fix error of generic array typedef (#10679) --- vlib/v/gen/c/cgen.v | 6 ++++- vlib/v/tests/generics_array_typedef_test.v | 31 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/generics_array_typedef_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index d07a6a6d9d..463344965a 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -786,7 +786,11 @@ pub fn (mut g Gen) write_typedef_types() { } match typ.kind { .array { - g.type_definitions.writeln('typedef array $typ.cname;') + info := typ.info as ast.Array + elem_sym := g.table.get_type_symbol(info.elem_type) + if elem_sym.kind != .placeholder { + g.type_definitions.writeln('typedef array $typ.cname;') + } } .array_fixed { info := typ.info as ast.ArrayFixed diff --git a/vlib/v/tests/generics_array_typedef_test.v b/vlib/v/tests/generics_array_typedef_test.v new file mode 100644 index 0000000000..24b204989a --- /dev/null +++ b/vlib/v/tests/generics_array_typedef_test.v @@ -0,0 +1,31 @@ +struct Node { +mut: + data T + next &Node = 0 +} + +struct SinglyLinkedList { +mut: + first_node &Node = 0 +} + +fn init_singlylinkedlist(nodes []Node) SinglyLinkedList { + mut current_node := &nodes[0] + + for i in 0 .. nodes.len - 1 { + current_node = &nodes[i] + current_node.next = &nodes[i + 1] + } + + return SinglyLinkedList{&nodes[0]} +} + +fn test_generic_array_typedef() { + sll := init_singlylinkedlist([Node{ data: 1 }, Node{ + data: 2 + }, Node{ + data: 798 + }]) + println(sll.first_node.next) + assert sll.first_node.next.data == 2 +}