From: Pierre-Emmanuel Patry <[email protected]>

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 <[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/002bacdecac0473bd10925efbf1fdf8195b99c3e

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

 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 16786e32e..d03793b42 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 d6e48e075..6faf825ae 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.53.0

Reply via email to