From: Yap Zhi Heng <[email protected]>
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]>
---
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 7ff37c92434..f966f002aa2 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 7ac48a921d4..5e1af4d1c53 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.50.1