]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Handle tail expression normalization right before lowering to HIR.
authorMatthew Jasper <mjjasper1@gmail.com>
Thu, 8 Jun 2023 18:29:44 +0000 (19:29 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:46:28 +0000 (18:46 +0100)
This allows braced macros at the end of blocks to correctly expand to
zero or more statements followed by a tail expression. Parsing still
creates a tail expression for now.

gcc/rust/ChangeLog:

* ast/rust-ast.cc (BlockExpr::strip_tail_expr):
Don't normalize tail expression in this method.
(BlockExpr::normalize_tail_expr): New method that only does the normalization.
* ast/rust-expr.h: Declare new method.
* hir/rust-ast-lower-block.h: Normalize tail expressions on blocks before lowering.

Signed-off-by: Matthew Jasper <mjjasper1@gmail.com>
gcc/rust/ast/rust-ast.cc
gcc/rust/ast/rust-expr.h
gcc/rust/hir/rust-ast-lower-block.h

index 06b28e0f0018c22401bc5335016b8e2b35f3ef9f..2eac09ba5d37c905b9477d0735fc9c64a4070452 100644 (file)
@@ -4223,12 +4223,10 @@ Attribute::is_parsed_to_meta_item () const
 }
 
 void
-BlockExpr::strip_tail_expr ()
+BlockExpr::normalize_tail_expr ()
 {
-  if (expr)
+  if (!expr)
     {
-      expr = nullptr;
-
       // HACK: try to turn the last statement into a tail expression
       if (statements.size () && statements.back ()->is_expr ())
        {
index 3e7c93c5098fde159a05cc858ab9538c7a948b7a..adf9b684527e46aba4a66fff80be2c89c0b01c08 100644 (file)
@@ -2488,7 +2488,9 @@ public:
   }
 
   // Removes the tail expression from the block.
-  void strip_tail_expr ();
+  void strip_tail_expr () { expr = nullptr; }
+  // Normalizes a trailing statement without a semicolon to a tail expression.
+  void normalize_tail_expr ();
 
   const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
   std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; }
index a4d6069b7634e1c1bfaf857f75e384183d014106..300c24e0ab6d6d174b36ffa80634763f7b310ab0 100644 (file)
@@ -33,6 +33,7 @@ public:
   static HIR::BlockExpr *translate (AST::BlockExpr *expr, bool *terminated)
   {
     ASTLoweringBlock resolver;
+    expr->normalize_tail_expr ();
     expr->accept_vis (resolver);
     if (resolver.translated != nullptr)
       {