mirror of
https://github.com/vlang/v.git
synced 2025-09-16 02:49:31 -04:00
cgen: fix reference variable str() method call (#21753)
This commit is contained in:
parent
17e32d051b
commit
4a7c70c909
@ -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)
|
||||
|
17
vlib/v/tests/reference_variable_str_test.v
Normal file
17
vlib/v/tests/reference_variable_str_test.v
Normal file
@ -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']
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user