Skip to content

Commit e421cb2

Browse files
authored
cgen: fix autostr for interface with circular type (fix #23022) (#23026)
1 parent 40dc775 commit e421cb2

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

vlib/v/gen/c/auto_str_methods.v

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -420,17 +420,22 @@ fn (mut g Gen) gen_str_for_interface(info ast.Interface, styp string, typ_str st
420420
fn_builder.write_string2('\tif (x._typ == _${styp}_${sub_sym.cname}_index)',
421421
' return ${res};')
422422
} else {
423-
mut val := '${func_name}(${deref}(${sub_sym.cname}*)x._${sub_sym.cname}'
424-
if should_use_indent_func(sub_sym.kind) && !sym_has_str_method {
425-
val += ', indent_count'
423+
if !(sub_sym.kind == .array && g.table.sym(g.table.value_type(typ)).cname == styp) {
424+
mut val := '${func_name}(${deref}(${sub_sym.cname}*)x._${sub_sym.cname}'
425+
if should_use_indent_func(sub_sym.kind) && !sym_has_str_method {
426+
val += ', indent_count'
427+
}
428+
val += ')'
429+
res := 'str_intp(2, _MOV((StrIntpData[]){
430+
{_SLIT("${clean_interface_v_type_name}("), ${si_s_code}, {.d_s = ${val}}},
431+
{_SLIT(")"), 0, {.d_c = 0 }}
432+
}))'
433+
fn_builder.write_string2('\tif (x._typ == _${styp}_${sub_sym.cname}_index)',
434+
' return ${res};\n')
435+
} else {
436+
fn_builder.write_string2('\tif (x._typ == _${styp}_${sub_sym.cname}_index)',
437+
' return _SLIT("<circular>");\n')
426438
}
427-
val += ')'
428-
res := 'str_intp(2, _MOV((StrIntpData[]){
429-
{_SLIT("${clean_interface_v_type_name}("), ${si_s_code}, {.d_s = ${val}}},
430-
{_SLIT(")"), 0, {.d_c = 0 }}
431-
}))'
432-
fn_builder.write_string2('\tif (x._typ == _${styp}_${sub_sym.cname}_index)',
433-
' return ${res};\n')
434439
}
435440
}
436441
fn_builder.writeln('\treturn _SLIT("unknown interface value");')
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
interface Any {}
2+
3+
fn test_main() {
4+
mut a := []Any{}
5+
a.insert(0, Any(5))
6+
a.insert(1, Any(5.0))
7+
a.insert(2, Any(a[0]))
8+
a.insert(3, Any(a)) // Terminated by signal 11 (SIGSEGV)
9+
assert dump('${a}') == '[Any(5), Any(5.0), Any(5), <circular>]'
10+
}

0 commit comments

Comments
 (0)