From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 451153852744; Wed, 8 Jun 2022 12:21:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 451153852744 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] macros: Allow macro calls in trait implementations X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 935b561e7fb6471773e2a7e860011b76702cd563 X-Git-Newrev: a7ef6f98be0e25187ad1690428aafc17e19b5751 Message-Id: <20220608122102.451153852744@sourceware.org> Date: Wed, 8 Jun 2022 12:21:02 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jun 2022 12:21:02 -0000 https://gcc.gnu.org/g:a7ef6f98be0e25187ad1690428aafc17e19b5751 commit a7ef6f98be0e25187ad1690428aafc17e19b5751 Author: Arthur Cohen Date: Wed Mar 16 16:57:17 2022 +0100 macros: Allow macro calls in trait implementations Just like inherent implementation blocks, trait implementation blocks (`impl Trait for Type`) can also contain macro invocations. Diff: --- gcc/rust/ast/rust-ast.h | 28 ++++++++++++++++++++++++++ gcc/rust/expand/rust-attribute-visitor.cc | 8 ++++++-- gcc/rust/expand/rust-macro-expand.cc | 19 +++++++++++++++++ gcc/rust/expand/rust-macro-expand.h | 3 ++- gcc/rust/parse/rust-parse.h | 2 +- gcc/testsuite/rust/execute/torture/macros23.rs | 19 +++++++++++++++++ 6 files changed, 75 insertions(+), 4 deletions(-) diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index e33fff2d988..a22c2d1ad1d 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -1514,6 +1514,7 @@ public: EXTERN, TRAIT, IMPL, + TRAIT_IMPL, }; private: @@ -1526,6 +1527,7 @@ private: std::unique_ptr external_item; std::unique_ptr trait_item; std::unique_ptr impl_item; + std::unique_ptr trait_impl_item; public: SingleASTNode (std::unique_ptr expr) @@ -1552,6 +1554,10 @@ public: : kind (IMPL), impl_item (std::move (item)) {} + SingleASTNode (std::unique_ptr trait_impl_item) + : kind (TRAIT_IMPL), trait_impl_item (std::move (trait_impl_item)) + {} + SingleASTNode (SingleASTNode const &other) { kind = other.kind; @@ -1580,6 +1586,10 @@ public: case IMPL: impl_item = other.impl_item->clone_inherent_impl_item (); break; + + case TRAIT_IMPL: + trait_impl_item = other.trait_impl_item->clone_trait_impl_item (); + break; } } @@ -1611,6 +1621,10 @@ public: case IMPL: impl_item = other.impl_item->clone_inherent_impl_item (); break; + + case TRAIT_IMPL: + trait_impl_item = other.trait_impl_item->clone_trait_impl_item (); + break; } return *this; } @@ -1679,6 +1693,12 @@ public: return std::move (impl_item); } + std::unique_ptr take_trait_impl_item () + { + rust_assert (!is_error ()); + return std::move (trait_impl_item); + } + void accept_vis (ASTVisitor &vis) { switch (kind) @@ -1706,6 +1726,10 @@ public: case IMPL: impl_item->accept_vis (vis); break; + + case TRAIT_IMPL: + trait_impl_item->accept_vis (vis); + break; } } @@ -1725,6 +1749,8 @@ public: return trait_item == nullptr; case IMPL: return impl_item == nullptr; + case TRAIT_IMPL: + return trait_impl_item == nullptr; } gcc_unreachable (); @@ -1747,6 +1773,8 @@ public: return "Trait Item: " + trait_item->as_string (); case IMPL: return "Impl Item: " + impl_item->as_string (); + case TRAIT_IMPL: + return "Trait Impl Item: " + impl_item->as_string (); } gcc_unreachable (); diff --git a/gcc/rust/expand/rust-attribute-visitor.cc b/gcc/rust/expand/rust-attribute-visitor.cc index 50821edcfae..3de660897ca 100644 --- a/gcc/rust/expand/rust-attribute-visitor.cc +++ b/gcc/rust/expand/rust-attribute-visitor.cc @@ -2549,8 +2549,12 @@ AttrVisitor::visit (AST::TraitImpl &impl) if (impl.has_where_clause ()) expand_where_clause (impl.get_where_clause ()); - // strip trait impl items if required - expand_pointer_allow_strip (impl.get_impl_items ()); + std::function (AST::SingleASTNode)> + extractor + = [] (AST::SingleASTNode node) { return node.take_trait_impl_item (); }; + + expand_macro_children (MacroExpander::TRAIT_IMPL, impl.get_impl_items (), + extractor); } void AttrVisitor::visit (AST::ExternalStaticItem &item) diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index 80f822b3e53..3bdb8c685e6 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -859,6 +859,22 @@ transcribe_many_impl_items (Parser &parser, TokenId &delimiter) }); } +/** + * Transcribe 0 or more trait impl items from a macro invocation + * + * @param parser Parser to extract items from + * @param delimiter Id of the token on which parsing should stop + */ +static std::vector +transcribe_many_trait_impl_items (Parser &parser, + TokenId &delimiter) +{ + return parse_many (parser, delimiter, [&parser] () { + auto item = parser.parse_trait_impl_item (); + return AST::SingleASTNode (std::move (item)); + }); +} + /** * Transcribe 0 or more statements from a macro invocation * @@ -932,6 +948,9 @@ transcribe_context (MacroExpander::ContextType ctx, case MacroExpander::ContextType::IMPL: return transcribe_many_impl_items (parser, last_token_id); break; + case MacroExpander::ContextType::TRAIT_IMPL: + return transcribe_many_trait_impl_items (parser, last_token_id); + break; case MacroExpander::ContextType::EXTERN: return transcribe_many_ext (parser, last_token_id); break; diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h index 61b69e412a4..f08525fd4e8 100644 --- a/gcc/rust/expand/rust-macro-expand.h +++ b/gcc/rust/expand/rust-macro-expand.h @@ -187,9 +187,10 @@ struct MacroExpander { ITEM, BLOCK, + EXTERN, TRAIT, IMPL, - EXTERN, + TRAIT_IMPL, }; ExpansionCfg cfg; diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h index bb5bf3d7072..588061629e9 100644 --- a/gcc/rust/parse/rust-parse.h +++ b/gcc/rust/parse/rust-parse.h @@ -138,6 +138,7 @@ public: std::unique_ptr parse_external_item (); std::unique_ptr parse_trait_item (); std::unique_ptr parse_inherent_impl_item (); + std::unique_ptr parse_trait_impl_item (); AST::PathInExpression parse_path_in_expression (); std::vector > parse_lifetime_params (); AST::Visibility parse_visibility (); @@ -298,7 +299,6 @@ private: std::unique_ptr parse_inherent_impl_function_or_method (AST::Visibility vis, AST::AttrVec outer_attrs); - std::unique_ptr parse_trait_impl_item (); std::unique_ptr parse_trait_impl_function_or_method (AST::Visibility vis, AST::AttrVec outer_attrs); diff --git a/gcc/testsuite/rust/execute/torture/macros23.rs b/gcc/testsuite/rust/execute/torture/macros23.rs new file mode 100644 index 00000000000..846352d0487 --- /dev/null +++ b/gcc/testsuite/rust/execute/torture/macros23.rs @@ -0,0 +1,19 @@ +trait Valuable { + const VALUE: i32; +} + +struct Something; + +macro_rules! implement { + () => { + const VALUE: i32 = 18; + }; +} + +impl Valuable for Something { + implement!(); +} + +fn main() -> i32 { + Something::VALUE - 18 +}