[gccrs COMMIT] gccrs: Make ExpandVisitor visit more attributes

[email protected] Wed, 5 Aug 2026 11:17:15 +0000
Newsgroups gmane.comp.gcc.rust,gmane.comp.gcc.patches
Message-ID <[email protected]>
From: Owen Avery <[email protected]>

Makes ExpandVisitor rely more on functions provided by PointerVisitor.
More can be done in this area, but this is enough for now to fix an
issue compiling core.

gcc/rust/ChangeLog:

	* expand/rust-expand-visitor.cc
	(ExpandVisitor::expand_struct_fields): Remove function
	definition.
	(ExpandVisitor::expand_tuple_fields): Likewise.
	(ExpandVisitor::visit (Function)): Visit outer attributes, add
	comment about inner attribute visiting.
	(ExpandVisitor::visit (StructStruct)): Remove function
	definition.
	(ExpandVisitor::visit (TupleStruct)): Likewise.
	(ExpandVisitor::visit (EnumItemTuple)): Likewise.
	(ExpandVisitor::visit (EnumItemStruct)): Likewise.
	(ExpandVisitor::visit (Union)): Likewise.
	(ExpandVisitor::visit (ExternalStaticItem)): Likewise.
	* expand/rust-expand-visitor.h
	(ExpandVisitor::visit (StructStruct)): Remove function
	declaration.
	(ExpandVisitor::visit (TupleStruct)): Likewise.
	(ExpandVisitor::visit (EnumItemTuple)): Likewise.
	(ExpandVisitor::visit (EnumItemStruct)): Likewise.
	(ExpandVisitor::visit (Union)): Likewise.
	(ExpandVisitor::visit (ExternalStaticItem)): Likewise.
	(ExpandVisitor::expand_fields): Likewise.

gcc/testsuite/ChangeLog:

	* rust/compile/doc_macro_2.rs: New test.

Signed-off-by: Owen Avery <[email protected]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: https://github.com/Rust-GCC/gccrs/commit/840bac99462208bc849a44fd36bb36cf04f711a5

The commit has NOT been mentioned in any issue.

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4746

 gcc/rust/expand/rust-expand-visitor.cc    | 66 ++---------------------
 gcc/rust/expand/rust-expand-visitor.h     | 16 ------
 gcc/testsuite/rust/compile/doc_macro_2.rs | 24 +++++++++
 3 files changed, 27 insertions(+), 79 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/doc_macro_2.rs

diff --git a/gcc/rust/expand/rust-expand-visitor.cc b/gcc/rust/expand/rust-expand-visitor.cc
index 64a4f7a2d..3a042ac9a 100644
--- a/gcc/rust/expand/rust-expand-visitor.cc
+++ b/gcc/rust/expand/rust-expand-visitor.cc
@@ -431,18 +431,6 @@ ExpandVisitor::maybe_expand_pattern (std::unique_ptr<AST::Pattern> &pattern)
     pattern = final_fragment.take_pattern_fragment ();
 }
 
-void
-ExpandVisitor::expand_struct_fields (std::vector<AST::StructField> &fields)
-{
-  expand_fields (fields);
-}
-
-void
-ExpandVisitor::expand_tuple_fields (std::vector<AST::TupleField> &fields)
-{
-  expand_fields (fields);
-}
-
 // FIXME: This can definitely be refactored with the method above
 void
 ExpandVisitor::expand_function_params (
@@ -742,6 +730,9 @@ ExpandVisitor::visit (AST::UseDeclaration &use_decl)
 void
 ExpandVisitor::visit (AST::Function &function)
 {
+  visit_outer_attrs (function);
+  // TODO: handle body inner attributes more regularly?
+  //       apparently, they should be applied to this function
   if (function.has_body ())
     visit_inner_using_attrs (
       function, function.get_definition ().value ()->get_inner_attrs ());
@@ -760,61 +751,16 @@ ExpandVisitor::visit (AST::Function &function)
     visit (*function.get_definition ());
 }
 
-void
-ExpandVisitor::visit (AST::StructStruct &struct_item)
-{
-  for (auto &generic : struct_item.get_generic_params ())
-    visit (generic);
-
-  if (struct_item.has_where_clause ())
-    expand_where_clause (struct_item.get_where_clause ());
-
-  expand_struct_fields (struct_item.get_fields ());
-}
-
-void
-ExpandVisitor::visit (AST::TupleStruct &tuple_struct)
-{
-  for (auto &generic : tuple_struct.get_generic_params ())
-    visit (generic);
-
-  if (tuple_struct.has_where_clause ())
-    expand_where_clause (tuple_struct.get_where_clause ());
-
-  expand_tuple_fields (tuple_struct.get_fields ());
-}
-
 void
 ExpandVisitor::visit (AST::EnumItem &item)
 {}
 
-void
-ExpandVisitor::visit (AST::EnumItemTuple &item)
-{
-  expand_tuple_fields (item.get_tuple_fields ());
-}
-
-void
-ExpandVisitor::visit (AST::EnumItemStruct &item)
-{
-  expand_struct_fields (item.get_struct_fields ());
-}
-
 void
 ExpandVisitor::visit (AST::EnumItemDiscriminant &item)
 {
   maybe_expand_expr (item.get_expr_ptr ());
 }
 
-void
-ExpandVisitor::visit (AST::Union &union_item)
-{
-  for (auto &generic : union_item.get_generic_params ())
-    visit (generic);
-
-  expand_struct_fields (union_item.get_variants ());
-}
-
 void
 ExpandVisitor::visit (AST::Trait &trait)
 {
@@ -888,12 +834,6 @@ void
 ExpandVisitor::visit (AST::ExternalTypeItem &item)
 {}
 
-void
-ExpandVisitor::visit (AST::ExternalStaticItem &static_item)
-{
-  maybe_expand_type (static_item.get_type_ptr ());
-}
-
 void
 ExpandVisitor::visit (AST::ExternBlock &block)
 {
diff --git a/gcc/rust/expand/rust-expand-visitor.h b/gcc/rust/expand/rust-expand-visitor.h
index 6faf825ae..f387d28a1 100644
--- a/gcc/rust/expand/rust-expand-visitor.h
+++ b/gcc/rust/expand/rust-expand-visitor.h
@@ -257,18 +257,12 @@ public:
   void visit (AST::UseTreeRebind &) override;
   void visit (AST::UseDeclaration &use_decl) override;
   void visit (AST::Function &function) override;
-  void visit (AST::StructStruct &struct_item) override;
-  void visit (AST::TupleStruct &tuple_struct) override;
   void visit (AST::EnumItem &item) override;
-  void visit (AST::EnumItemTuple &item) override;
-  void visit (AST::EnumItemStruct &item) override;
   void visit (AST::EnumItemDiscriminant &item) override;
-  void visit (AST::Union &union_item) override;
   void visit (AST::Trait &trait) override;
   void visit (AST::InherentImpl &impl) override;
   void visit (AST::TraitImpl &impl) override;
   void visit (AST::ExternalTypeItem &item) override;
-  void visit (AST::ExternalStaticItem &item) override;
   void visit (AST::ExternBlock &block) override;
 
   // I don't think it would be possible to strip macros without expansion
@@ -297,16 +291,6 @@ public:
 private:
   MacroExpander &expander;
   NodeId macro_invoc_expect_id;
-
-  /**
-   * Helper to expand all macro invocations in lieu of types within a vector of
-   * fields (StructField or TupleField).
-   */
-  template <typename T> void expand_fields (std::vector<T> &fields)
-  {
-    for (auto &field : fields)
-      maybe_expand_type (field.get_field_type_ptr ());
-  }
 };
 
 } // namespace Rust
diff --git a/gcc/testsuite/rust/compile/doc_macro_2.rs b/gcc/testsuite/rust/compile/doc_macro_2.rs
new file mode 100644
index 000000000..82101ec61
--- /dev/null
+++ b/gcc/testsuite/rust/compile/doc_macro_2.rs
@@ -0,0 +1,24 @@
+// { dg-additional-options "-w" }
+#![feature(no_core)]
+#![no_core]
+
+macro_rules! foo {
+    ($e:expr, $($t:tt)*) => {
+        #[doc = $e]
+        $($t)*
+    }
+}
+
+macro_rules! bar {
+    () => { "bar" }
+}
+
+struct S;
+
+trait T {
+    fn f();
+}
+
+impl T for S {
+    foo!(bar!(), fn f() {});
+}

base-commit: 5020193c98fe7c4540773f9477eadab3daac406e
-- 
2.54.0