diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 60f64956d2..93bb24934c 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -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 } diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 06fd4dd26c..5a8c023c48 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -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') diff --git a/vlib/v/tests/tmpl/template.txt b/vlib/v/tests/tmpl/template.txt new file mode 100644 index 0000000000..75cbda31df --- /dev/null +++ b/vlib/v/tests/tmpl/template.txt @@ -0,0 +1 @@ +@name diff --git a/vlib/v/tests/tmpl_in_return_match_expr_test.v b/vlib/v/tests/tmpl_in_return_match_expr_test.v new file mode 100644 index 0000000000..28e57fc672 --- /dev/null +++ b/vlib/v/tests/tmpl_in_return_match_expr_test.v @@ -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 +}