Skip to content

Commit 25f14d3

Browse files
authored
cgen: fix auto str which expects ptr for ptr type (fix #23552) (#23553)
1 parent d9a2fb1 commit 25f14d3

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

‎vlib/v/gen/c/auto_str_methods.v‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ fn (mut g Gen) gen_str_for_option(typ ast.Type, styp string, str_fn_name string)
203203
g.auto_str_funcs.writeln('\tstring res;')
204204
g.auto_str_funcs.writeln('\tif (it.state == 0) {')
205205
deref := if typ.is_ptr() {
206-
'**(${sym.cname}**)&'
206+
dot := if expects_ptr { '*'.repeat(typ.nr_muls()) } else { '*'.repeat(typ.nr_muls() + 1) }
207+
'${dot}(${sym.cname}**)&'
207208
} else if expects_ptr {
208209
'(${sym.cname}*)'
209210
} else {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import net.html
2+
3+
fn test_nil() {
4+
mut doc := html.parse('<body><div class="Box-footer"><div class="truncate">abc</div></div></body>')
5+
footer := doc.get_tags_by_class_name('Box-footer')[0]
6+
hrefs := footer.get_tag_by_class_name('Truncate')
7+
println(hrefs)
8+
res := '${hrefs}'
9+
assert res == '&Option(&nil)'
10+
}
11+
12+
fn test_non_nil() {
13+
mut doc := html.parse('<body><div class="Box-footer"><div class="Truncate">abc</div></div></body>')
14+
footer := doc.get_tags_by_class_name('Box-footer')[0]
15+
hrefs := footer.get_tag_by_class_name('Truncate')
16+
println(hrefs)
17+
res := '${hrefs}'
18+
assert res == '&Option(<div class="Truncate" >abc</div>)'
19+
}

0 commit comments

Comments
 (0)