Skip to content

Commit

Permalink
cgen: fix printing array of recursive reference struct (fix #17858) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Apr 10, 2023
1 parent 6a60db8 commit 4e498b4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
14 changes: 13 additions & 1 deletion vlib/v/gen/c/auto_str_methods.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
&Person{
name: 'John'
relatives: [<circular>]
}
Success
Original file line number Diff line number Diff line change
@@ -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')
}

0 comments on commit 4e498b4

Please sign in to comment.