Skip to content

Commit

Permalink
cgen: fix autostr for interface with circular type (fix vlang#23022) (v…
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Dec 1, 2024
1 parent 40dc775 commit e421cb2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
25 changes: 15 additions & 10 deletions vlib/v/gen/c/auto_str_methods.v
Original file line number Diff line number Diff line change
Expand Up @@ -420,17 +420,22 @@ fn (mut g Gen) gen_str_for_interface(info ast.Interface, styp string, typ_str st
fn_builder.write_string2('\tif (x._typ == _${styp}_${sub_sym.cname}_index)',
' return ${res};')
} else {
mut val := '${func_name}(${deref}(${sub_sym.cname}*)x._${sub_sym.cname}'
if should_use_indent_func(sub_sym.kind) && !sym_has_str_method {
val += ', indent_count'
if !(sub_sym.kind == .array && g.table.sym(g.table.value_type(typ)).cname == styp) {
mut val := '${func_name}(${deref}(${sub_sym.cname}*)x._${sub_sym.cname}'
if should_use_indent_func(sub_sym.kind) && !sym_has_str_method {
val += ', indent_count'
}
val += ')'
res := 'str_intp(2, _MOV((StrIntpData[]){
{_SLIT("${clean_interface_v_type_name}("), ${si_s_code}, {.d_s = ${val}}},
{_SLIT(")"), 0, {.d_c = 0 }}
}))'
fn_builder.write_string2('\tif (x._typ == _${styp}_${sub_sym.cname}_index)',
' return ${res};\n')
} else {
fn_builder.write_string2('\tif (x._typ == _${styp}_${sub_sym.cname}_index)',
' return _SLIT("<circular>");\n')
}
val += ')'
res := 'str_intp(2, _MOV((StrIntpData[]){
{_SLIT("${clean_interface_v_type_name}("), ${si_s_code}, {.d_s = ${val}}},
{_SLIT(")"), 0, {.d_c = 0 }}
}))'
fn_builder.write_string2('\tif (x._typ == _${styp}_${sub_sym.cname}_index)',
' return ${res};\n')
}
}
fn_builder.writeln('\treturn _SLIT("unknown interface value");')
Expand Down
10 changes: 10 additions & 0 deletions vlib/v/tests/printing/dump_interface_circular_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
interface Any {}

fn test_main() {
mut a := []Any{}
a.insert(0, Any(5))
a.insert(1, Any(5.0))
a.insert(2, Any(a[0]))
a.insert(3, Any(a)) // Terminated by signal 11 (SIGSEGV)
assert dump('${a}') == '[Any(5), Any(5.0), Any(5), <circular>]'
}

0 comments on commit e421cb2

Please sign in to comment.