cgen: fix auto str which expects ptr for ptr type (fix #23552) (#23553)

This commit is contained in:
Felipe Pena 2025-01-23 12:00:50 -03:00 committed by GitHub
parent d9a2fb1630
commit 25f14d3bbb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 1 deletions

View File

@ -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('\tstring res;')
g.auto_str_funcs.writeln('\tif (it.state == 0) {') g.auto_str_funcs.writeln('\tif (it.state == 0) {')
deref := if typ.is_ptr() { 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 { } else if expects_ptr {
'(${sym.cname}*)' '(${sym.cname}*)'
} else { } else {

View File

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