1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix fixed array of string item concatenation (#17801)

This commit is contained in:
Felipe Pena 2023-03-28 08:56:54 -03:00 committed by GitHub
parent da153aa780
commit db8331da24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 4 deletions

View File

@ -419,10 +419,16 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
mut op_expected_left := ast.Type(0) mut op_expected_left := ast.Type(0)
mut op_expected_right := ast.Type(0) mut op_expected_right := ast.Type(0)
if node.op == .plus_assign && unaliased_right_sym.kind == .string { if node.op == .plus_assign && unaliased_right_sym.kind == .string {
if left is ast.IndexExpr { if mut left is ast.IndexExpr {
// a[0] += str => `array_set(&a, 0, &(string[]) {string__plus(...))})` if g.table.sym(left.left_type).kind == .array_fixed {
g.expr(left) // strs[0] += str2 => `strs[0] = string__plus(strs[0], str2)`
g.write('string__plus(') g.expr(left)
g.write(' = string__plus(')
} else {
// a[0] += str => `array_set(&a, 0, &(string[]) {string__plus(...))})`
g.expr(left)
g.write('string__plus(')
}
} else { } else {
// str += str2 => `str = string__plus(str, str2)` // str += str2 => `str = string__plus(str, str2)`
g.expr(left) g.expr(left)

View File

@ -0,0 +1,11 @@
fn test_array() {
mut strs := ['abc', 'cde']!
strs[0] += 'def'
assert strs[0] == 'abcdef'
}
fn test_fixed_array() {
mut strs := ['abc', 'cde']
strs[0] += 'def'
assert strs[0] == 'abcdef'
}