mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: use heuristic to detect circular reference in auto str (#11090)
This commit is contained in:
parent
7d3476cbca
commit
c560d58f1e
@ -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 {
|
||||
fn_builder.write_string('tos2((byteptr)$func)')
|
||||
} else {
|
||||
if field.typ.is_ptr() && sym.kind == .struct_ {
|
||||
fn_builder.write_string('(indent_count > 25) ? _SLIT("<probably circular>") : ')
|
||||
}
|
||||
fn_builder.write_string(func)
|
||||
}
|
||||
}
|
||||
|
21
vlib/v/tests/str_circular_test.v
Normal file
21
vlib/v/tests/str_circular_test.v
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user