From 013a4fc0f660a224730cf5b9a7f167af3e629c7c Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 1 Jul 2020 16:40:04 +0200 Subject: [PATCH] cgen: do not free strings in const decls --- vlib/v/gen/cgen.v | 8 +++++++- vlib/v/gen/fn.v | 1 + vlib/v/gen/str.v | 6 +++--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 0152d654d2..5c7ff2f3e7 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -83,7 +83,7 @@ mut: attrs []string // attributes before next decl stmt is_builtin_mod bool hotcode_fn_names []string - cur_fn &ast.FnDecl = 0 + //cur_fn ast.FnDecl cur_generic_type table.Type // `int`, `string`, etc in `foo()` sql_i int sql_stmt_name string @@ -93,6 +93,7 @@ mut: strs_to_free string inside_call bool has_main bool + inside_const bool } const ( @@ -2517,6 +2518,11 @@ fn (mut g Gen) return_statement(node ast.Return) { } fn (mut g Gen) const_decl(node ast.ConstDecl) { + g.inside_const = true + defer { + g.inside_const = false + } + for field in node.fields { name := c_name(field.name) // TODO hack. Cut the generated value and paste it into definitions. diff --git a/vlib/v/gen/fn.v b/vlib/v/gen/fn.v index 3a998b0a5b..8ec48656cb 100644 --- a/vlib/v/gen/fn.v +++ b/vlib/v/gen/fn.v @@ -28,6 +28,7 @@ fn (mut g Gen) gen_fn_decl(it ast.FnDecl) { g.cur_generic_type = 0 return } + //g.cur_fn = it fn_start_pos := g.out.len msvc_attrs := g.write_fn_attrs() // Live diff --git a/vlib/v/gen/str.v b/vlib/v/gen/str.v index 8f395eceeb..e49bcf7f82 100644 --- a/vlib/v/gen/str.v +++ b/vlib/v/gen/str.v @@ -135,7 +135,6 @@ fn (mut g Gen) string_literal(node ast.StringLiteral) { // g.write('tos4("$escaped_val", $it.val.len)') // g.write('_SLIT("$escaped_val")') g.write('tos_lit("$escaped_val")') - // g.write('tos_lit("$escaped_val")') } } @@ -143,8 +142,9 @@ fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) { mut cur_line := '' mut tmp := '' free := g.pref.autofree && g.inside_call && !g.inside_return && - g.inside_ternary == 0 && g.cur_fn != 0 && - g.cur_fn.name != '' + g.inside_ternary == 0 && !g.inside_const + //&& g.cur_fn != 0 && + //g.cur_fn.name != '' if free { // Save the string expr in a temporary variable, so that it can be removed after the call. tmp = g.new_tmp_var()