Skip to content

Incorrect diagnostics while referencing supertrait associated const #72190

Closed
@djc

Description

@djc

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.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`A-trait-systemArea: Trait systemC-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions