From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id B8C71383CCF3; Wed, 31 Aug 2022 10:41:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B8C71383CCF3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661942480; bh=i3jCG3hlTmnx94KnV9CuNxd6bpuSYo+XnVpw9SBfXQo=; h=From:To:Subject:Date:From; b=k4SS/TVN0nm4MRYOGKqJxei1cPupTHoSCmoWOF4RzH99U+eoRLf+cw2+tuND5wRcO c/2v4/JCxZhameANbRc7oYDSfiHn2WfOh328Dm3nA6Q1ZowZkqj0BFsmI3M4TgunyV mbZOrZlvV0oCFlPLMAsrz/A18DLUtcYvKzjE7dLM= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] ast: Only expand expressions and types if the kind is right X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 2bf4f17d1ff0dbfd5c21776f45094d518f01cd49 X-Git-Newrev: 842829a591c39c24332c3605606f5d89ff89f7d6 Message-Id: <20220831104120.B8C71383CCF3@sourceware.org> Date: Wed, 31 Aug 2022 10:41:20 +0000 (GMT) List-Id: https://gcc.gnu.org/g:842829a591c39c24332c3605606f5d89ff89f7d6 commit 842829a591c39c24332c3605606f5d89ff89f7d6 Author: Arthur Cohen Date: Mon Aug 29 13:37:13 2022 +0200 ast: Only expand expressions and types if the kind is right Diff: --- gcc/rust/ast/rust-ast.h | 15 +++++++++++++++ gcc/rust/expand/rust-attribute-visitor.cc | 11 +++++++---- gcc/rust/expand/rust-macro-expand.cc | 4 +++- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index 461a2460f8f..0b032a6637f 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -24,6 +24,7 @@ #include "rust-hir-map.h" #include "rust-token.h" #include "rust-location.h" +#include "rust-diagnostics.h" namespace Rust { // TODO: remove typedefs and make actual types for these @@ -1868,6 +1869,10 @@ private: */ bool is_single_fragment () const { return nodes.size () == 1; } + bool is_single_fragment (SingleASTNode::NodeType expected) const + { + return is_single_fragment () && nodes[0].get_kind () == expected; + } bool is_single_fragment_kind (SingleASTNode::NodeType kind) const { @@ -1913,6 +1918,16 @@ public: bool should_expand () const { return !is_error (); } + bool is_expression_fragment () const + { + return is_single_fragment (SingleASTNode::NodeType::EXPRESSION); + } + + bool is_type_fragment () const + { + return is_single_fragment (SingleASTNode::NodeType::TYPE); + } + std::unique_ptr take_expression_fragment () { rust_assert (is_single_fragment_kind (SingleASTNode::NodeType::EXPRESSION)); diff --git a/gcc/rust/expand/rust-attribute-visitor.cc b/gcc/rust/expand/rust-attribute-visitor.cc index 8016f9430eb..12b8cec5d09 100644 --- a/gcc/rust/expand/rust-attribute-visitor.cc +++ b/gcc/rust/expand/rust-attribute-visitor.cc @@ -2662,7 +2662,7 @@ AttrVisitor::visit (AST::InherentImpl &impl) for (auto ¶m : impl.get_generic_params ()) param->accept_vis (*this); - expander.push_context (MacroExpander::ContextType::TYPE); + expander.push_context (MacroExpander::ContextType::ITEM); auto &type = impl.get_type (); type->accept_vis (*this); @@ -2706,7 +2706,7 @@ AttrVisitor::visit (AST::TraitImpl &impl) for (auto ¶m : impl.get_generic_params ()) param->accept_vis (*this); - expander.push_context (MacroExpander::ContextType::TYPE); + expander.push_context (MacroExpander::ContextType::ITEM); auto &type = impl.get_type (); type->accept_vis (*this); @@ -3427,11 +3427,13 @@ AttrVisitor::visit (AST::BareFunctionType &type) // no where clause, apparently } + void AttrVisitor::maybe_expand_expr (std::unique_ptr &expr) { auto final_fragment = expand_macro_fragment_recursive (); - if (final_fragment.should_expand ()) + if (final_fragment.should_expand () + && final_fragment.is_expression_fragment ()) expr = final_fragment.take_expression_fragment (); } @@ -3439,7 +3441,8 @@ void AttrVisitor::maybe_expand_type (std::unique_ptr &type) { auto final_fragment = expand_macro_fragment_recursive (); - if (final_fragment.should_expand ()) + if (final_fragment.should_expand () && final_fragment.is_type_fragment ()) type = final_fragment.take_type_fragment (); } + } // namespace Rust diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index eef9194bf7a..dd8d468aad1 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -864,7 +864,9 @@ transcribe_expression (Parser &parser) static AST::ASTFragment transcribe_type (Parser &parser) { - auto type = parser.parse_type (); + auto type = parser.parse_type (true); + for (auto err : parser.get_errors ()) + err.emit_error (); return AST::ASTFragment ({std::move (type)}); }