Closed
Description
The following code works with Rust 1.29.0:
union TransmuteHack<T: Copy, U: Copy> {
from: T,
to: U,
}
pub static VALUE: usize = 42;
pub static ADDRESS: usize = unsafe { TransmuteHack { from: &VALUE }.to };
But it fails with the current beta (and nightly):
error[E0080]: this static likely exhibits undefined behavior
--> <source>:7:1
|
7 | pub static ADDRESS: usize = unsafe { TransmuteHack { from: &VALUE }.to };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected the type usize
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
error: aborting due to previous error
For more information about this error, try `rustc --explain E0080`.
Compiler returned: 1
I think this code should still be valid. I don't see any reason for this to be undefined behavior. This is a hack, yes, but it's very useful for FFI-related work.