Skip to content

Confusing "the caller chooses a type which can be different" [E308] #126547

Closed
@jendrikw

Description

@jendrikw

Code

fn f<T>(t: &T) -> T {
    t
}

Current output

error[E0308]: mismatched types
 --> src/lib.rs:2:5
  |
1 | fn f<T>(t: &T) -> T {
  |      -            - expected `T` because of return type
  |      |
  |      expected this type parameter
2 |     t
  |     ^ expected type parameter `T`, found `&T`
  |
  = note: expected type parameter `_`
                  found reference `&_`
  = note: the caller chooses a type for `T` which can be different from `&T`

Desired output

error[E0308]: mismatched types
 --> src/lib.rs:2:5
  |
1 | fn f<T>(t: &T) -> T {
  |      -            - expected `T` because of return type
  |      |
  |      expected this type parameter
2 |     t
  |     ^ expected type parameter `T`, found `&T`
  |
  = note: expected type parameter `_`
                  found reference `&_`

Rationale and extra context

The statement "the caller chooses a type for T which can be different from &T" is confusing because T and &T are always different. I think it would be best to supress that note when the types only differ in reference-ness. The message was introduced in #122195.

Other cases

No response

Rust Version

rustc 1.81.0-nightly (3cf924b93 2024-06-15)
binary: rustc
commit-hash: 3cf924b934322fd7b514600a7dc84fc517515346
commit-date: 2024-06-15
host: x86_64-unknown-linux-gnu
release: 1.81.0-nightly
LLVM version: 18.1.7

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.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