cgen: fix autostr for interface with circular type (fix #23022) (#23026)

This commit is contained in:
Felipe Pena 2024-12-01 06:21:26 -03:00 committed by GitHub
parent 40dc775974
commit e421cb2bc2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 10 deletions

View File

@ -420,6 +420,7 @@ 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 {
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'
@ -431,6 +432,10 @@ 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};\n')
} else {
fn_builder.write_string2('\tif (x._typ == _${styp}_${sub_sym.cname}_index)',
' return _SLIT("<circular>");\n')
}
}
}
fn_builder.writeln('\treturn _SLIT("unknown interface value");')

View File

@ -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>]'
}