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

Recursive diagnostics when using consts in arrays #72929

Open
SeeSpring opened this issue Jun 2, 2020 · 3 comments
Open

Recursive diagnostics when using consts in arrays #72929

SeeSpring opened this issue Jun 2, 2020 · 3 comments
Labels
A-associated-items Area: Associated items such as associated types and consts. A-diagnostics Area: Messages for errors, warnings, and lints A-traits Area: Trait system D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@SeeSpring
Copy link
Contributor

I tried this code:

trait A {
    const N: usize;
    const ARRAY: [u8; Self::N];
}

trait B<T: A> {
    fn f() -> [u8; <T as A>::N];
}

Playground

I expected to see this happen: the compiler gives useful diagnostics

Instead, this happened: the compiler gives diagnostics which don't work

   Compiling playground v0.0.1 (/playground)
error[E0599]: no associated item named `N` found for type parameter `Self` in the current scope
 --> src/lib.rs:3:29
  |
3 |     const ARRAY: [u8; Self::N];
  |                             ^ associated item not found in `Self`
  |
  = help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `N`, perhaps you need to add a supertrait for it:
  |
1 | trait A: A {
  |        ^^^

error[E0277]: the trait bound `T: A` is not satisfied
 --> src/lib.rs:7:20
  |
2 |     const N: usize;
  |     --------------- required by `A::N`
...
7 |     fn f() -> [u8; <T as A>::N];
  |                    ^^^^^^^^^^^ the trait `A` is not implemented for `T`
  |
help: consider further restricting this bound
  |
6 | trait B<T: A + A> {
  |              ^^^

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0277, E0599.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

Meta

rustc --version --verbose:

rustc 1.45.0-nightly (74e804683 2020-05-30)
binary: rustc
commit-hash: 74e80468347471779be6060d8d7d6d04e98e467f
commit-date: 2020-05-30
host: x86_64-pc-windows-msvc
release: 1.45.0-nightly
LLVM version: 10.0
@SeeSpring SeeSpring added the C-bug Category: This is a bug. label Jun 2, 2020
@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints A-traits Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 2, 2020
@estebank estebank added A-associated-items Area: Associated items such as associated types and consts. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Jun 4, 2020
@estebank
Copy link
Contributor

estebank commented Jun 4, 2020

cc @eddyb

@Dylan-DPC
Copy link
Member

Current output:

error: generic parameters may not be used in const operations
 --> src/lib.rs:3:23
  |
3 |     const ARRAY: [u8; Self::N];
  |                       ^^^^^^^ cannot perform const operation using `Self`
  |
  = note: type parameters may not be used in const expressions
  = help: use `#![feature(generic_const_exprs)]` to allow generic const expressions

error: generic parameters may not be used in const operations
 --> src/lib.rs:7:21
  |
7 |     fn f() -> [u8; <T as A>::N];
  |                     ^ cannot perform const operation using `T`
  |
  = note: type parameters may not be used in const expressions
  = help: use `#![feature(generic_const_exprs)]` to allow generic const expressions

error: could not compile `playground` (lib) due to 2 previous errors

@estebank estebank added D-papercut Diagnostics: An error or lint that needs small tweaks. and removed C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Mar 30, 2023
@estebank
Copy link
Contributor

The only thing I'd like to change is to make it clear that the array length is a const expression, besides that I believe we can close this ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items such as associated types and consts. A-diagnostics Area: Messages for errors, warnings, and lints A-traits Area: Trait system D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants