From c704a49283a1d9e95f7f5141a135800f1ab7ec1e Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 21 Feb 2021 00:51:08 +0800 Subject: [PATCH] parser: fix error of $tmpl in anon_fn (fix #8847) (#8849) --- vlib/v/parser/fn.v | 15 +++++++++------ vlib/v/tests/tmpl_test.v | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 8db0f089fd..1ef666e6ae 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -551,6 +551,14 @@ fn (mut p Parser) anon_fn() ast.AnonFn { p.tok.position()) } mut label_names := []string{} + mut func := table.Fn{ + params: args + is_variadic: is_variadic + return_type: return_type + } + name := 'anon_fn_${p.table.fn_type_signature(func)}_$p.tok.pos' + keep_fn_name := p.cur_fn_name + p.cur_fn_name = name if p.tok.kind == .lcbr { tmp := p.label_names p.label_names = [] @@ -558,13 +566,8 @@ fn (mut p Parser) anon_fn() ast.AnonFn { label_names = p.label_names p.label_names = tmp } + p.cur_fn_name = keep_fn_name p.close_scope() - mut func := table.Fn{ - params: args - is_variadic: is_variadic - return_type: return_type - } - name := 'anon_fn_${p.table.fn_type_signature(func)}_$p.tok.pos' func.name = name idx := p.table.find_or_register_fn_type(p.mod, func, true, false) typ := table.new_type(idx) diff --git a/vlib/v/tests/tmpl_test.v b/vlib/v/tests/tmpl_test.v index 0a6711ce13..3734715b3d 100644 --- a/vlib/v/tests/tmpl_test.v +++ b/vlib/v/tests/tmpl_test.v @@ -13,6 +13,26 @@ age: 25 numbers: [1, 2, 3] +1 + +2 + +3' +} + +fn test_tmpl_in_anon_fn() { + anon := fn (name string, age int, numbers []int) string { + return $tmpl('tmpl/1.txt') + } + + println(anon('Peter', 25, [1, 2, 3])) + assert anon('Peter', 25, [1, 2, 3]).trim_space() == 'name: Peter + +age: 25 + +numbers: [1, 2, 3] + + 1 2