[COMMITTED 24/43] gccrs: Flatten AttrInputExpr literals to AttrInputLiteral

arthur.cohen@embecosm.com arthur.cohen@embecosm.com
Tue Apr 14 23:18:45 GMT 2026


From: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

Some macros may be expanded to literals within attribute input. This
this means they could be converted to AttrInputLiteral and properly
differentiate both expr and literal in the builtin attribute checker to
emit an error message when a real expr remains within an attribute.

gcc/rust/ChangeLog:

	* expand/rust-expand-visitor.cc (ExpandVisitor::visit): Flatten
	AttrInputExpr containing a literal to an AttrInputLiteral.
	* expand/rust-expand-visitor.h: Add function prototype.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
---
 gcc/rust/expand/rust-expand-visitor.cc | 18 ++++++++++++++++++
 gcc/rust/expand/rust-expand-visitor.h  |  2 ++
 2 files changed, 20 insertions(+)

diff --git a/gcc/rust/expand/rust-expand-visitor.cc b/gcc/rust/expand/rust-expand-visitor.cc
index 16786e32e15..d03793b4287 100644
--- a/gcc/rust/expand/rust-expand-visitor.cc
+++ b/gcc/rust/expand/rust-expand-visitor.cc
@@ -335,6 +335,24 @@ ExpandVisitor::expand_inner_stmts (AST::BlockExpr &expr)
   expander.pop_context ();
 }
 
+void
+ExpandVisitor::visit (AST::Attribute &attr)
+{
+  // An attribute input containing a macro may have been expanded to a literal
+  if (attr.has_attr_input ()
+      && attr.get_attr_input ().get_attr_input_type ()
+	   == AST::AttrInput::AttrInputType::EXPR)
+    {
+      auto &expr = static_cast<AST::AttrInputExpr &> (attr.get_attr_input ());
+      if (expr.get_expr ().is_literal ())
+	{
+	  auto &lit = static_cast<AST::LiteralExpr &> (expr.get_expr ());
+	  attr.set_attr_input (std::make_unique<AST::AttrInputLiteral> (lit));
+	}
+    }
+  AST::DefaultASTVisitor::visit (attr);
+}
+
 void
 ExpandVisitor::maybe_expand_expr (std::unique_ptr<AST::Expr> &expr)
 {
diff --git a/gcc/rust/expand/rust-expand-visitor.h b/gcc/rust/expand/rust-expand-visitor.h
index d6e48e0757f..6faf825ae3e 100644
--- a/gcc/rust/expand/rust-expand-visitor.h
+++ b/gcc/rust/expand/rust-expand-visitor.h
@@ -221,6 +221,8 @@ public:
   void visit (AST::LifetimeParam &) override;
   void visit (AST::ConstGenericParam &) override;
 
+  void visit (AST::Attribute &attribute) override;
+
   void visit (AST::MacroInvocation &macro_invoc) override;
 
   void visit (AST::PathInExpression &path) override;
-- 
2.50.1



More information about the Gcc-rust mailing list