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

cgen: fix or-block with return void statement (#17527)

This commit is contained in:
Felipe Pena 2023-03-07 11:01:46 -03:00 committed by GitHub
parent 9826310882
commit 68cab00ded
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 2 deletions

View File

@ -20,8 +20,24 @@ fn (mut g Gen) expr_with_opt_or_block(expr ast.Expr, expr_typ ast.Type, var_expr
g.gen_option_error(g.cur_fn.return_type, expr) g.gen_option_error(g.cur_fn.return_type, expr)
g.writeln(';') g.writeln(';')
} else { } else {
g.gen_or_block_stmts(var_expr.str(), '', (expr as ast.Ident).or_expr.stmts, g.inside_or_block = true
ret_typ, false) defer {
g.inside_or_block = false
}
stmts := (expr as ast.Ident).or_expr.stmts
// handles stmt block which returns something
// e.g. { return none }
if stmts.len > 0 && stmts.last() is ast.ExprStmt
&& (stmts.last() as ast.ExprStmt).typ != ast.void_type {
g.gen_or_block_stmts(var_expr.str(), '', stmts, ret_typ, false)
} else {
// handles stmt block which doesn't returns value
// e.g. { return }
g.stmts(stmts)
if stmts.len > 0 && stmts.last() is ast.ExprStmt {
g.writeln(';')
}
}
} }
g.writeln('}') g.writeln('}')
g.inside_opt_data = old_inside_opt_data g.inside_opt_data = old_inside_opt_data

View File

@ -0,0 +1,14 @@
fn foo(val ?int) {
if val == none {
a := val or { return }
assert false
println(a)
}
assert false
}
fn test_main() {
foo(none)
assert true
}