Closed
Description
While trying to write an offsetof that works in a constant expr, the compiler gets a low-level assertion failure.
struct Foo {
a: u64,
b: u32,
c: u8
}
macro_rules! offset_of {
($t:ty, $field:ident) => {
unsafe {
(&(*(0 as *const $t)).$field) as *const _ as *const _
}
}
}
const OFFSET_OF_C: *const () = offset_of!(Foo, c);
fn main() {
println!("offset of c: {}" , OFFSET_OF_C as usize);
}
With rust-nightly 2015011204074bed1e8trusty:
$rustc test.rs
test.rs:2:5: 2:11 warning: struct field is never used: `a`, #[warn(dead_code)] on by default
test.rs:2 a: u64,
^~~~~~
test.rs:3:5: 3:11 warning: struct field is never used: `b`, #[warn(dead_code)] on by default
test.rs:3 b: u32,
^~~~~~
rustc: /build/buildd/rust-nightly-201501120407~4bed1e8~trusty/src/llvm/include/llvm/Support/Casting.h:237: typename llvm::cast_retty<X, Y*>::ret_type llvm::cast(Y*) [with X = llvm::GlobalVariable; Y = llvm::Value; typename llvm::cast_retty<X, Y*>::ret_type = llvm::GlobalVariable*]: Assertion `isa<X>(Val) && "cast<Ty>() argument of incompatible type!"' failed.
Aborted
Via @edef1c