jswrenn/typic

Fix Transparency

Closed this issue · 0 comments

A struct should not be Transparent if it has a field that is also not Transparent:

#[typic::repr(C)]
pub struct A(u8);

assert_not_impl_any!(A: Transparent);

#[typic::repr(C)]
pub struct B(pub A);

let _: B = 0u8.transmute_into(); // This *should* fail.

However, the correct behavior isn't quite as simple as usual transitive trait derive (e.g., derive(Clone) and derive(Copy)). For instance:

#[typic::repr(C, u8)]
enum Foo {
  A(pub u8),
  B(pub u8),
}

#[typic::repr(C, u8)]
enum Bar {
  A(pub u8),
  B(pub u8),
  C(u8)
}

let _ : Bar = Foo::A(42).transmute_into(); // This *should* work.

To me, this implies that transparency should be part of the layout information at both low and high levels.