cgen: fix return_statement generated redundant `;` (#9299)

pull/9296/head
yuyi 2021-03-14 20:58:17 +08:00 committed by GitHub
parent 125be84e3d
commit c0779e8455
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 1 deletions

View File

@ -4329,6 +4329,7 @@ fn (mut g Gen) return_statement(node ast.Return) {
sym := g.table.get_type_symbol(g.fn_decl.return_type)
fn_return_is_multi := sym.kind == .multi_return
fn_return_is_optional := g.fn_decl.return_type.has_flag(.optional)
mut has_semicolon := false
if node.exprs.len == 0 {
if fn_return_is_optional {
styp := g.typ(g.fn_decl.return_type)
@ -4502,6 +4503,7 @@ fn (mut g Gen) return_statement(node ast.Return) {
g.returned_var_name = expr.name
}
g.writeln(';')
has_semicolon = true
// autofree before `return`
// set free_parent_scopes to true, since all variables defined in parent
// scopes need to be freed before the return
@ -4510,14 +4512,17 @@ fn (mut g Gen) return_statement(node ast.Return) {
}
if tmp != '' {
g.write('return $tmp')
has_semicolon = false
}
}
} else { // if node.exprs.len == 0 {
println('this should never happen')
g.write('/*F*/return')
}
if !has_semicolon {
g.writeln(';')
}
}
fn (mut g Gen) const_decl(node ast.ConstDecl) {
g.inside_const = true