rust-lang / rust-lang/rust

Misleading type error for impl trait lifetime mismatch

Open
#146,633 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Code
struct Foo {}

struct Executor<T>(T)
where
    T: Fn(&Foo);

impl<T> Executor<T>
where
    T: Fn(&Foo),
{
    pub fn do_stuff(&self) {}
}

struct Bar;

impl Bar {
    fn make_closure<'a>(&'a self) -> impl Fn(&'a Foo) {
        |_| {}
    }

    fn do_execute(&self) {
        let mut executor = Executor(self.make_closure());
        executor.do_stuff();
    }
}

fn main() {}
Current output
error[E0599]: no method named `do_stuff` found for struct `Executor<impl Fn(&Foo)>` in the current scope
  --> src/main.rs:23:18
   |
3  | struct Executor<T>(T)
   | ------------------ method `do_stuff` not found for this struct
...
23 |         executor.do_stuff();
   |                  ^^^^^^^^ method not found in `Executor<impl Fn(&Foo)>`
   |
   = note: the method was found for
           - `Executor<T>`

For more information about this error, try `rustc --explain E0599`.
Desired output

Rationale and extra context

The error is misleading, saying that the do_stuff method doesn't exist for the type, when the problem is that make_closure returns a closure with incorrect lifetimes.

The other case is the same type of incorrect lifetime, except as a standalone function instead of a method, but gives a much better error from compiler.

Other cases
fn do_execute<'a>(f: impl Fn(&'a Foo)) {
    // Creates a much better error
    Executor(f);
}
Rust Version
rustc 1.89.0 (29483883e 2025-08-04)
binary: rustc
commit-hash: 29483883eed69d5fb4db01964cdf2af4d86e9cb2
commit-date: 2025-08-04
host: x86_64-unknown-linux-gnu
release: 1.89.0
LLVM version: 20.1.7
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

Reproduce the supplied method and standalone-function examples with rustc 1.89.0, then compare their diagnostics, focusing on the impl-trait lifetime mismatch in make_closure. Done means the method case reports the lifetime problem instead of claiming that do_stuff is missing.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.