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

master
yuyi 2022-04-25 17:36:03 +08:00 committed by GitHub
parent 60e205a193
commit 88f22b4367
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 { defer {
g.tmp_count = ctmp g.tmp_count = ctmp
} }
prev_inside_ternary := g.inside_ternary
g.inside_ternary = 0
g.stmts(node.stmts) g.stmts(node.stmts)
g.inside_ternary = prev_inside_ternary
if node.is_noreturn { if node.is_noreturn {
g.writeln('\twhile(1);') 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'
}