Skip to content

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

Closed
@jendrikw

Description

@jendrikw
Contributor

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

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Jun 16, 2024
jendrikw

jendrikw commented on Jun 16, 2024

@jendrikw
ContributorAuthor

@rustbot label +D-confusing

added
D-confusingDiagnostics: Confusing error or lint that should be reworked.
on Jun 16, 2024
fmease

fmease commented on Jun 16, 2024

@fmease
Member

Thanks, that note definitely doesn't make sense.

CC @jieyouxu (#122195).

I suggested to move note_caller_chooses_ty_for_ty_param out of try_suggest_return_impl_trait here in order to catch more cases but that was a bit too aggressive as can be witnessed here — that's on me.

We should replicate some parts of try_suggest_return_impl_trait, namely checking that the type param in question does not occur in the return expression type. That did cross my mind back then but I didn't really follow up on that thought for some reason.

self-assigned this
on Jun 16, 2024
jieyouxu

jieyouxu commented on Jun 16, 2024

@jieyouxu
Member

Thanks for the cc @fmease, I'll take a look ~today/tomorrow

jieyouxu

jieyouxu commented on Jun 16, 2024

@jieyouxu
Member

Opened #126558 to avoid making the note if found return type contains the expected return type (type parameter).

added a commit that references this issue on Jun 18, 2024

Rollup merge of rust-lang#126558 - jieyouxu:caller-chooses-ty, r=fmease

a0f8d86
added 2 commits that reference this issue on Jun 19, 2024

Rollup merge of rust-lang#126558 - jieyouxu:caller-chooses-ty, r=fmease

5ec5dda

Rollup merge of rust-lang#126558 - jieyouxu:caller-chooses-ty, r=fmease

bf841c4
added a commit that references this issue on Jun 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

    Participants

    @jendrikw@fmease@jieyouxu@rustbot

    Issue actions

      Confusing "the caller chooses a type which can be different" [E308] · Issue #126547 · rust-lang/rust