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

cgen: fix $tmpl() in returning match expr (fix #16109, fix #16124) (#16229)

This commit is contained in:
yuyi 2022-10-27 23:03:27 +08:00 committed by GitHub
parent e356a74a4c
commit 0ca5b1f6ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 1 deletions

View File

@ -126,6 +126,7 @@ mut:
inside_match_result bool
inside_vweb_tmpl bool
inside_return bool
inside_return_tmpl bool
inside_struct_init bool
inside_or_block bool
inside_call bool
@ -4281,7 +4282,9 @@ fn (mut g Gen) return_stmt(node ast.Return) {
if node.exprs.len > 0 {
// skip `return $vweb.html()`
if node.exprs[0] is ast.ComptimeCall && (node.exprs[0] as ast.ComptimeCall).is_vweb {
g.inside_return_tmpl = true
g.expr(node.exprs[0])
g.inside_return_tmpl = false
g.writeln(';')
return
}

View File

@ -75,7 +75,7 @@ fn (mut g Gen) comptime_call(mut node ast.ComptimeCall) {
} else {
// return $tmpl string
g.write(cur_line)
if g.inside_return {
if g.inside_return_tmpl {
g.write('return ')
}
g.write('_tmpl_res_$fn_name')

View File

@ -0,0 +1 @@
@name

View File

@ -0,0 +1,51 @@
fn foo1() string {
return match true {
true {
name := 'abc'
$tmpl('tmpl/template.txt')
}
else {
'else'
}
}
}
fn foo2() string {
return if true {
name := 'abc'
$tmpl('tmpl/template.txt')
} else {
'else'
}
}
struct Res {
mut:
str string
}
fn foo3() Res {
name := 'abc'
return Res{
str: $tmpl('tmpl/template.txt')
}
}
fn new_res(str string) Res {
mut res := Res{}
res.str = str
return res
}
fn foo4() Res {
name := 'abc'
return new_res($tmpl('tmpl/template.txt'))
}
fn test_tmpl_in_return_match_expr() {
println(foo1())
println(foo2())
println(foo3())
println(foo4())
assert true
}