diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc index 9375dd021f45..48935418d84b 100644 --- a/gcc/rust/backend/rust-compile.cc +++ b/gcc/rust/backend/rust-compile.cc @@ -234,32 +234,19 @@ CompileBlock::visit (HIR::BlockExpr &expr) for (auto &s : expr.get_statements ()) { auto compiled_expr = CompileStmt::Compile (s.get (), ctx); - if (compiled_expr == nullptr) - continue; - - if (result == nullptr) + if (compiled_expr != nullptr) { - Bstatement *final_stmt + Bstatement *compiled_stmt = ctx->get_backend ()->expression_statement (fnctx.fndecl, compiled_expr); - ctx->add_statement (final_stmt); - } - else - { - Bexpression *result_reference - = ctx->get_backend ()->var_expression (result, - s->get_locus_slow ()); - - Bstatement *assignment = ctx->get_backend ()->assignment_statement ( - fnctx.fndecl, result_reference, compiled_expr, expr.get_locus ()); - ctx->add_statement (assignment); + ctx->add_statement (compiled_stmt); } } if (expr.has_expr ()) { - // the previous passes will ensure this is a valid return - // dead code elimination should remove any bad trailing expressions + // the previous passes will ensure this is a valid return or + // a valid trailing expression Bexpression *compiled_expr = CompileExpr::Compile (expr.expr.get (), ctx); if (compiled_expr != nullptr) { @@ -388,6 +375,21 @@ HIRCompileBase::compile_function_body ( for (auto &s : function_body->get_statements ()) { auto compiled_expr = CompileStmt::Compile (s.get (), ctx); + if (compiled_expr != nullptr) + { + Bstatement *compiled_stmt + = ctx->get_backend ()->expression_statement (fndecl, compiled_expr); + ctx->add_statement (compiled_stmt); + } + } + + if (function_body->has_expr ()) + { + // the previous passes will ensure this is a valid return + // or a valid trailing expression + Bexpression *compiled_expr + = CompileExpr::Compile (function_body->expr.get (), ctx); + if (compiled_expr != nullptr) { if (has_return_type) @@ -395,9 +397,9 @@ HIRCompileBase::compile_function_body ( std::vector retstmts; retstmts.push_back (compiled_expr); - auto ret - = ctx->get_backend ()->return_statement (fndecl, retstmts, - s->get_locus_slow ()); + auto ret = ctx->get_backend ()->return_statement ( + fndecl, retstmts, + function_body->get_final_expr ()->get_locus_slow ()); ctx->add_statement (ret); } else @@ -409,31 +411,6 @@ HIRCompileBase::compile_function_body ( } } } - - if (function_body->has_expr ()) - { - // the previous passes will ensure this is a valid return - // dead code elimination should remove any bad trailing expressions - Bexpression *compiled_expr - = CompileExpr::Compile (function_body->expr.get (), ctx); - - if (has_return_type && compiled_expr) - { - std::vector retstmts; - retstmts.push_back (compiled_expr); - - auto ret = ctx->get_backend ()->return_statement ( - fndecl, retstmts, - function_body->get_final_expr ()->get_locus_slow ()); - ctx->add_statement (ret); - } - else if (compiled_expr) - { - Bstatement *final_stmt - = ctx->get_backend ()->expression_statement (fndecl, compiled_expr); - ctx->add_statement (final_stmt); - } - } } } // namespace Compile diff --git a/gcc/testsuite/rust.test/execute/block_expr1.rs b/gcc/testsuite/rust.test/execute/block_expr1.rs new file mode 100644 index 000000000000..d561f8cab593 --- /dev/null +++ b/gcc/testsuite/rust.test/execute/block_expr1.rs @@ -0,0 +1,8 @@ +fn main() -> i32 { + let ret = { + 1; + 2; + 0 + }; + ret +} diff --git a/gcc/testsuite/rust.test/execute/func1.rs b/gcc/testsuite/rust.test/execute/func1.rs new file mode 100644 index 000000000000..0a093d885877 --- /dev/null +++ b/gcc/testsuite/rust.test/execute/func1.rs @@ -0,0 +1,5 @@ +fn main() -> i32 { + 1; + 2; + 0 +}