Skip to content

Clean up the compilation of block expressions #390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 23 additions & 46 deletions gcc/rust/backend/rust-compile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -388,16 +375,31 @@ 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)
{
std::vector<Bexpression *> 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
Expand All @@ -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<Bexpression *> 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
Expand Down
8 changes: 8 additions & 0 deletions gcc/testsuite/rust.test/execute/block_expr1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() -> i32 {
let ret = {
1;
2;
0
};
ret
}
5 changes: 5 additions & 0 deletions gcc/testsuite/rust.test/execute/func1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() -> i32 {
1;
2;
0
}