cgen: fix fn()? return

pull/5558/head
yuyi 2020-06-29 17:02:40 +08:00 committed by GitHub
parent 1ba5996404
commit c588bdee5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 1 deletions

View File

@ -667,7 +667,7 @@ pub fn rmdir_recursive(path string) {
pub fn rmdir_all(path string) ? {
mut ret_err := ''
items := os.ls(path) or {
return none
return error(err)
}
for item in items {
if os.is_dir(os.join_path(path, item)) {

View File

@ -269,6 +269,11 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
body_start_pos := p.peek_tok.position()
if p.tok.kind == .lcbr {
stmts = p.parse_block_no_scope(true)
// Add return if `fn(...) ? {...}` have no return at end
if return_type != table.void_type && p.table.get_type_symbol(return_type).kind == .void &&
return_type.has_flag(.optional) && (stmts.len == 0 || stmts[stmts.len-1] !is ast.Return) {
stmts << ast.Return{ pos: p.tok.position() }
}
}
p.close_scope()
if !no_body && are_args_type_only {