mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix auto string method generated for []&int{len:1} (#14829)
This commit is contained in:
parent
6a4ba22eae
commit
585b5145fa
@ -586,8 +586,16 @@ fn (mut g Gen) gen_str_for_array(info ast.Array, styp string, str_fn_name string
|
||||
// Note: we need to take account of whether the user has defined
|
||||
// `fn (x T) str() {` or `fn (x &T) str() {`, and convert accordingly
|
||||
deref, deref_label := deref_kind(str_method_expects_ptr, is_elem_ptr, typ)
|
||||
g.auto_str_funcs.writeln('\t\tstrings__Builder_write_string(&sb, _SLIT("$deref_label"));')
|
||||
g.auto_str_funcs.writeln('\t\tstring x = ${elem_str_fn_name}( $deref it);')
|
||||
if is_elem_ptr {
|
||||
g.auto_str_funcs.writeln('\t\tstring x = _SLIT("nil");')
|
||||
g.auto_str_funcs.writeln('\t\tif (it != 0) {')
|
||||
g.auto_str_funcs.writeln('\t\t\tstrings__Builder_write_string(&sb, _SLIT("$deref_label"));')
|
||||
g.auto_str_funcs.writeln('\t\t\tx = ${elem_str_fn_name}(${deref}it);')
|
||||
g.auto_str_funcs.writeln('\t\t}')
|
||||
} else {
|
||||
g.auto_str_funcs.writeln('\t\tstrings__Builder_write_string(&sb, _SLIT("$deref_label"));')
|
||||
g.auto_str_funcs.writeln('\t\tstring x = ${elem_str_fn_name}(${deref}it);')
|
||||
}
|
||||
}
|
||||
}
|
||||
g.auto_str_funcs.writeln('\t\tstrings__Builder_write_string(&sb, x);')
|
||||
|
5
vlib/v/tests/string_array_of_ref_type_test.v
Normal file
5
vlib/v/tests/string_array_of_ref_type_test.v
Normal file
@ -0,0 +1,5 @@
|
||||
fn test_string_array_of_ref_type() {
|
||||
a := []&int{len: 2}
|
||||
println(a)
|
||||
assert '$a' == '[nil, nil]'
|
||||
}
|
Loading…
Reference in New Issue
Block a user