Skip to content

Misleading error message on associated type bounds #87493

Closed
@sffc

Description

@sffc

I tried this code:

trait MyTrait {
    type Assoc;
}

#[allow(dead_code)]
fn foo<S, T>(_s: S, _t: T)
where
    S: MyTrait, T: MyTrait<Assoc == S::Assoc>
{}

Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=adddc330eab816444616202cc576f88e

I expected to see this happen: The error message should indicate that the == should be a single =.

Instead, this happened: The error message gives no hint of the correct syntax. It gives two errors, neither of which are helpful:

error: expected one of `,` or `>`, found `==`
 --> src/lib.rs:8:34
  |
8 |     S: MyTrait, T: MyTrait<Assoc == S::Assoc>
  |                                  ^^ expected one of `,` or `>`
  |
help: expressions must be enclosed in braces to be used as const generic arguments
  |
8 |     S: MyTrait, T: MyTrait<{ Assoc == S::Assoc }>
  |                            ^                   ^

This error is unhelpful because it says "expected one of , or >", but = is also allowed. Also, the suggestion to use curly braces is a red herring.

error[E0107]: this trait takes 0 const arguments but 1 const argument was supplied
 --> src/lib.rs:8:20
  |
8 |     S: MyTrait, T: MyTrait<Assoc == S::Assoc>
  |                    ^^^^^^^------------------- help: remove these generics
  |                    |
  |                    expected 0 const arguments
  |
note: trait defined here, with 0 const parameters
 --> src/lib.rs:1:7
  |
1 | trait MyTrait {
  |       ^^^^^^^

This error is unhelpful because I am not specifying const arguments; I'm specifying associated types.

Meta

Rust version according to play.rust-lang.org: 1.53.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTC-bugCategory: This is a bug.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-papercutDiagnostics: An error or lint that needs small tweaks.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