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:
parent
9826310882
commit
68cab00ded
@ -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.writeln(';')
|
||||
} else {
|
||||
g.gen_or_block_stmts(var_expr.str(), '', (expr as ast.Ident).or_expr.stmts,
|
||||
ret_typ, false)
|
||||
g.inside_or_block = true
|
||||
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.inside_opt_data = old_inside_opt_data
|
||||
|
14
vlib/v/tests/option_or_block_test.v
Normal file
14
vlib/v/tests/option_or_block_test.v
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user