Skip to content

Commit de1c431

Browse files
authored
cgen: fix auto str for map with ptr str (#20741)
1 parent 349741d commit de1c431

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

vlib/v/gen/c/auto_str_methods.v

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,9 +806,11 @@ fn (mut g Gen) gen_str_for_map(info ast.Map, styp string, str_fn_name string) {
806806
tmp_str := str_intp_rune('${elem_str_fn_name}(*(${val_styp}*)DenseArray_value(&m.key_values, i))')
807807
g.auto_str_funcs.writeln('\t\tstrings__Builder_write_string(&sb, ${tmp_str});')
808808
} else {
809-
ptr_str := '*'.repeat(if receiver_is_ptr { val_typ.nr_muls() - 1 } else { val_typ.nr_muls() })
809+
ptr_str := '*'.repeat(val_typ.nr_muls())
810810
if val_typ.has_flag(.option) {
811811
g.auto_str_funcs.writeln('\t\tstrings__Builder_write_string(&sb, ${g.get_str_fn(val_typ)}(*${ptr_str}(${val_styp}*)DenseArray_value(&m.key_values, i)));')
812+
} else if receiver_is_ptr {
813+
g.auto_str_funcs.writeln('\t\tstrings__Builder_write_string(&sb, ${elem_str_fn_name}(${ptr_str}(${val_styp}*)DenseArray_value(&m.key_values, i)));')
812814
} else {
813815
g.auto_str_funcs.writeln('\t\tstrings__Builder_write_string(&sb, ${elem_str_fn_name}(*${ptr_str}(${val_styp}*)DenseArray_value(&m.key_values, i)));')
814816
}

vlib/v/tests/map_auto_str_ptr_test.v

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module main
2+
3+
struct Blob {
4+
label int
5+
}
6+
7+
fn (mut b Blob) str() string {
8+
return ''
9+
}
10+
11+
struct Labels {
12+
blobs map[int]Blob
13+
}
14+
15+
fn break_it() Labels {
16+
mut a := Labels{}
17+
return a
18+
}
19+
20+
fn test_main() {
21+
mut a := break_it()
22+
assert a.blobs.len == 0
23+
}

0 commit comments

Comments
 (0)