diff --git a/vlib/v/gen/c/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v index 371bae4c03..e45fedb67e 100644 --- a/vlib/v/gen/c/auto_str_methods.v +++ b/vlib/v/gen/c/auto_str_methods.v @@ -447,7 +447,7 @@ fn (mut g Gen) gen_str_for_struct(info table.Struct, styp string, str_fn_name st // generates all definitions of substructs mut fnames2strfunc := map{ '': '' - } // map[string]string // TODO vfmt bug + } for field in info.fields { sym := g.table.get_type_symbol(field.typ) if !sym.has_method('str') { @@ -510,7 +510,13 @@ fn (mut g Gen) gen_str_for_struct(info table.Struct, styp string, str_fn_name st g.auto_str_funcs.write('*') } } - g.auto_str_funcs.write(func) + // handle circular ref type of struct to the struct itself + if styp == field_styp { + g.auto_str_funcs.write('_SLIT("")') + } else { + g.auto_str_funcs.write(func) + } + if i < info.fields.len - 1 { g.auto_str_funcs.write(',\n\t\t') } diff --git a/vlib/v/tests/string_interpolation_struct_test.v b/vlib/v/tests/string_interpolation_struct_test.v index 1329ace688..bf1d3ecf7e 100644 --- a/vlib/v/tests/string_interpolation_struct_test.v +++ b/vlib/v/tests/string_interpolation_struct_test.v @@ -50,3 +50,22 @@ fn test_struct_map_field_string_interpolation() { assert s.contains("dict: {'a': 1, 'b': 2}") assert s.ends_with('}') } + +struct Circular { +mut: + next &Circular +} + +fn test_stack_circular_elem_auto_str() { + mut elem := Circular{0} + elem.next = &elem + s := '$elem'.replace('\n', '|') + assert s == 'Circular{| next: &|}' +} + +fn test_heap_circular_elem_auto_str() { + mut elem := &Circular{0} + elem.next = elem + s := '$elem'.replace('\n', '|') + assert s == '&Circular{| next: &|}' +}