diff options
author | Matthew Jasper <mjjasper1@gmail.com> | 2023-06-08 19:29:44 +0100 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2023-06-20 08:10:23 +0000 |
commit | cbca7bdde221f68da59cd6d624cd34bf439b04b7 (patch) | |
tree | d3af8b272396b2ae209619362cf70306877cfa09 | |
parent | 3a0f10e7984f8b2457201cfd8676e1351ac3b7a0 (diff) | |
download | gcc-cbca7bdde221f68da59cd6d624cd34bf439b04b7.zip gcc-cbca7bdde221f68da59cd6d624cd34bf439b04b7.tar.gz gcc-cbca7bdde221f68da59cd6d624cd34bf439b04b7.tar.bz2 |
gccrs: Handle tail expression normalization right before lowering to HIR.
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>
-rw-r--r-- | gcc/rust/ast/rust-ast.cc | 6 | ||||
-rw-r--r-- | gcc/rust/ast/rust-expr.h | 4 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-block.h | 1 |
3 files changed, 6 insertions, 5 deletions
diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc index dd7aaa6..4a2c952 100644 --- a/gcc/rust/ast/rust-ast.cc +++ b/gcc/rust/ast/rust-ast.cc @@ -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 ()) { diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h index 3e7c93c..adf9b68 100644 --- a/gcc/rust/ast/rust-expr.h +++ b/gcc/rust/ast/rust-expr.h @@ -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; } diff --git a/gcc/rust/hir/rust-ast-lower-block.h b/gcc/rust/hir/rust-ast-lower-block.h index 0f90549..c945c31 100644 --- a/gcc/rust/hir/rust-ast-lower-block.h +++ b/gcc/rust/hir/rust-ast-lower-block.h @@ -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) { |