]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: fix inner attr derive doesn't show error
authorLucas Ly Ba <lucas.ly-ba@outlook.fr>
Wed, 15 Oct 2025 15:28:36 +0000 (15:28 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Thu, 30 Oct 2025 20:30:56 +0000 (21:30 +0100)
gcc/rust/ChangeLog:

* ast/rust-ast.cc (Attribute::is_derive):
Change string derive to its definition.
* util/rust-attribute-values.h:
Add attribute definition derive.
* util/rust-attributes.cc (AttributeChecker::visit):
Add method check_inner_attributes.
(AttributeChecker::check_inner_attributes):
Check if there is a bad inner attribute.
* util/rust-attributes.h:
Add method check_inner_attributes in .h.

gcc/testsuite/ChangeLog:

* rust/compile/issue-4212.rs: New test.

Signed-off-by: Lucas Ly Ba <lucas.ly-ba@outlook.fr>
gcc/rust/ast/rust-ast.cc
gcc/rust/util/rust-attribute-values.h
gcc/rust/util/rust-attributes.cc
gcc/rust/util/rust-attributes.h
gcc/testsuite/rust/compile/issue-4212.rs [new file with mode: 0644]

index 4e81de85be42a57c62887c229290e36204cb62ca..337a338f9a3902b336e6dc180bd8af928ebfe518 100644 (file)
@@ -248,7 +248,7 @@ Attribute::as_string () const
 bool
 Attribute::is_derive () const
 {
-  return has_attr_input () && get_path () == "derive";
+  return has_attr_input () && get_path () == Values::Attributes::DERIVE;
 }
 
 /**
index 0f35f56f798e6e01a350cedb97dde70f47050b7b..a22664a1c48ff700ccfe32d4f8e7b799c0a42e51 100644 (file)
@@ -49,6 +49,8 @@ public:
   static constexpr auto &PROC_MACRO_DERIVE = "proc_macro_derive";
   static constexpr auto &PROC_MACRO_ATTRIBUTE = "proc_macro_attribute";
 
+  static constexpr auto &DERIVE = "derive";
+
   static constexpr auto &TARGET_FEATURE = "target_feature";
   // From now on, these are reserved by the compiler and gated through
   // #![feature(rustc_attrs)]
index e4d787794d4b20115dd8aa3fdfc15fb07a9114b5..9621100cc9524d0f67a060887b908bb727590e7e 100644 (file)
@@ -90,6 +90,8 @@ static const BuiltinAttrDefinition __definitions[]
      {Attrs::PROC_MACRO, EXPANSION},
      {Attrs::PROC_MACRO_DERIVE, EXPANSION},
      {Attrs::PROC_MACRO_ATTRIBUTE, EXPANSION},
+
+     {Attrs::DERIVE, EXPANSION},
      // FIXME: This is not implemented yet, see
      // https://github.com/Rust-GCC/gccrs/issues/1475
      {Attrs::TARGET_FEATURE, CODE_GENERATION},
@@ -170,6 +172,7 @@ AttributeChecker::go (AST::Crate &crate)
 void
 AttributeChecker::visit (AST::Crate &crate)
 {
+  check_inner_attributes (crate.get_inner_attrs ());
   check_attributes (crate.get_inner_attrs ());
 
   for (auto &item : crate.items)
@@ -353,6 +356,15 @@ AttributeChecker::check_attribute (const AST::Attribute &attribute)
     check_doc_attribute (attribute);
 }
 
+void
+AttributeChecker::check_inner_attributes (const AST::AttrVec &attributes)
+{
+  for (auto &attr : attributes)
+    if (attr.is_derive ())
+      rust_error_at (attr.get_locus (),
+                    "derive attribute cannot be used at crate level");
+}
+
 void
 AttributeChecker::check_attributes (const AST::AttrVec &attributes)
 {
index 0ad3c2b92848b35cfc0dc84bc64aa545cf1dc6ac..b10a0806530bb4c49754405563aeb7599cf3d103 100644 (file)
@@ -106,6 +106,8 @@ private:
   void check_attribute (const AST::Attribute &attribute);
 
   /* Check the validity of all given attributes */
+
+  void check_inner_attributes (const AST::AttrVec &attributes);
   void check_attributes (const AST::AttrVec &attributes);
 
   // rust-ast.h
diff --git a/gcc/testsuite/rust/compile/issue-4212.rs b/gcc/testsuite/rust/compile/issue-4212.rs
new file mode 100644 (file)
index 0000000..e068e45
--- /dev/null
@@ -0,0 +1,5 @@
+#![derive(PartialOrd, PartialEq)]
+// { dg-error "derive attribute cannot be used at crate level" "" { target *-*-* } .-1 }
+pub fn check_ge(a: i32, b: i32) -> bool {
+    a >= b
+}