[COMMITTED 56/77] gccrs: Small fixes for #[repr(transparent)]

arthur.cohen@embecosm.com arthur.cohen@embecosm.com
Fri Aug 7 12:39:47 GMT 2026


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

gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit(StructStruct)): Update
	E0690 error message to use the correct variable for number of fields.
	* typecheck/rust-tyty.cc (ArrayType::is_zero_sized): Catch edge case of array's element
	type being a ZST with capacity is more than 0.

gcc/testsuite/ChangeLog:

	* rust/compile/repr_transparent_fields.rs: Add more test cases.

Signed-off-by: Yap Zhi Heng <yapzhhg@gmail.com>
---
 gcc/rust/typecheck/rust-hir-type-check-item.cc       |  2 +-
 gcc/rust/typecheck/rust-tyty.cc                      |  3 +++
 .../rust/compile/repr_transparent_fields.rs          | 12 ++++++++++++
 3 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/gcc/rust/typecheck/rust-hir-type-check-item.cc b/gcc/rust/typecheck/rust-hir-type-check-item.cc
index 6c1d9113291..44d5434b126 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-item.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-item.cc
@@ -378,7 +378,7 @@ TypeCheckItem::visit (HIR::StructStruct &struct_decl)
 	  rust_error_at (struct_decl.get_locus (), ErrorCode::E0690,
 			 "transparent struct needs at most one field with "
 			 "non-trivial size or alignment, but has %lu",
-			 (unsigned long) struct_decl.get_fields ().size ());
+			 (unsigned long) num_non_zst);
 	  return;
 	}
     }
diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc
index 825febe6f6e..bc26692058a 100644
--- a/gcc/rust/typecheck/rust-tyty.cc
+++ b/gcc/rust/typecheck/rust-tyty.cc
@@ -2602,6 +2602,9 @@ ArrayType::is_equal (const BaseType &other) const
 bool
 ArrayType::is_zero_sized () const
 {
+  if (element_type.get_tyty ()->is_zero_sized ())
+    return true;
+
   auto *capacity_ty = get_capacity ();
   if (capacity_ty != nullptr
       && capacity_ty->get_kind () == TyTy::TypeKind::CONST)
diff --git a/gcc/testsuite/rust/compile/repr_transparent_fields.rs b/gcc/testsuite/rust/compile/repr_transparent_fields.rs
index 72219e48674..48246006131 100644
--- a/gcc/testsuite/rust/compile/repr_transparent_fields.rs
+++ b/gcc/testsuite/rust/compile/repr_transparent_fields.rs
@@ -26,3 +26,15 @@ struct Qux {
     foo: i32,
     phantom: PhantomData<i32>,
 }
+
+#[repr(transparent)]
+struct Quux {
+    phantom_array: [(); 6],
+    foo: i32,
+}
+
+#[repr(transparent)]
+struct Corge {
+    empty_array: [i32; 0],
+    foo: i32,
+}
-- 
2.50.1



More information about the Gcc-rust mailing list