Skip to content

Commit

Permalink
cgen: fix reference variable str() method call
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Jun 28, 2024
1 parent 17e32d0 commit 8c14215
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
7 changes: 6 additions & 1 deletion vlib/v/gen/c/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,12 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
// TODO2
// g.generate_tmp_autofree_arg_vars(node, name)
if !node.receiver_type.is_ptr() && left_type.is_ptr() && node.name == 'str' {
g.write('ptr_str(')
if left_type.is_int_valptr() {
g.write('ptr_str(')
} else {
g.gen_expr_to_string(node.left, left_type)
return
}
} else if node.receiver_type.is_ptr() && left_type.is_ptr() && node.name == 'str'
&& !left_sym.has_method('str') {
g.gen_expr_to_string(node.left, left_type)
Expand Down
17 changes: 17 additions & 0 deletions vlib/v/tests/reference_variable_str_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@[heap]
struct Foo {
bar string = 'bar'
baz string = 'baz'
}

fn (foo Foo) str() string {
return 'bar: ${foo.bar}, baz: ${foo.baz}'
}

fn test_reference_variable_str() {
mut many_foos := []&Foo{len: 3, init: &Foo{}}
println(many_foos.map(it.str()).join('\n'))
println(many_foos)
assert many_foos.map(it.str()) == ['&bar: bar, baz: baz', '&bar: bar, baz: baz',
'&bar: bar, baz: baz']
}

0 comments on commit 8c14215

Please sign in to comment.