Skip to content

Commit

Permalink
cgen: fix auto str which expects ptr for ptr type (fix #23552) (#23553)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Jan 23, 2025
1 parent d9a2fb1 commit 25f14d3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion vlib/v/gen/c/auto_str_methods.v
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ fn (mut g Gen) gen_str_for_option(typ ast.Type, styp string, str_fn_name string)
g.auto_str_funcs.writeln('\tstring res;')
g.auto_str_funcs.writeln('\tif (it.state == 0) {')
deref := if typ.is_ptr() {
'**(${sym.cname}**)&'
dot := if expects_ptr { '*'.repeat(typ.nr_muls()) } else { '*'.repeat(typ.nr_muls() + 1) }
'${dot}(${sym.cname}**)&'
} else if expects_ptr {
'(${sym.cname}*)'
} else {
Expand Down
19 changes: 19 additions & 0 deletions vlib/v/tests/printing/print_option_ref_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import net.html

fn test_nil() {
mut doc := html.parse('<body><div class="Box-footer"><div class="truncate">abc</div></div></body>')
footer := doc.get_tags_by_class_name('Box-footer')[0]
hrefs := footer.get_tag_by_class_name('Truncate')
println(hrefs)
res := '${hrefs}'
assert res == '&Option(&nil)'
}

fn test_non_nil() {
mut doc := html.parse('<body><div class="Box-footer"><div class="Truncate">abc</div></div></body>')
footer := doc.get_tags_by_class_name('Box-footer')[0]
hrefs := footer.get_tag_by_class_name('Truncate')
println(hrefs)
res := '${hrefs}'
assert res == '&Option(<div class="Truncate" >abc</div>)'
}

0 comments on commit 25f14d3

Please sign in to comment.