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

Loss of trait bound #77344

Closed
stphnt opened this issue Sep 29, 2020 · 2 comments
Closed

Loss of trait bound #77344

stphnt opened this issue Sep 29, 2020 · 2 comments
Labels
C-bug Category: This is a bug.

Comments

@stphnt
Copy link

stphnt commented Sep 29, 2020

I tried this code (playground):

pub trait Max {
    const MAX: usize;
}

pub struct Something();

impl Max for Something {
    const MAX: usize = 10;
}

struct Data<M: Max> {
    storage: [u8; <M as Max>::MAX],
    // alternatively you could define `storage` as the following and a similar error will result.
    // storage: [u8; M::MAX],
    _marker: std::marker::PhantomData<M>,
}

fn main() {
    println!("{}", std::mem::size_of::<Data<Something>>());
}

I expected to see this happen: it compile and the size of the type to be 10.

Instead, this happened: it did not compile and it reported the following confusing/wrong error message:

error[E0277]: the trait bound `M: Max` is not satisfied
  --> src/main.rs:12:19
   |
2  |     const MAX: usize;
   |     ----------------- required by `Max::MAX`
...
12 |     storage: [u8; <M as Max>::MAX],
   |                   ^^^^^^^^^^^^^^^ the trait `Max` is not implemented for `M`
   |
help: consider further restricting this bound
   |
11 | struct Data<M: Max + Max> {
   |                    ^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.

It asks for the trait bound that is currently present to be added (and adding it produces the same error again).

Meta

I checked this on the playground and it happens on:

  • stable (1.46.0)
  • beta (1.47.0-beta.5 2020-09-29 9f0e6fa...)
  • nightly (1.48.0-nightly 2020-09-28 fc2daaa...)

stable and nightly also produced the same error on my personal computer.

rustc --version --verbose:

rustc 1.46.0 (04488afe3 2020-08-24)
binary: rustc
commit-hash: 04488afe34512aa4c33566eb16d8c912a3ae04f9
commit-date: 2020-08-24
host: x86_64-unknown-linux-gnu
release: 1.46.0
LLVM version: 10.0
@stphnt stphnt added the C-bug Category: This is a bug. label Sep 29, 2020
@jonas-schievink
Copy link
Contributor

Thanks for the report! This looks like a duplicate of #43408, however, so closing in favor of that.

Please leave a comment on that issue if you have more information to contribute.

@stphnt
Copy link
Author

stphnt commented Sep 29, 2020

Thanks for the quick response! I suspected there was already an issue for it, but wasn't sure where to look for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants