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

Incorrect diagnostics while referencing supertrait associated const #72190

Closed
djc opened this issue May 14, 2020 · 2 comments
Closed

Incorrect diagnostics while referencing supertrait associated const #72190

djc opened this issue May 14, 2020 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-trait-system Area: Trait system C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@djc
Copy link
Contributor

djc commented May 14, 2020

I tried this code:

trait Trait: SuperTrait {
    fn array() -> [u8; Self::SIZE];
}

trait SuperTrait {
    const SIZE: usize;
}

I expected to see this happen: an error that would help me write the correct code.

Instead, this happened: rustc tells me to duplicate the supertrait bound:

error[E0599]: no associated item named `SIZE` found for type parameter `Self` in the current scope
 --> src/lib.rs:2:30
  |
2 |     fn array() -> [u8; Self::SIZE];
  |                              ^^^^ 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 `SIZE`, perhaps you need to add another supertrait for it:
  |
1 | trait Trait: SuperTrait + SuperTrait {
  |                         ^^^^^^^^^^^^

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=a60b4876d5d432ebe824d2dbfa93830d

This happens on both stable and nightly.

(I think it should potentially suggest <Self as SuperTrait>::Size in this case, but that doesn't seem to work either, see #72192.)

@djc djc added the C-bug Category: This is a bug. label May 14, 2020
@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-trait-system Area: Trait system A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. and removed C-bug Category: This is a bug. C-enhancement Category: An issue proposing an enhancement or a PR with one. labels May 14, 2020
@estebank
Copy link
Contributor

estebank commented May 14, 2020

What you're trying to write is not currently representable. The diagnostic needs to be fixed as it has for cases inside blocks already. CC @eddyb.

@compiler-errors
Copy link
Member

Error message is clear now:

error: generic parameters may not be used in const operations
 --> src/lib.rs:2:24
  |
2 |     fn array() -> [u8; Self::SIZE];
  |                        ^^^^^^^^^^ 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

And with generic_const_exprs, it just works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-trait-system Area: Trait system C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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