cgen: fix return formatting

pull/4072/head
Joe Conigliaro 2020-03-19 22:27:47 +11:00
parent b290efa394
commit 0f08a92302
1 changed files with 4 additions and 2 deletions

View File

@ -1301,14 +1301,15 @@ fn (g mut Gen) index_expr(node ast.IndexExpr) {
} }
fn (g mut Gen) return_statement(it ast.Return) { fn (g mut Gen) return_statement(it ast.Return) {
g.write('return ') g.write('return')
if g.fn_decl.name == 'main' { if g.fn_decl.name == 'main' {
g.writeln('0;') g.writeln(' 0;')
return return
} }
fn_return_is_optional := table.type_is_optional(g.fn_decl.return_type) fn_return_is_optional := table.type_is_optional(g.fn_decl.return_type)
// multiple returns // multiple returns
if it.exprs.len > 1 { if it.exprs.len > 1 {
g.write(' ')
typ_sym := g.table.get_type_symbol(g.fn_decl.return_type) typ_sym := g.table.get_type_symbol(g.fn_decl.return_type)
mr_info := typ_sym.info as table.MultiReturn mr_info := typ_sym.info as table.MultiReturn
mut styp := g.typ(g.fn_decl.return_type) mut styp := g.typ(g.fn_decl.return_type)
@ -1331,6 +1332,7 @@ fn (g mut Gen) return_statement(it ast.Return) {
} }
// normal return // normal return
else if it.exprs.len == 1 { else if it.exprs.len == 1 {
g.write(' ')
// `return opt_ok(expr)` for functions that expect an optional // `return opt_ok(expr)` for functions that expect an optional
if fn_return_is_optional && !table.type_is_optional(it.types[0]) { if fn_return_is_optional && !table.type_is_optional(it.types[0]) {
mut is_none := false mut is_none := false