Open
Description
EDIT: This can now be made to work on nightly compilers, although it's a bit awkward to use. See this comment.
It would seem that when using size_of in const fn context, it fails to properly compute the size of generic types.
The following function fails
unsafe fn zeroed<T: Sized>() -> T {
// First create an array that has the same size as T, initialized at 0
let x = [0u8; std::mem::size_of::<T>()];
// Then, transmute it to type T, and return it
std::mem::transmute(x)
}
The error is the following :
error[E0277]: the trait bound `T: std::marker::Sized` is not satisfied
--> src/main.rs:3:19
|
3 | let x = [0u8; std::mem::size_of::<T>()];
| ^^^^^^^^^^^^^^^^^^^^^^ `T` does not have a constant size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `T`
= help: consider adding a `where T: std::marker::Sized` bound
= note: required by `std::mem::size_of`
This is very confusing because it complains that T
doesn't have std::marker::Sized
, even though it does have that bound.
Meta
rustc_version : rustc 1.20.0-nightly (ae98ebf 2017-07-20)