diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index 1c92d673b226e6..208a96af14ffb3 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -46,7 +46,7 @@ fn (mut g Gen) array_init(node ast.ArrayInit, var_name string) { } for i, expr in node.exprs { if node.expr_types[i] == ast.string_type - && expr !in [ast.StringLiteral, ast.StringInterLiteral] { + && expr !in [ast.IndexExpr, ast.CallExpr, ast.StringLiteral, ast.StringInterLiteral, ast.InfixExpr] { g.write('string_clone(') g.expr(expr) g.write(')') @@ -1035,12 +1035,14 @@ fn (mut g Gen) gen_array_insert(node ast.CallExpr) { g.expr(node.args[1].expr) g.write('.len)') } else { + needs_clone := left_info.elem_type == ast.string_type + && node.args[1].expr !in [ast.IndexExpr, ast.CallExpr, ast.StringLiteral, ast.StringInterLiteral, ast.InfixExpr] g.write(', &(${elem_type_str}[]){') - if left_info.elem_type == ast.string_type { + if needs_clone { g.write('string_clone(') } g.expr_with_cast(node.args[1].expr, node.args[1].typ, left_info.elem_type) - if left_info.elem_type == ast.string_type { + if needs_clone { g.write(')') } g.write('})') diff --git a/vlib/v/gen/c/assert.v b/vlib/v/gen/c/assert.v index 63f48feca2c24d..87a970e2b5d50e 100644 --- a/vlib/v/gen/c/assert.v +++ b/vlib/v/gen/c/assert.v @@ -229,7 +229,8 @@ fn (mut g Gen) gen_assert_single_expr(expr ast.Expr, typ ast.Type) { } else { mut should_clone := true - if typ == ast.string_type && expr is ast.StringLiteral { + if typ == ast.string_type + && expr in [ast.IndexExpr, ast.CallExpr, ast.StringLiteral, ast.StringInterLiteral] { should_clone = false } if expr is ast.CTempVar { diff --git a/vlib/v/gen/c/infix.v b/vlib/v/gen/c/infix.v index 784f9140581417..bb446e4ddea887 100644 --- a/vlib/v/gen/c/infix.v +++ b/vlib/v/gen/c/infix.v @@ -1054,6 +1054,7 @@ fn (mut g Gen) infix_expr_left_shift_op(node ast.InfixExpr) { needs_clone := !g.is_builtin_mod && array_info.elem_type.idx() == ast.string_type_idx && array_info.elem_type.nr_muls() == 0 + && node.right !in [ast.StringLiteral, ast.StringInterLiteral, ast.CallExpr, ast.IndexExpr, ast.InfixExpr] if needs_clone { g.write('string_clone(') }