cgen: fix auto string method generated for []&int{len:1} (#14829)

master
yuyi 2022-06-22 19:54:04 +08:00 committed by GitHub
parent 6a4ba22eae
commit 585b5145fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View File

@ -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);')

View File

@ -0,0 +1,5 @@
fn test_string_array_of_ref_type() {
a := []&int{len: 2}
println(a)
assert '$a' == '[nil, nil]'
}