From f4566bb32410e017525cafc7cf2b2848e3ba4806 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 6 Apr 2021 18:38:12 +0300 Subject: [PATCH] autofree: fix `return x[0]` (optional) --- vlib/v/gen/c/index.v | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/vlib/v/gen/c/index.v b/vlib/v/gen/c/index.v index 18cb7e095b..99c6e5677e 100644 --- a/vlib/v/gen/c/index.v +++ b/vlib/v/gen/c/index.v @@ -174,7 +174,9 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) { .function { 'voidptr*' } else { '$elem_type_str*' } } - needs_clone := info.elem_type == ast.string_type_idx && g.is_autofree && !g.is_assign_lhs + // do not clone inside `opt_ok(opt_ok(&(string[]) {..})` before returns + needs_clone := info.elem_type == ast.string_type_idx && g.is_autofree && !(g.inside_return + && g.fn_decl.return_type.has_flag(.optional)) && !g.is_assign_lhs is_gen_or_and_assign_rhs := gen_or && !g.discard_or_result cur_line := if is_gen_or_and_assign_rhs { line := g.go_before_stmt(0)