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 doesn't think trait is implemented in array size expression #77011

Closed
sofia-m-a opened this issue Sep 21, 2020 · 1 comment
Closed
Labels
C-bug Category: This is a bug.

Comments

@sofia-m-a
Copy link

In the following code,

pub trait C {
    const SIZE: usize;
    type D;
}

pub struct A<B: C> {
    s: [B; <B as C>::SIZE],
    t: B::D,
}

both Rust Stable 1.46 and Nightly 1.48 report that

error[E0277]: the trait bound `B: C` is not satisfied
 --> src/main.rs:6:12
  |
2 |     const SIZE: usize;
  |     ------------------ required by `C::SIZE`
...
6 |     s: [B; <B as C>::SIZE],
  |            ^^^^^^^^^^^^^^ the trait `C` is not implemented for `B`
  |
help: consider further restricting this bound
  |
5 | pub struct A<B: C + C> {
  |                   ^^^

error: aborting due to previous error

The bound C on the generic parameter B is correctly picked up at the type level, for t: C::D, but not at the constant-value level,
for <B as C>::SIZE

Moreover, the suggested fix is useless

Meta

rustc --version --verbose:

rustc 1.48.0-nightly (1fd5b9d51 2020-09-20)
binary: rustc
commit-hash: 1fd5b9d516c035a898dcb437b2f982bea5d4bc88
commit-date: 2020-09-20
host: x86_64-unknown-linux-gnu
release: 1.48.0-nightly
LLVM version: 11.0

The bug also seems to affect Stable 1.46

If this is intended behavior, how are you meant to bound B appropriately to be able to use associated constants?

Is this something relating to const-generics awaiting stabilization? Either way, the error message is very misleading.

Extended case

Extend the above code with

fn e<B: C>() {
    let s: [B; <B as C>::SIZE] = todo!();
}

struct F;
impl C for F {
    const SIZE: usize = 0;
    type D = F;
}

fn g() {
    let s: [F; <F as C>::SIZE] = todo!();
}

Then, e has the same error as A, but not g.

@sofia-m-a sofia-m-a added the C-bug Category: This is a bug. label Sep 21, 2020
@jonas-schievink
Copy link
Contributor

Thanks for the report! This is currently a known issue where array lengths cannot make use of generic parameters, which is tracked in #43408, so closing as a duplicate of that.

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