cgen: do not free strings in const decls

pull/5606/head
Alexander Medvednikov 2020-07-01 16:40:04 +02:00
parent 6dad3cc018
commit 013a4fc0f6
3 changed files with 11 additions and 4 deletions

View File

@ -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<T>()`
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.

View File

@ -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

View File

@ -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()