Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect error when using mem::size_of with generics, and "const_size_of" feature enabled. #50651

Closed
peterjoel opened this issue May 11, 2018 · 2 comments

Comments

@peterjoel
Copy link
Contributor

peterjoel commented May 11, 2018

See playground.

I have a function like this:

fn make_empty_array_gen<T>() -> T {
    unsafe {
        let tmp = [0u8; mem::size_of::<T>()];
        mem::transmute(tmp)
    }
}
fn main() {
    let mut foo_gen: [u64; 5] = make_empty_array_gen();
    println!("foo_gen = {:?}", foo_gen);
}

Which, in this usage, I'd expect to be monomorphised like this:

fn make_empty_array() -> [u64; 5] {
    unsafe {
        let tmp = [0u8; mem::size_of::<[u64; 5]>()];
        mem::transmute(tmp)
    }
}

Which has no problems if I just write it like that instead of using generics. But the generic version gives this error:

error[E0277]: the trait bound `T: std::marker::Sized` is not satisfied
  --> src/main.rs:14:25
   |
14 |         let tmp = [0u8; 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`

I'm not sure if this use case is intended to be supported. But, even if it isn't, the error message is very bad, since T: Sized should always be an implied constraint here.

@jannschu
Copy link

Probably duplicate of issue #43408.

@peterjoel
Copy link
Contributor Author

@jannschu Yes, you are right. I'll close this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants