Skip to content

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

Open
@camsteffen

Description

@camsteffen

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsA-higher-rankedArea: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs)D-imprecise-spansDiagnostics: spans don't point to exactly the erroneous codeD-verboseDiagnostics: Too much output caused by a single piece of incorrect code.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