rust-lang / rust-lang/rust

Error points at `None` with implementation of `Fn` is not general enough

Open
#143,281 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-borrow-checker A-diagnostics A-higher-ranked D-imprecise-spans D-verbose T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
fn test() -> Option<impl Fn(&String)> {
    if false {
        return None;
    }
    Some(|_| {})
}
Current output
error: implementation of `Fn` is not general enough
 --> src/lib.rs:3:16
  |
3 |         return None;
  |                ^^^^ implementation of `Fn` is not general enough
  |
  = note: closure with signature `fn(&'2 String)` must implement `Fn<(&'1 String,)>`, for any lifetime `'1`...
  = note: ...but it actually implements `Fn<(&'2 String,)>`, for some specific lifetime `'2`

error: implementation of `FnOnce` is not general enough
 --> src/lib.rs:3:16
  |
3 |         return None;
  |                ^^^^ implementation of `FnOnce` is not general enough
  |
  = note: closure with signature `fn(&'2 String)` must implement `FnOnce<(&'1 String,)>`, for any lifetime `'1`...
  = note: ...but it actually implements `FnOnce<(&'2 String,)>`, for some specific lifetime `'2`

error: implementation of `Fn` is not general enough
 --> src/lib.rs:5:5
  |
5 |     Some(|_| {})
  |     ^^^^^^^^^^^^ implementation of `Fn` is not general enough
  |
  = note: closure with signature `fn(&'0 String)` must implement `Fn<(&String,)>`, for some specific lifetime `'0`...
  = note: ...but it actually implements `Fn<(&'1 String,)>`, for some specific lifetime `'1`

error: could not compile `playground` (lib) due to 3 previous errors
Desired output

I think ideally there would only be an error pointing at the actual closure and not None.

Rationale and extra context

It's made worse that there are two errors on None so the "good" error is buried further.

A broader issue is that this doesn't compile in the first place.

Other cases

Rust Version
1.88.0
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 provided Rust example with Rust 1.88.0 first, then trace the compiler's diagnostic handling for the lifetime and generalization errors reported at None and the closure. Done means the diagnostic points at the actual closure without the two additional None errors.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.