1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix printing array of recursive reference struct (fix #17858) (#17922)

This commit is contained in:
yuyi 2023-04-10 22:09:27 +08:00 committed by GitHub
parent 6a60db8768
commit 4e498b4303
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 1 deletions

View File

@ -969,9 +969,21 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, typ_str string,
funcprefix += '*'
}
}
mut is_field_array := false
if sym.info is ast.Array {
field_styp = g.typ(sym.info.elem_type).trim('*')
is_field_array = true
} else if sym.info is ast.ArrayFixed {
field_styp = g.typ(sym.info.elem_type).trim('*')
is_field_array = true
}
// handle circular ref type of struct to the struct itself
if styp == field_styp && !allow_circular {
fn_body.write_string('${funcprefix}_SLIT("<circular>")')
if is_field_array {
fn_body.write_string('it.${c_name(field.name)}.len > 0 ? ${funcprefix}_SLIT("[<circular>]") : ${funcprefix}_SLIT("[]")')
} else {
fn_body.write_string('${funcprefix}_SLIT("<circular>")')
}
} else {
// manage C charptr
if field.typ in ast.charptr_types {

View File

@ -0,0 +1,5 @@
&Person{
name: 'John'
relatives: [<circular>]
}
Success

View File

@ -0,0 +1,18 @@
struct Person {
name string
mut:
relatives []&Person
}
fn main() {
mut father := &Person{
name: 'John'
}
mut mother := &Person{
name: 'Jane'
relatives: [father]
}
father.relatives = [mother]
println(father)
println('Success')
}