[gccrs COMMIT 3/3] gccrs: Add new checks for `#[repr(align)]` attribute

gerris.rs@gmail.com gerris.rs@gmail.com
Wed Jul 8 09:16:09 GMT 2026


From: Yap Zhi Heng <yapzhhg@gmail.com>

gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-base.cc (TypeCheckBase::parse_repr_options): New
	check for `#[repr(align)]` to enforce having a parameter that is a power of 2.

gcc/testsuite/ChangeLog:

	* rust/compile/invalid_repr_hint.rs: Update existing and add new `align` test cases.

Signed-Off-By: Yap Zhi Heng <yapzhhg@gmail.com>
---
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/864defecb6c0faf055005b48a24914651ef5c727

The commit has NOT been mentioned in any issue.

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

 gcc/rust/typecheck/rust-hir-type-check-base.cc  | 13 +++++++++++++
 gcc/testsuite/rust/compile/invalid_repr_hint.rs |  5 ++++-
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.cc b/gcc/rust/typecheck/rust-hir-type-check-base.cc
index 7ff37c924..f966f002a 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-base.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-base.cc
@@ -522,6 +522,15 @@ TypeCheckBase::parse_repr_options (const AST::AttrVec &attrs, location_t locus)
 
 	  if (oparen == std::string::npos)
 	    {
+	      if (inline_option.compare ("align") == 0)
+		{
+		  rust_error_at (attr.get_locus (), ErrorCode::E0589,
+				 "invalid %<repr(align)%> attribute: %<align%> "
+				 "needs an argument");
+		  delete meta_items;
+		  break;
+		}
+
 	      is_pack = inline_option.compare ("packed") == 0;
 	      is_c = inline_option.compare ("C") == 0;
 	      is_integer = (inline_option.compare ("isize") == 0
@@ -571,6 +580,10 @@ TypeCheckBase::parse_repr_options (const AST::AttrVec &attrs, location_t locus)
 	    }
 	  else if (is_align)
 	    {
+	      if (value == 0 || (value & (value - 1)) != 0)
+		rust_error_at (
+		  attr.get_locus (), ErrorCode::E0589,
+		  "invalid %<repr(align)%> attribute: not a power of two");
 	      repr.repr_kind = TyTy::ADTType::ReprKind::ALIGN;
 	      repr.align = value;
 	    }
diff --git a/gcc/testsuite/rust/compile/invalid_repr_hint.rs b/gcc/testsuite/rust/compile/invalid_repr_hint.rs
index 7ac48a921..5e1af4d1c 100644
--- a/gcc/testsuite/rust/compile/invalid_repr_hint.rs
+++ b/gcc/testsuite/rust/compile/invalid_repr_hint.rs
@@ -6,7 +6,10 @@ struct Foo {
     x: i32,
 }
 
-#[repr(align)] // { dg-error "unrecognized representation hint" }
+#[repr(align)] // { dg-error "invalid .repr.align.. attribute: .align. needs an argument" }
 struct Bar {
     x: i32,
 }
+
+#[repr(align(3))] // { dg-error "invalid .repr.align.. attribute: not a power of two" }
+struct Baz {}
-- 
2.54.0



More information about the Gcc-rust mailing list