rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
96.84k stars 12.51k forks source link

ZST's layout leaks #114391

Open joshlf opened 1 year ago

joshlf commented 1 year ago

I have the following code:

#[repr(transparent)]
struct Foo(Zst, [u8]);

struct Zst(());

I would expect this code to be rejected - since Zst doesn't have a repr, it isn't guaranteed to be a ZST. As a result, repr(transparent) should consider Foo to have two non-ZST fields, which is illegal. Instead, it compiles successfully.

One practical consequence is that, if I were to publish Zst in a crate, downstream crates could write code whose correctness depends on Zst's layout even though I didn't intend to expose it anywhere.

Rust version: 1.71.0

scottmcm commented 1 year ago

This is a stronger version of #78586

(It's probably worth tracking separately, because this is is likely harder to fix.)