cgen: fix fixed array of function str() (fix #10744) (#10753)

pull/10770/head
yuyi 2021-07-12 00:24:33 +08:00 committed by GitHub
parent 938e9b61b5
commit a3ed9c1bbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 1 deletions

View File

@ -563,11 +563,12 @@ fn (mut g Gen) gen_str_for_array_fixed(info ast.ArrayFixed, styp string, str_fn_
g.auto_str_funcs.writeln('static string indent_${str_fn_name}($styp a, int indent_count) {')
g.auto_str_funcs.writeln('\tstrings__Builder sb = strings__new_builder($info.size * 10);')
g.auto_str_funcs.writeln('\tstrings__Builder_write_string(&sb, _SLIT("["));')
g.auto_str_funcs.writeln('\tfor (int i = 0; i < $info.size; ++i) {')
if sym.kind == .function {
g.auto_str_funcs.writeln('\t\tstring x = ${elem_str_fn_name}();')
g.auto_str_funcs.writeln('\t\tstrings__Builder_write_string(&sb, x);')
} else {
deref, deref_label := deref_kind(str_method_expects_ptr, is_elem_ptr, typ)
g.auto_str_funcs.writeln('\tfor (int i = 0; i < $info.size; ++i) {')
if should_use_indent_func(sym.kind) && !sym_has_str_method {
if is_elem_ptr {
g.auto_str_funcs.writeln('\t\tstrings__Builder_write_string(&sb, _SLIT("$deref_label"));')

View File

@ -438,3 +438,9 @@ fn test_multi_return() {
value: 'two'
})"
}
fn test_fixed_array_of_function() {
a := [println, println]!
println(a)
assert '$a' == '[fn (string), fn (string)]'
}