From fd58e9f819732ee7b82d358dbeb664d2b063f6b6 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 3 Aug 2021 13:03:00 +0800 Subject: [PATCH] cgen: fix auto str for map of reference struct (fix #11024) (#11028) --- vlib/v/gen/c/auto_str_methods.v | 3 ++- vlib/v/tests/string_struct_interpolation_test.v | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/vlib/v/gen/c/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v index ad1fd6b436..fe5f6d1b64 100644 --- a/vlib/v/gen/c/auto_str_methods.v +++ b/vlib/v/gen/c/auto_str_methods.v @@ -679,7 +679,8 @@ fn (mut g Gen) gen_str_for_map(info ast.Map, styp string, str_fn_name string) { tmp_str := str_intp_sq('*($val_styp*)DenseArray_value(&m.key_values, i)') g.auto_str_funcs.writeln('\t\tstrings__Builder_write_string(&sb, $tmp_str);') } else if should_use_indent_func(val_sym.kind) && !val_sym.has_method('str') { - g.auto_str_funcs.writeln('\t\tstrings__Builder_write_string(&sb, indent_${elem_str_fn_name}(*($val_styp*)DenseArray_value(&m.key_values, i), indent_count));') + ptr_str := '*'.repeat(val_typ.nr_muls()) + g.auto_str_funcs.writeln('\t\tstrings__Builder_write_string(&sb, indent_${elem_str_fn_name}(*${ptr_str}($val_styp*)DenseArray_value(&m.key_values, i), indent_count));') } else if val_sym.kind in [.f32, .f64] { tmp_val := '*($val_styp*)DenseArray_value(&m.key_values, i)' if val_sym.kind == .f32 { diff --git a/vlib/v/tests/string_struct_interpolation_test.v b/vlib/v/tests/string_struct_interpolation_test.v index 2c64efd387..11080b22dd 100644 --- a/vlib/v/tests/string_struct_interpolation_test.v +++ b/vlib/v/tests/string_struct_interpolation_test.v @@ -22,3 +22,18 @@ fn test_adding_to_mutable_string_field() { eprintln(foo.str) assert foo.str == 'hi!' } + +struct MyStruct { + a string + b int +} + +fn test_map_of_ref_struct_string() { + mut ar := map[string]&MyStruct{} + ar['a'] = &MyStruct{} + println(ar) + assert '$ar'.contains('MyStruct') + assert ('b' in ar) == false + assert ('a' in ar) == true + assert 'a' in ar +}