cgen: fix error for anon fn decl inside ternary (#14150)

yuyi 2022-04-25 17:36:03 +08:00 committed by Jef Roosens
parent 81963b51ab
commit b314b8d8bf
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 28 additions and 0 deletions

View File

@ -379,7 +379,10 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) {
defer {
g.tmp_count = ctmp
}
prev_inside_ternary := g.inside_ternary
g.inside_ternary = 0
g.stmts(node.stmts)
g.inside_ternary = prev_inside_ternary
if node.is_noreturn {
g.writeln('\twhile(1);')
}

View File

@ -0,0 +1,25 @@
fn foofun(op rune) fn () string {
return match op {
`1` {
fn () string {
return '1 passed'
}
}
`2` {
fn () string {
return '2 passed'
}
}
else {
fn () string {
return 'Nor 1 or 2 passed'
}
}
}
}
fn test_anon_fn_decl_inside_ternary() {
a := foofun(`1`)
println(a())
assert a() == '1 passed'
}