cgen: fix error of generic array typedef (#10679)

pull/10698/head
yuyi 2021-07-07 17:42:53 +08:00 committed by GitHub
parent f070222124
commit 6436d9a827
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 1 deletions

View File

@ -786,8 +786,12 @@ pub fn (mut g Gen) write_typedef_types() {
} }
match typ.kind { match typ.kind {
.array { .array {
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;') g.type_definitions.writeln('typedef array $typ.cname;')
} }
}
.array_fixed { .array_fixed {
info := typ.info as ast.ArrayFixed info := typ.info as ast.ArrayFixed
elem_sym := g.table.get_type_symbol(info.elem_type) elem_sym := g.table.get_type_symbol(info.elem_type)

View File

@ -0,0 +1,31 @@
struct Node<T> {
mut:
data T
next &Node<T> = 0
}
struct SinglyLinkedList<T> {
mut:
first_node &Node<T> = 0
}
fn init_singlylinkedlist<T>(nodes []Node<T>) SinglyLinkedList<T> {
mut current_node := &nodes[0]
for i in 0 .. nodes.len - 1 {
current_node = &nodes[i]
current_node.next = &nodes[i + 1]
}
return SinglyLinkedList<T>{&nodes[0]}
}
fn test_generic_array_typedef() {
sll := init_singlylinkedlist<int>([Node<int>{ data: 1 }, Node<int>{
data: 2
}, Node<int>{
data: 798
}])
println(sll.first_node.next)
assert sll.first_node.next.data == 2
}