]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Emit an error on malformed path
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Wed, 1 Oct 2025 10:36:45 +0000 (12:36 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Thu, 30 Oct 2025 20:30:54 +0000 (21:30 +0100)
Path must be made of a single literal item, otherwise an error should be
thrown.

gcc/rust/ChangeLog:

* util/rust-attributes.cc (AttributeChecker::check_attribute): Recurse
within attr input for additional attribute checking.
(AttributeChecker::visit): Remove empty definition in favor of default
ast visitor definition.
* util/rust-attributes.h: Remove now unused prototypes.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/util/rust-attributes.cc
gcc/rust/util/rust-attributes.h

index bd91fdd5d031b0f52ec25e6e97a4b8c697669c8e..49c4282755cc9ee10e4486557920d3b7717f17c4 100644 (file)
@@ -326,6 +326,20 @@ check_proc_macro_non_root (AST::AttrVec attributes, location_t loc)
 void
 AttributeChecker::check_attribute (const AST::Attribute &attribute)
 {
+  if (!attribute.empty_input ())
+    {
+      const auto &attr_input = attribute.get_attr_input ();
+      auto type = attr_input.get_attr_input_type ();
+      if (type == AST::AttrInput::AttrInputType::TOKEN_TREE)
+       {
+         const auto &option = static_cast<const AST::DelimTokenTree &> (
+           attribute.get_attr_input ());
+         std::unique_ptr<AST::AttrInputMetaItemContainer> meta_item (
+           option.parse_to_meta_item ());
+         AST::DefaultASTVisitor::visit (meta_item);
+       }
+    }
+
   BuiltinAttrDefinition result;
 
   // This checker does not check non-builtin attributes
@@ -354,10 +368,6 @@ void
 AttributeChecker::visit (AST::DelimTokenTree &)
 {}
 
-void
-AttributeChecker::visit (AST::AttrInputMetaItemContainer &)
-{}
-
 void
 AttributeChecker::visit (AST::IdentifierExpr &)
 {}
@@ -421,8 +431,16 @@ AttributeChecker::visit (AST::MetaItemLitExpr &)
 {}
 
 void
-AttributeChecker::visit (AST::MetaItemPathExpr &)
-{}
+AttributeChecker::visit (AST::MetaItemPathExpr &attribute)
+{
+  if (!attribute.get_expr ().is_literal ())
+    {
+      rust_error_at (attribute.get_locus (),
+                    "malformed %<path%> attribute input");
+      rust_inform (attribute.get_locus (),
+                  "must be of the form: %<#[path = \"file\"]%>");
+    }
+}
 
 void
 AttributeChecker::visit (AST::BorrowExpr &)
@@ -648,6 +666,7 @@ AttributeChecker::visit (AST::TypeBoundWhereClauseItem &)
 void
 AttributeChecker::visit (AST::Module &module)
 {
+  check_attributes (module.get_outer_attrs ());
   check_proc_macro_non_function (module.get_outer_attrs ());
   for (auto &item : module.get_items ())
     {
@@ -862,10 +881,6 @@ void
 AttributeChecker::visit (AST::MetaItemPath &)
 {}
 
-void
-AttributeChecker::visit (AST::MetaItemSeq &)
-{}
-
 void
 AttributeChecker::visit (AST::MetaWord &)
 {}
index 22670991beb79c8fd43340bb83ee818c99ff8ba1..0ad3c2b92848b35cfc0dc84bc64aa545cf1dc6ac 100644 (file)
@@ -112,7 +112,6 @@ private:
   void visit (AST::Crate &crate) override;
   void visit (AST::Token &tok) override;
   void visit (AST::DelimTokenTree &delim_tok_tree) override;
-  void visit (AST::AttrInputMetaItemContainer &input) override;
   void visit (AST::IdentifierExpr &ident_expr) override;
   void visit (AST::Lifetime &lifetime) override;
   void visit (AST::LifetimeParam &lifetime_param) override;
@@ -220,7 +219,6 @@ private:
   void visit (AST::MacroRulesDefinition &rules_def) override;
   void visit (AST::MacroInvocation &macro_invoc) override;
   void visit (AST::MetaItemPath &meta_item) override;
-  void visit (AST::MetaItemSeq &meta_item) override;
   void visit (AST::MetaWord &meta_item) override;
   void visit (AST::MetaNameValueStr &meta_item) override;
   void visit (AST::MetaListPaths &meta_item) override;