https://gcc.gnu.org/g:5f8db9c25f054be5107d4adf679669d1d3cd5c19
commit r17-3087-g5f8db9c25f054be5107d4adf679669d1d3cd5c19 Author: Yap Zhi Heng <[email protected]> Date: Mon Jul 6 20:12:50 2026 +0800 gccrs: Add new checks for `#[repr(align)]` attribute 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 <[email protected]> Diff: --- 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 7ff37c92434c..f966f002aa2b 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 7ac48a921d4d..5e1af4d1c530 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 {}
