]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: make invalid inner attributes show error
authorLucas Ly Ba <lucas.ly-ba@outlook.com>
Mon, 3 Nov 2025 16:28:56 +0000 (16:28 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Mon, 17 Nov 2025 14:58:19 +0000 (15:58 +0100)
gcc/rust/ChangeLog:

* ast/rust-ast.cc (Attribute::is_derive):
Change is_derive method with its valid path.
* util/rust-attribute-values.h:
Delete redudant derive attribute.
* util/rust-attributes.cc (AttributeChecker::check_inner_attribute):
Helper method for check_inner_attributes
(AttributeChecker::check_inner_attributes):
Implement method for errors check.
* util/rust-attributes.h:
Add methods above in header.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Lucas Ly Ba <lucas.ly-ba@outlook.com>
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
gcc/testsuite/rust/compile/issue-4219.rs [new file with mode: 0644]

index 6d37ef7e22fb8ab3d60955cd2d27111f8dc2dd7a..f3ad2fe5da05fa4d588914c1a3e361bb579b6e6b 100644 (file)
@@ -248,7 +248,7 @@ Attribute::as_string () const
 bool
 Attribute::is_derive () const
 {
-  return has_attr_input () && get_path () == Values::Attributes::DERIVE;
+  return has_attr_input () && get_path () == Values::Attributes::DERIVE_ATTR;
 }
 
 /**
index a22664a1c48ff700ccfe32d4f8e7b799c0a42e51..0f35f56f798e6e01a350cedb97dde70f47050b7b 100644 (file)
@@ -49,8 +49,6 @@ 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 9621100cc9524d0f67a060887b908bb727590e7e..70f26e7ce361ba20b1e93253b137762da840bc63 100644 (file)
@@ -90,8 +90,6 @@ 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},
@@ -101,7 +99,6 @@ static const BuiltinAttrDefinition __definitions[]
      {Attrs::RUSTC_INHERIT_OVERFLOW_CHECKS, CODE_GENERATION},
      {Attrs::STABLE, STATIC_ANALYSIS},
      {Attrs::UNSTABLE, STATIC_ANALYSIS},
-
      // assuming we keep these for static analysis
      {Attrs::RUSTC_PROMOTABLE, CODE_GENERATION},
      {Attrs::RUSTC_CONST_STABLE, STATIC_ANALYSIS},
@@ -114,23 +111,22 @@ static const BuiltinAttrDefinition __definitions[]
      {Attrs::RUSTC_RESERVATION_IMPL, TYPE_CHECK},
      {Attrs::RUSTC_PAREN_SUGAR, TYPE_CHECK},
      {Attrs::RUSTC_NONNULL_OPTIMIZATION_GUARANTEED, TYPE_CHECK},
-
      {Attrs::RUSTC_LAYOUT_SCALAR_VALID_RANGE_START, CODE_GENERATION},
-
      // TODO: be careful about calling functions marked with this?
      {Attrs::RUSTC_ARGS_REQUIRED_CONST, CODE_GENERATION},
-
      {Attrs::PRELUDE_IMPORT, NAME_RESOLUTION},
-
      {Attrs::RUSTC_DIAGNOSTIC_ITEM, STATIC_ANALYSIS},
      {Attrs::RUSTC_ON_UNIMPLEMENTED, STATIC_ANALYSIS},
-
      {Attrs::FUNDAMENTAL, TYPE_CHECK},
      {Attrs::NON_EXHAUSTIVE, TYPE_CHECK},
      {Attrs::RUSTFMT, EXTERNAL},
-
      {Attrs::TEST, CODE_GENERATION}};
 
+static const std::set<std::string> __outer_attributes
+  = {Attrs::INLINE,        Attrs::DERIVE_ATTR, Attrs::ALLOW_INTERNAL_UNSTABLE,
+     Attrs::LANG,          Attrs::REPR,        Attrs::PATH,
+     Attrs::TARGET_FEATURE, Attrs::TEST};
+
 BuiltinAttributeMappings *
 BuiltinAttributeMappings::get ()
 {
@@ -326,6 +322,26 @@ check_proc_macro_non_root (AST::AttrVec attributes, location_t loc)
     }
 }
 
+void
+AttributeChecker::check_inner_attribute (const AST::Attribute &attribute)
+{
+  BuiltinAttrDefinition result;
+
+  if (!is_builtin (attribute, result))
+    return;
+
+  if (__outer_attributes.find (result.name) != __outer_attributes.end ())
+    rust_error_at (attribute.get_locus (),
+                  "attribute cannot be used at crate level");
+}
+
+void
+AttributeChecker::check_inner_attributes (const AST::AttrVec &attributes)
+{
+  for (auto &attr : attributes)
+    check_inner_attribute (attr);
+}
+
 void
 AttributeChecker::check_attribute (const AST::Attribute &attribute)
 {
@@ -356,15 +372,6 @@ 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 b10a0806530bb4c49754405563aeb7599cf3d103..f4a2d389c3d278c384378eafff016d7f33ca9049 100644 (file)
@@ -102,12 +102,14 @@ public:
 
 private:
   using AST::DefaultASTVisitor::visit;
+
+  /* Check the validity of an inner attribute */
+  void check_inner_attribute (const AST::Attribute &attribute);
+  /* Check the validy of all inner attributes */
+  void check_inner_attributes (const AST::AttrVec &attributes);
   /* Check the validity of a given attribute */
   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
index e068e458c24fced0ee975debcdafa4f0d474f42c..e7bf113d7aad8537c6f4888502f7a8e96b820f87 100644 (file)
@@ -1,5 +1,5 @@
 #![derive(PartialOrd, PartialEq)]
-// { dg-error "derive attribute cannot be used at crate level" "" { target *-*-* } .-1 }
+// { dg-error "attribute cannot be used at crate level" "" { target *-*-* } .-1 }
 pub fn check_ge(a: i32, b: i32) -> bool {
     a >= b
 }
diff --git a/gcc/testsuite/rust/compile/issue-4219.rs b/gcc/testsuite/rust/compile/issue-4219.rs
new file mode 100644 (file)
index 0000000..d6e70e9
--- /dev/null
@@ -0,0 +1,5 @@
+#![inline]
+// { dg-error "attribute cannot be used at crate level" "" { target *-*-* } .-1 }
+pub fn check_ge(a: i32, b: i32) -> bool {
+    a >= b
+}