https://gcc.gnu.org/g:129fa6e42b69bea4b9c6cf952dcc64b4c1670afb
commit r16-4769-g129fa6e42b69bea4b9c6cf952dcc64b4c1670afb Author: Owen Avery <[email protected]> Date: Fri Jul 11 23:35:53 2025 -0400 gccrs: Handle attributes in expression macros gcc/rust/ChangeLog: * expand/rust-macro-expand.cc (transcribe_expression): Parse any outer attributes before parsing an expression. * parse/rust-parse.h (Parser::parse_outer_attributes): Make public. gcc/testsuite/ChangeLog: * rust/compile/attr-macro.rs: New test. Signed-off-by: Owen Avery <[email protected]> Diff: --- gcc/rust/expand/rust-macro-expand.cc | 3 ++- gcc/rust/parse/rust-parse.h | 6 +++++- gcc/testsuite/rust/compile/attr-macro.rs | 7 +++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index 5667a1a4f4f8..dfead3acc1d6 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -961,7 +961,8 @@ transcribe_expression (Parser<MacroInvocLexer> &parser) auto &lexer = parser.get_token_source (); auto start = lexer.get_offs (); - auto expr = parser.parse_expr (); + auto attrs = parser.parse_outer_attributes (); + auto expr = parser.parse_expr (std::move (attrs)); if (expr == nullptr) return AST::Fragment::create_error (); diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h index fa518ac5544e..fa9c909338bb 100644 --- a/gcc/rust/parse/rust-parse.h +++ b/gcc/rust/parse/rust-parse.h @@ -212,6 +212,11 @@ public: std::unique_ptr<AST::MacroInvocation> parse_macro_invocation (AST::AttrVec outer_attrs); + /* + * This has to be public for parsing expressions with outer attributes + */ + AST::AttrVec parse_outer_attributes (); + private: void skip_after_semicolon (); void skip_after_end (); @@ -228,7 +233,6 @@ private: // AST-related stuff - maybe move or something? AST::Attribute parse_inner_attribute (); - AST::AttrVec parse_outer_attributes (); AST::Attribute parse_outer_attribute (); std::unique_ptr<AST::AttrInput> parse_attr_input (); std::tuple<AST::SimplePath, std::unique_ptr<AST::AttrInput>, location_t> diff --git a/gcc/testsuite/rust/compile/attr-macro.rs b/gcc/testsuite/rust/compile/attr-macro.rs new file mode 100644 index 000000000000..de9fce12cb97 --- /dev/null +++ b/gcc/testsuite/rust/compile/attr-macro.rs @@ -0,0 +1,7 @@ +macro_rules! foo { + () => { #[cfg(all())] 12 } +} + +fn main() -> i32 { + foo!() +}
