1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: fix error of $tmpl in anon_fn (fix #8847) (#8849)

This commit is contained in:
yuyi 2021-02-21 00:51:08 +08:00 committed by GitHub
parent 8327c9afc1
commit c704a49283
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 6 deletions

View File

@ -551,6 +551,14 @@ fn (mut p Parser) anon_fn() ast.AnonFn {
p.tok.position()) p.tok.position())
} }
mut label_names := []string{} 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 { if p.tok.kind == .lcbr {
tmp := p.label_names tmp := p.label_names
p.label_names = [] p.label_names = []
@ -558,13 +566,8 @@ fn (mut p Parser) anon_fn() ast.AnonFn {
label_names = p.label_names label_names = p.label_names
p.label_names = tmp p.label_names = tmp
} }
p.cur_fn_name = keep_fn_name
p.close_scope() 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 func.name = name
idx := p.table.find_or_register_fn_type(p.mod, func, true, false) idx := p.table.find_or_register_fn_type(p.mod, func, true, false)
typ := table.new_type(idx) typ := table.new_type(idx)

View File

@ -13,6 +13,26 @@ age: 25
numbers: [1, 2, 3] 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 1
2 2