Skip to content

Rust doesn't know type is sized #50824

Closed
Closed
@joshlf

Description

@joshlf

rustc is telling me that T needs to be Sized when it is clearly already Sized. This code:

#![feature(const_fn)]

const fn size_of<T: Sized>() -> (usize, T) where T: Sized {
    (::std::mem::size_of::<T>(), ::std::mem::transmute([0u8; ::std::mem::size_of::<T>()]))
}

(playground link)

Produces this error:

error[E0277]: the trait bound `T: std::marker::Sized` is not satisfied
 --> src/main.rs:4:62
  |
4 |     (::std::mem::size_of::<T>(), ::std::mem::transmute([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`

Obviously T: Sized and where T: Sized are redundant, but I did it to demonstrate that neither is sufficient. The same error appears if only one of the bounds is used, or if neither are used.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-diagnosticsArea: Messages for errors, warnings, and lintsA-type-systemArea: Type systemC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions