When I try to compile:
fn foo<T : Sized> ()
{
use ::std::mem::size_of;
let array = [(); size_of::<T>()];
}
fn main () {}
I get the following error msg:
error[E0277]: the size for values of type `T` cannot be known at compilation time
--> <anon>:4:22
|
4 | let array = [(); size_of::<T>()];
| ^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `T`
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= help: consider adding a `where T: std::marker::Sized` bound
= note: required by `std::mem::size_of`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.
whilst there clearly is a T : Sized
trait bound. I am not that surprised that the code errors, since I am, after all, trying to use the return value of a generic function in a place requiring a const
expression (even though ::std::mem::size_of
is a const fn
), but I expected a more accurate error message. Being told by the compiler that I need a T : Sized
bound when there clearly is such bound does not feel right.
And in an ideal world, it should even compile, since after monomorphisation the size should indeed be known, but that's another issue and I imagine there might be technical reasons forbidding this.