static-assertions icon indicating copy to clipboard operation
static-assertions copied to clipboard

Create `assert_field_offsets!` macro

Open nvzqz opened this issue 6 years ago • 1 comments

This introduces assert_field_offsets! for ensuring that given struct fields are at specific offsets.

This current implementation works in Rust 1.37 and 1.38. However, the current beta and nightly compilers fail to perform the constant evaluation. This is not unexpected since the current code creates a reference at mem::align_of::<T>(), where there is no initialized memory. Even though the reference is non-null, dereferencing those addresses is definitely undefined behavior.

I won't be merging this PR as-is. However, I'm interested to learn how this macro could be implemented in a sound way.

See also: https://github.com/Gilnaa/memoffset/issues/24.

(cc @oli-obk @RalfJung)

nvzqz avatar Oct 06 '19 13:10 nvzqz

However, I'm interested to learn how this macro could be implemented in a sound way.

Right now, it cannot be implemented soundly. It needs both https://github.com/rust-lang/rust/pull/63810 and https://github.com/rust-lang/rust/pull/64588, as well as allowing &raw in const-context.

RalfJung avatar Oct 08 '19 15:10 RalfJung