cgen: fix gen interface to string(fix #19788) (#19829)

This commit is contained in:
shove 2023-11-11 00:15:53 +08:00 committed by GitHub
parent 11bc8084de
commit b3a9701129
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -113,12 +113,17 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) {
if is_ptr && !is_var_mut {
ref_str := '&'.repeat(typ.nr_muls())
g.write('str_intp(1, _MOV((StrIntpData[]){{_SLIT("${ref_str}"), ${si_s_code} ,{.d_s = isnil(')
if is_ptr && typ.has_flag(.option) {
if typ.has_flag(.option) {
g.write('*(${g.base_type(exp_typ)}*)&')
g.expr(expr)
g.write('.data')
g.write(') ? _SLIT("Option(&nil)") : ')
} else {
inside_casting_to_str_old := g.inside_casting_to_str
g.inside_casting_to_str = false
defer {
g.inside_casting_to_str = inside_casting_to_str_old
}
g.expr(expr)
g.write(') ? _SLIT("nil") : ')
}

View File

@ -18,3 +18,18 @@ fn test_interface_str_method() {
ret := printer(s)
assert ret == 's'
}
// for test interface gen to string
interface Abc {}
struct Xyz {}
fn test_interface_gen_to_string() {
d := Abc(Xyz{})
mut res := ''
if d is Xyz {
println(d)
res = '${d}'
}
assert res == '&Xyz{}'
}