]> gcc.gnu.org Git - gcc.git/commitdiff
gccrs: Handle replacing stripped tail expressions
authorOwen Avery <powerboat9.gamer@gmail.com>
Wed, 26 Apr 2023 13:29:04 +0000 (09:29 -0400)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:34:16 +0000 (18:34 +0100)
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>
gcc/rust/ast/rust-ast.cc
gcc/rust/ast/rust-expr.h
gcc/testsuite/rust/execute/torture/cfg-tail.rs [new file with mode: 0644]

index a664a319513a2c4ea2fc5f1b84c51eff8e71f74e..7a9dc8a008b5ecba18cc44c37021176dc87b26d7 100644 (file)
@@ -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. */
index abe6e03ce45b23759e98b2502067eef783809b19..c43baf3cadec3f749b5c71becbf9328b31a94b94 100644 (file)
@@ -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; }
diff --git a/gcc/testsuite/rust/execute/torture/cfg-tail.rs b/gcc/testsuite/rust/execute/torture/cfg-tail.rs
new file mode 100644 (file)
index 0000000..69411a6
--- /dev/null
@@ -0,0 +1,9 @@
+fn foo() -> i32 {
+    {54}
+    #[cfg(all(A, not(A)))]
+    {45}
+}
+
+fn main() -> i32 {
+    return foo() - 54;
+}
This page took 0.074148 seconds and 5 git commands to generate.