1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix auto str for map of reference struct (fix #11024) (#11028)

This commit is contained in:
yuyi 2021-08-03 13:03:00 +08:00 committed by GitHub
parent fc193bebf2
commit fd58e9f819
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

@ -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 {

View File

@ -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
}