rust-lang / rust-lang/rust

Confusing mismatched types error with the same expected/found types when universes are involved

Open
#124,471 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-lifetimes D-confusing T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
fn main() {
    consume_fn(identity);
}

fn identity(x: &u32) -> &u32 { x }

fn consume_fn<F>(_: impl FnOnce(&u32) -> F) {}
Current output
error[E0308]: mismatched types
 --> ./t.rs:4:5
  |
4 |     consume_fn(identity);
  |     ^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
  |
  = note: expected reference `&_`
             found reference `&_`
note: the lifetime requirement is introduced here
 --> ./t.rs:9:42
  |
9 | fn consume_fn<F>(_: impl FnOnce(&u32) -> F) {}
  |                                          ^
Desired output
error[E0308]: mismatched types
 --> ./t.rs:4:5
  |
4 |     consume_fn(identity);
  |     ^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
  |
  = note: expected reference `&'1 _`
             found reference `for<'2> &'2 _`
note: the lifetime requirement is introduced here
 --> ./t.rs:9:42
  |
9 | fn consume_fn<F>(_: impl FnOnce(&u32) -> F) {}
  |                                          ^
Rationale and extra context

Emitting expected/found with the same thing is not very helpful. Ideally we would also mention why the types can't be actually equal here (F being a concrete type and not being able to mention the lifetime in FnOnce), but I'm not sure how to best fit it in an error message.

Other cases

No response

Rust Version
rustc 1.79.0-nightly (ef8b9dcf2 2024-04-24)
binary: rustc
commit-hash: ef8b9dcf23700f2e2265317611460d3a65c19eff
commit-date: 2024-04-24
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.4
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the provided Rust snippet with the referenced rustc 1.79.0-nightly version and compare the current diagnostic with the desired output. Trace the compiler diagnostic path that renders the expected and found types, then verify that the output distinguishes the involved lifetimes without regressing other mismatched-type diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.