From e433badcb86e0a3630160d5a68e90f465eb50439 Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 8 Dec 2021 16:12:02 +0800 Subject: [PATCH] parser, cgen: fix multiple comptime tmpl in one function (#12757) --- vlib/v/gen/c/comptime.v | 4 +-- vlib/v/parser/comptime.v | 2 +- vlib/v/parser/tmpl.v | 32 +++++++++---------- .../multiple_comptime_tmpl_in_one_fn_test.v | 11 +++++++ vlib/v/tests/tmpl/a.txt | 1 + vlib/v/tests/tmpl/b.txt | 1 + 6 files changed, 31 insertions(+), 20 deletions(-) create mode 100644 vlib/v/tests/multiple_comptime_tmpl_in_one_fn_test.v create mode 100644 vlib/v/tests/tmpl/a.txt create mode 100644 vlib/v/tests/tmpl/b.txt diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 4bcd86bf24..7f82aaab18 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -62,12 +62,12 @@ fn (mut g Gen) comptime_call(mut node ast.ComptimeCall) { } } + fn_name := g.fn_decl.name.replace('.', '__') + node.pos.pos.str() if is_html { // return vweb html template - g.writeln('vweb__Context_html(&app->Context, _tmpl_res_$g.fn_decl.name); strings__Builder_free(&sb); string_free(&_tmpl_res_$g.fn_decl.name);') + g.writeln('vweb__Context_html(&app->Context, _tmpl_res_$fn_name); strings__Builder_free(&sb_$fn_name); string_free(&_tmpl_res_$fn_name);') } else { // return $tmpl string - fn_name := g.fn_decl.name.replace('.', '__') g.write(cur_line) if g.inside_return { g.write('return ') diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index 61e5195af4..af19d93d0a 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -192,7 +192,7 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall { } // println('path is now "$path"') } - tmp_fn_name := p.cur_fn_name.replace('.', '__') + tmp_fn_name := p.cur_fn_name.replace('.', '__') + start_pos.pos.str() $if trace_comptime ? { println('>>> compiling comptime template file "$path" for $tmp_fn_name') } diff --git a/vlib/v/parser/tmpl.v b/vlib/v/parser/tmpl.v index ed608d94e0..5e6a53895f 100644 --- a/vlib/v/parser/tmpl.v +++ b/vlib/v/parser/tmpl.v @@ -8,10 +8,6 @@ import v.errors import os import strings -const tmpl_str_start = "sb.write_string('" - -const tmpl_str_end = "')\n" - enum State { html css //