Skip to content

Commit

Permalink
cgen: fix gen interface to string(fix vlang#19788) (vlang#19829)
Browse files Browse the repository at this point in the history
  • Loading branch information
shove70 authored Nov 10, 2023
1 parent 11bc808 commit b3a9701
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
7 changes: 6 additions & 1 deletion vlib/v/gen/c/str.v
Original file line number Diff line number Diff line change
Expand Up @@ -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") : ')
}
Expand Down
15 changes: 15 additions & 0 deletions vlib/v/tests/interface_str_method_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -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{}'
}

0 comments on commit b3a9701

Please sign in to comment.