]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: util/attributes: Error on #[repr] applied to functions
authorJayant Chauhan <0001jayant@gmail.com>
Tue, 13 Jan 2026 08:10:47 +0000 (13:40 +0530)
committerArthur Cohen <arthur.cohen@embecosm.com>
Fri, 27 Feb 2026 14:57:04 +0000 (15:57 +0100)
The #[repr] attribute is only valid for structs, enums, and unions.
Applying it to a function is invalid and should result in an error.
This patch adds a check in the attribute visitor to reject #[repr]
on functions, matching rustc behavior.

Fixes Rust-GCC#4232

gcc/rust/ChangeLog:

* util/rust-attributes.cc (AttributeChecker::check_attributes): Emit error for #[repr].

gcc/testsuite/ChangeLog:

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

Signed-off-by: Jayant Chauhan <0001jayant@gmail.com>
gcc/rust/util/rust-attributes.cc
gcc/testsuite/rust/compile/issue-4232.rs [new file with mode: 0644]

index 0e5234687602f855d40cb05340fc79598319a9e7..bf7f3d9827d6c9081ae47338103f70e492f58d44 100644 (file)
@@ -912,6 +912,12 @@ AttributeChecker::visit (AST::Function &fun)
        {
          check_link_section_attribute (attribute);
        }
+      else if (result.name == Attrs::REPR)
+       {
+         rust_error_at (
+           attribute.get_locus (),
+           "attribute should be applied to a struct, enum, or union");
+       }
     }
 
   if (fun.has_body ())
diff --git a/gcc/testsuite/rust/compile/issue-4232.rs b/gcc/testsuite/rust/compile/issue-4232.rs
new file mode 100644 (file)
index 0000000..fa12538
--- /dev/null
@@ -0,0 +1,3 @@
+// { dg-options "-w" }
+#[repr(C)] // { dg-error "attribute should be applied to a struct, enum, or union" }
+fn a() {}
\ No newline at end of file