IssueRaiser

Array lengths don't support generic parameters.

**EDIT**: This can now be made to work on nightly compilers, although it's a bit awkward to use. See [this comment](https://github.com/rust-lang/rust/issues/43408#issuecomment-917600907). 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 ```rust 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 (ae98ebfcb 2017-07-20)

Posted by roblabla on Jul 22, 2017, 4:10 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.4,500 issues0 comments

support (Software)

Array lengths don't support generic parameters.

roblablaJul 22, 2017, 4:10 PM

**EDIT**: This can now be made to work on nightly compilers, although it's a bit awkward to use. See [this comment](https://github.com/rust-lang/rust/issues/43408#issuecomment-917600907). 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 ```rust 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 (ae98ebfcb 2017-07-20)

Comments (0)

No comments yet. Start the discussion.

Comment on this issue