cgen: fix error for array of reference auto_str (#13665)

pull/13670/head
yuyi 2022-03-06 18:15:31 +08:00 committed by GitHub
parent b0f651bf81
commit c8e33ad219
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -610,7 +610,11 @@ fn (mut g Gen) gen_str_for_array(info ast.Array, styp string, str_fn_name string
// Rune are managed at this level as strings
g.auto_str_funcs.writeln('\t\tstring x = str_intp(2, _MOV((StrIntpData[]){{_SLIT("\`"), $c.si_s_code, {.d_s = ${elem_str_fn_name}(it) }}, {_SLIT("\`"), 0, {.d_c = 0 }}}));\n')
} else if sym.kind == .string {
if is_elem_ptr {
g.auto_str_funcs.writeln('\t\tstring x = str_intp(2, _MOV((StrIntpData[]){{_SLIT("&\'"), $c.si_s_code, {.d_s = *it }}, {_SLIT("\'"), 0, {.d_c = 0 }}}));\n')
} else {
g.auto_str_funcs.writeln('\t\tstring x = str_intp(2, _MOV((StrIntpData[]){{_SLIT("\'"), $c.si_s_code, {.d_s = it }}, {_SLIT("\'"), 0, {.d_c = 0 }}}));\n')
}
} else {
// There is a custom .str() method, so use it.
// NB: we need to take account of whether the user has defined

View File

@ -0,0 +1,7 @@
fn test_str_array_of_reference() {
names := ['John', 'Paul', 'George', 'Ringo']
a := unsafe { [&names[0], &names[1]] }
println(a[0])
println(a)
assert '$a' == "[&'John', &'Paul']"
}