diff options
-rw-r--r-- | gcc/rust/ast/rust-expr.h | 2 | ||||
-rw-r--r-- | gcc/rust/expand/rust-expand-visitor.cc | 22 | ||||
-rw-r--r-- | gcc/rust/expand/rust-expand-visitor.h | 3 |
3 files changed, 17 insertions, 10 deletions
diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h index 2791a8c..8ea2c8c 100644 --- a/gcc/rust/ast/rust-expr.h +++ b/gcc/rust/ast/rust-expr.h @@ -2503,6 +2503,8 @@ public: // Normalizes a trailing statement without a semicolon to a tail expression. void normalize_tail_expr (); + void try_convert_last_stmt (); + 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/expand/rust-expand-visitor.cc b/gcc/rust/expand/rust-expand-visitor.cc index 6df9f7c..d79aeac 100644 --- a/gcc/rust/expand/rust-expand-visitor.cc +++ b/gcc/rust/expand/rust-expand-visitor.cc @@ -173,11 +173,11 @@ expand_stmt_attribute (T &item, AST::SimplePath &name, MacroExpander &expander) } void -expand_tail_expr (AST::BlockExpr &item, MacroExpander &expander) +expand_tail_expr (AST::BlockExpr &block_expr, MacroExpander &expander) { - if (item.has_tail_expr ()) + if (block_expr.has_tail_expr ()) { - auto tail = item.take_tail_expr (); + auto tail = block_expr.take_tail_expr (); auto attrs = tail->get_outer_attrs (); bool changed = false; for (auto it = attrs.begin (); it != attrs.end ();) @@ -192,16 +192,17 @@ expand_tail_expr (AST::BlockExpr &item, MacroExpander &expander) it = attrs.erase (it); changed = true; auto new_stmts - = expand_stmt_attribute (item, current.get_path (), expander); - auto &stmts = item.get_statements (); + = expand_stmt_attribute (block_expr, current.get_path (), + expander); + auto &stmts = block_expr.get_statements (); std::move (new_stmts.begin (), new_stmts.end (), std::inserter (stmts, stmts.end ())); } } if (changed) - item.strip_tail_expr (); + block_expr.normalize_tail_expr (); else - item.set_tail_expr (std::move (tail)); + block_expr.set_tail_expr (std::move (tail)); } } @@ -286,7 +287,7 @@ ExpandVisitor::expand_inner_items ( void ExpandVisitor::expand_inner_stmts ( - std::vector<std::unique_ptr<AST::Stmt>> &stmts) + AST::BlockExpr &expr, std::vector<std::unique_ptr<AST::Stmt>> &stmts) { expander.push_context (MacroExpander::ContextType::STMT); @@ -362,6 +363,9 @@ ExpandVisitor::expand_inner_stmts ( } } + if (!expr.has_tail_expr ()) + expr.normalize_tail_expr (); + std::function<std::unique_ptr<AST::Stmt> (AST::SingleASTNode)> extractor = [] (AST::SingleASTNode node) { return node.take_stmt (); }; @@ -830,7 +834,7 @@ ExpandVisitor::visit (AST::ClosureExprInner &expr) void ExpandVisitor::visit (AST::BlockExpr &expr) { - expand_inner_stmts (expr.get_statements ()); + expand_inner_stmts (expr, expr.get_statements ()); expand_tail_expr (expr, expander); if (expr.has_tail_expr ()) diff --git a/gcc/rust/expand/rust-expand-visitor.h b/gcc/rust/expand/rust-expand-visitor.h index 630b601..e7e303b 100644 --- a/gcc/rust/expand/rust-expand-visitor.h +++ b/gcc/rust/expand/rust-expand-visitor.h @@ -158,7 +158,8 @@ public: * @param values Vector of values to mutate in-place and append into */ void expand_inner_items (std::vector<std::unique_ptr<AST::Item>> &values); - void expand_inner_stmts (std::vector<std::unique_ptr<AST::Stmt>> &values); + void expand_inner_stmts (AST::BlockExpr &expr, + std::vector<std::unique_ptr<AST::Stmt>> &values); // TODO: See if possible to make more specialization for Impl items, Block // stmts etc? This could allow us to remove expand_macro_children or at least |