Skip to content

Misleading span in trait bound error with generic parameters #143336

Open
@purplesyringa

Description

@purplesyringa

Code

trait A<T> {
    fn f(self, x: T);
}

fn main() {
    A::f(1, "");
}

Current output

error[E0277]: the trait bound `{integer}: A<_>` is not satisfied
 --> src/lib.rs:6:13
  |
6 |     A::f(1, "");
  |     ----    ^^ the trait `A<_>` is not implemented for `{integer}`
  |     |
  |     required by a bound introduced by this call
  |
help: this trait has no implementations, consider adding one
 --> src/lib.rs:1:1
  |
1 | trait A<T> {
  | ^^^^^^^^^^

For more information about this error, try `rustc --explain E0277`.

Desired output

error[E0277]: the trait bound `{integer}: A` is not satisfied
 --> src/lib.rs:6:10
  |
6 |     A::f(1, "");
  |     ---- ^ the trait `A` is not implemented for `{integer}`
  |     |
  |     required by a bound introduced by this call
  |
help: this trait has no implementations, consider adding one
 --> src/lib.rs:1:1
  |
1 | trait A {
  | ^^^^^^^

For more information about this error, try `rustc --explain E0277`.

Rationale and extra context

No response

Other cases

Rust Version

$ rustc --version --verbose
rustc 1.90.0-nightly (71e4c005c 2025-07-01)
binary: rustc
commit-hash: 71e4c005caa812a16fcb08d0bf1e6f1eda7c8381
commit-date: 2025-07-01
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.7

Anything else?

This is probably related to #83868? That one also emits a misleading diagnostic on the argument, but in this case even the diagnostic on self is missing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-imprecise-spansDiagnostics: spans don't point to exactly the erroneous codeD-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