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

Compiler refuses code, demanding Sized bound #54972

Closed
thenewwazoo opened this issue Oct 10, 2018 · 3 comments
Closed

Compiler refuses code, demanding Sized bound #54972

thenewwazoo opened this issue Oct 10, 2018 · 3 comments
Labels
A-traits Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@thenewwazoo
Copy link

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.

@thenewwazoo
Copy link
Author

thenewwazoo commented Oct 10, 2018

Kudos to @stephaneyfx, who reduced it to:

trait Channel {
    type ID;
    const CHANNEL: Self::ID;
}

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

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-traits Area: Trait system labels Oct 10, 2018
@ExpHP
Copy link
Contributor

ExpHP commented Oct 11, 2018

I am perplexed. This can't just be a diagnostics thing, right? I cannot think of any argument (from object safety or otherwise) for why the posted impl would be incorrect for T: Sized, yet correct for T: ?Sized.

@estebank estebank added T-lang Relevant to the language team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed A-diagnostics Area: Messages for errors, warnings, and lints labels Mar 18, 2019
@estebank
Copy link
Contributor

estebank commented Oct 8, 2019

Compiles since 1.35.

@estebank estebank closed this as completed Oct 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-traits Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants