From fd07f7df6924d4f2ab41c55be7284359eb1cb4f4 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 14 Jan 2023 06:41:31 +0800 Subject: [PATCH] cgen: fix printing struct having fields of arrays of anonymous structs (#16953) --- .../checker/tests/comptime_dump_fields_var_test.out | 4 ++-- vlib/v/gen/c/auto_str_methods.v | 2 +- ...ting_struct_with_arrays_of_anon_struct_field.out | 5 +++++ ...nting_struct_with_arrays_of_anon_struct_field.vv | 13 +++++++++++++ 4 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 vlib/v/slow_tests/inout/printing_struct_with_arrays_of_anon_struct_field.out create mode 100644 vlib/v/slow_tests/inout/printing_struct_with_arrays_of_anon_struct_field.vv diff --git a/vlib/v/checker/tests/comptime_dump_fields_var_test.out b/vlib/v/checker/tests/comptime_dump_fields_var_test.out index ce67fd3b2..ce59179bc 100644 --- a/vlib/v/checker/tests/comptime_dump_fields_var_test.out +++ b/vlib/v/checker/tests/comptime_dump_fields_var_test.out @@ -2,7 +2,7 @@ [vlib/v/checker/tests/comptime_dump_fields_var_test.vv:13] val.$field.name: d [vlib/v/checker/tests/comptime_dump_fields_var_test.vv:13] val.$field.name: 1 [vlib/v/checker/tests/comptime_dump_fields_var_test.vv:13] val.$field.name: 0 -[vlib/v/checker/tests/comptime_dump_fields_var_test.vv:13] val.$field.name: _VAnonStruct1{ +[vlib/v/checker/tests/comptime_dump_fields_var_test.vv:13] val.$field.name: struct { i: 100 } -ok \ No newline at end of file +ok diff --git a/vlib/v/gen/c/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v index d336ee6c9..41031be22 100644 --- a/vlib/v/gen/c/auto_str_methods.v +++ b/vlib/v/gen/c/auto_str_methods.v @@ -829,7 +829,7 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, typ_str string, g.auto_fn_definitions << fn_builder.str() } fn_builder.writeln('static string indent_${str_fn_name}(${styp} it, int indent_count) {') - clean_struct_v_type_name := util.strip_main_name(typ_str) + clean_struct_v_type_name := if info.is_anon { 'struct ' } else { util.strip_main_name(typ_str) } // generate ident / indent length = 4 spaces if info.fields.len == 0 { fn_builder.writeln('\treturn _SLIT("${clean_struct_v_type_name}{}");') diff --git a/vlib/v/slow_tests/inout/printing_struct_with_arrays_of_anon_struct_field.out b/vlib/v/slow_tests/inout/printing_struct_with_arrays_of_anon_struct_field.out new file mode 100644 index 000000000..c9a430d5d --- /dev/null +++ b/vlib/v/slow_tests/inout/printing_struct_with_arrays_of_anon_struct_field.out @@ -0,0 +1,5 @@ +Abc{ + a: [struct { + b: 'this is b' + }] +} diff --git a/vlib/v/slow_tests/inout/printing_struct_with_arrays_of_anon_struct_field.vv b/vlib/v/slow_tests/inout/printing_struct_with_arrays_of_anon_struct_field.vv new file mode 100644 index 000000000..fa2b7d494 --- /dev/null +++ b/vlib/v/slow_tests/inout/printing_struct_with_arrays_of_anon_struct_field.vv @@ -0,0 +1,13 @@ +struct Abc { + a []struct { + b string + } +} + +fn main() { + abc := Abc{ + a: [struct {'this is b'}] + } + + println(abc) +} -- 2.39.5