diff options
author | Owen Avery <powerboat9.gamer@gmail.com> | 2023-04-26 09:29:04 -0400 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2024-01-16 18:34:16 +0100 |
commit | b181ea7289b229b1e5971ccbbdd4f74dbdacefa9 (patch) | |
tree | 895086ac63c219ccd31d21eed616da74d7aa554c /gcc/rust | |
parent | 35a4c25ae45adc7439327c8981e63f824c88fa5c (diff) | |
download | gcc-b181ea7289b229b1e5971ccbbdd4f74dbdacefa9.zip gcc-b181ea7289b229b1e5971ccbbdd4f74dbdacefa9.tar.gz gcc-b181ea7289b229b1e5971ccbbdd4f74dbdacefa9.tar.bz2 |
gccrs: Handle replacing stripped tail expressions
gcc/rust/ChangeLog:
* ast/rust-ast.cc
(BlockExpr::strip_tail_expr):
Try to take new tail expression from statements list.
* ast/rust-expr.h
(BlockExpr::strip_tail_expr):
Replace definition with only declaration.
gcc/testsuite/ChangeLog:
* rust/execute/torture/cfg-tail.rs: New test.
Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
Diffstat (limited to 'gcc/rust')
-rw-r--r-- | gcc/rust/ast/rust-ast.cc | 25 | ||||
-rw-r--r-- | gcc/rust/ast/rust-expr.h | 2 |
2 files changed, 26 insertions, 1 deletions
diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc index a664a31..7a9dc8a 100644 --- a/gcc/rust/ast/rust-ast.cc +++ b/gcc/rust/ast/rust-ast.cc @@ -4213,6 +4213,31 @@ Attribute::is_parsed_to_meta_item () const return has_attr_input () && attr_input->is_meta_item (); } +void +BlockExpr::strip_tail_expr () +{ + if (expr) + { + expr = nullptr; + + // HACK: try to turn the last statement into a tail expression + if (statements.size () && statements.back ()->is_expr ()) + { + auto &stmt = static_cast<ExprStmt &> (*statements.back ()); + + if (stmt.get_type () == ExprStmt::ExprStmtType::WITH_BLOCK) + { + auto &stmt_block = static_cast<ExprStmtWithBlock &> (stmt); + if (!stmt_block.is_semicolon_followed ()) + { + expr = std::move (stmt_block.get_expr ()); + statements.pop_back (); + } + } + } + } +} + /* Visitor implementations - these are short but inlining can't happen anyway * due to virtual functions and I didn't want to make the ast header includes * any longer than they already are. */ diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h index abe6e03..c43baf3 100644 --- a/gcc/rust/ast/rust-expr.h +++ b/gcc/rust/ast/rust-expr.h @@ -2446,7 +2446,7 @@ public: } // Removes the tail expression from the block. - void strip_tail_expr () { expr = nullptr; } + void strip_tail_expr (); const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; } |