From: Owen Avery Date: Sat, 12 Jul 2025 03:35:53 +0000 (-0400) Subject: gccrs: Handle attributes in expression macros X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=129fa6e42b69bea4b9c6cf952dcc64b4c1670afb;p=thirdparty%2Fgcc.git 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 --- diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index 5667a1a4f4f..dfead3acc1d 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -961,7 +961,8 @@ transcribe_expression (Parser &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 fa518ac5544..fa9c909338b 100644 --- a/gcc/rust/parse/rust-parse.h +++ b/gcc/rust/parse/rust-parse.h @@ -212,6 +212,11 @@ public: std::unique_ptr 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 parse_attr_input (); std::tuple, 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 00000000000..de9fce12cb9 --- /dev/null +++ b/gcc/testsuite/rust/compile/attr-macro.rs @@ -0,0 +1,7 @@ +macro_rules! foo { + () => { #[cfg(all())] 12 } +} + +fn main() -> i32 { + foo!() +}