Skip to content

Compiler refuses code, demanding Sized bound #54972

Closed
@thenewwazoo

Description

@thenewwazoo

In the following code:

struct Foo;

trait Channel<T> {
    type ID;
    const CHANNEL: Self::ID;
}

impl<T> Channel<T> for Foo {
    type ID = u8;
    const CHANNEL: u8 = 3;
}

the compiler refuses it with the following error:

error[E0277]: the size for values of type `T` cannot be known at compilation time
  --> src/lib.rs:10:5
   |
10 |     const CHANNEL: u8 = 3;
   |     ^^^^^^^^^^^^^^^^^^^^^^ 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-sized>
   = help: consider adding a `where T: std::marker::Sized` bound
   = note: required because of the requirements on the impl of `Channel<T>` for `Foo`

after adding T: ?Sized to lines 3 and 8, the code compiles.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-trait-systemArea: Trait systemT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions