From: Jayant Chauhan <[email protected]>

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 <[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/a23b62111b4c6ec25d36dd2b179e6c3fc3c86aa9

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

 gcc/rust/util/rust-attributes.cc         | 6 ++++++
 gcc/testsuite/rust/compile/issue-4232.rs | 3 +++
 2 files changed, 9 insertions(+)
 create mode 100644 gcc/testsuite/rust/compile/issue-4232.rs

diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc
index 0e5234687..bf7f3d982 100644
--- a/gcc/rust/util/rust-attributes.cc
+++ b/gcc/rust/util/rust-attributes.cc
@@ -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
index 000000000..fa1253880
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-4232.rs
@@ -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

base-commit: 3d11639f2af90367cd9749ade7ad6e23ff8ce124
-- 
2.52.0

Reply via email to