From c560d58f1e6c575be577ecd0c6f9ad61e2575e8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kr=C3=BCger?= <45282134+UweKrueger@users.noreply.github.com> Date: Sat, 7 Aug 2021 13:56:09 +0200 Subject: [PATCH] cgen: use heuristic to detect circular reference in auto str (#11090) --- vlib/v/gen/c/auto_str_methods.v | 3 +++ vlib/v/tests/str_circular_test.v | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 vlib/v/tests/str_circular_test.v diff --git a/vlib/v/gen/c/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v index fe5f6d1b64..ffec50cf68 100644 --- a/vlib/v/gen/c/auto_str_methods.v +++ b/vlib/v/gen/c/auto_str_methods.v @@ -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("") : ') + } fn_builder.write_string(func) } } diff --git a/vlib/v/tests/str_circular_test.v b/vlib/v/tests/str_circular_test.v new file mode 100644 index 0000000000..7479937b2b --- /dev/null +++ b/vlib/v/tests/str_circular_test.v @@ -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 +}