Skip to content

#[repr(transparent)] initial implementation#4650

Open
Polygonalr wants to merge 2 commits into
Rust-GCC:masterfrom
Polygonalr:repr-transparent
Open

#[repr(transparent)] initial implementation#4650
Polygonalr wants to merge 2 commits into
Rust-GCC:masterfrom
Polygonalr:repr-transparent

Conversation

@Polygonalr

@Polygonalr Polygonalr commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

This is needed for for #4565, because CStr is implemented with #[repr(transparent)] attribute. #4565 got merged, this PR will also include a fix for a mistake I made there.

Note 1: Type-checking doesn't support extra zero-sized members yet, e.g. Unique struct in core/src/ptr/unique.rs:

#[repr(transparent)]
pub struct Unique<T: ?Sized> {
    pointer: *const T,
    _marker: PhantomData<T>, // zero-sized member!
}

I can open an issue for this after this gets merged.

Note 2: I need someone who's more experienced in Rust to confirm the behavior of transparent structs with 0 fields, since 0-field transparent structs are present in both core and kernel. Added a comment regarding in my commit that for now.

gcc/rust/ChangeLog:

	* typecheck/rust-tyty.h (TyTy::ADTType::ReprKind): Add TRANSPARENT variant.
	* typecheck/rust-hir-type-check-base.cc (TypeCheckBase::parse_repr_options):
	Add parsing of transparent repr, throw error for invalid repr.
	* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit (StructStruct)):
	Throw error if #[repr(transparent)] struct/enum has more than 1 field.

Signed-Off-By: Yap Zhi Heng <yapzhhg@gmail.com>
gcc/rust/ChangeLog:

	* backend/rust-compile-expr.h (CompileExpr::compile_transparent_field_access): New
	helper function.
	* backend/rust-compile-expr.cc (CompileExpr::compile_transparent_field_access):
	Implement helper function for accessing the field of #[repr(transparent)] ADTs.
	(CompileExpr::visit (HIR::StructExprStructFields)): Support struct field construction
	properly for #[repr(transparent)] structs.
	(CompileExpr::visit (HIR::FieldAccessExpr)): Implement proper compilation of field
	access for #[repr(transparent)] ADTs.
	* backend/rust-compile-type.cc (TyTyResolveCompile::visit (TyTy::ADTType)): Implement
	proper compilation of typing for #[repr(transparent)] ADTs.

Signed-Off-By: Yap Zhi Heng <yapzhhg@gmail.com>
@powerboat9

Copy link
Copy Markdown
Collaborator

rust-lang/rust#77841
rust-lang/rust#115334

Seems like at most one field can be non-1-ZST (zero sized type with alignment 1). That non-1-ZST type can itself be a ZST. If there are no non-1-ZST types, it acts like () ABI wise.

rust-lang/rust#155299

Also, it looks like much more recent versions of rustc warn/error when the user tries to rely on certain types being 1-ZSTs. I'd figure that could be left for later though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants