reference icon indicating copy to clipboard operation
reference copied to clipboard

Clarify that #[repr(transparent)] is valid without a non-ZST field

Open kupiakos opened this issue 3 years ago • 2 comments

The following is valid, but the prior language suggests it is not:

#[repr(transparent)]
enum Banana {
    Phone,
}

#[repr(transparent)]
struct Blooey(());

#[repr(transparent)]
struct Orange;

Of note: it's still invalid to have a zero-sized field in a transparent struct that has an alignment requirement higher than 1:

#[repr(align(2))]
struct Inner;

// error[E0691]: zero-sized field in transparent struct has alignment
//               larger than 1
#[repr(transparent)]
struct Fizz(Inner);

kupiakos avatar Oct 13 '22 18:10 kupiakos

An extension on that align-2 weirdness: it's valid to express in generic code, but not concretely. Rustc bug?

// This is just fine, because it's generic?
#[repr(transparent)]
struct Fizz<T = Inner>(T, [(); 10]);

kupiakos avatar Oct 18 '22 18:10 kupiakos

Looks like https://github.com/rust-lang/rust/issues/100954 is already tracking the inconsistencies with #[repr(transparent)]

kupiakos avatar Oct 18 '22 18:10 kupiakos