cgen: use heuristic to detect circular reference in auto str (#11090)

pull/11094/head
Uwe Krüger 2021-08-07 13:56:09 +02:00 committed by GitHub
parent 7d3476cbca
commit c560d58f1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -834,6 +834,9 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, str_fn_name stri
if field.typ in ast.charptr_types { if field.typ in ast.charptr_types {
fn_builder.write_string('tos2((byteptr)$func)') fn_builder.write_string('tos2((byteptr)$func)')
} else { } else {
if field.typ.is_ptr() && sym.kind == .struct_ {
fn_builder.write_string('(indent_count > 25) ? _SLIT("<probably circular>") : ')
}
fn_builder.write_string(func) fn_builder.write_string(func)
} }
} }

View File

@ -0,0 +1,21 @@
[heap]
struct Aa {
mut:
bs []Bb
}
struct Bb {
mut:
a &Aa
}
fn test_circular() {
mut b := Bb{
a: &Aa{
bs: []Bb{cap: 1}
}
}
b.a.bs << b
s := b.str()
assert s.len < 3500
}