Skip to content

Borrowed Fn closure gives "implementation of FnOnce is not general" #87079

Open
@DevinR528

Description

@DevinR528

I tried this code: playground

fn foo<F>(func: F)
where
    F: Fn(&str)
{
    func("sss");
}

fn main() {
    foo(|s| println!("{}",s)); // Ok
    foo(&|s| println!("{}",s)); // Error
}

The current output is:

   Compiling playground v0.0.1 (/playground)
error: implementation of `FnOnce` is not general enough
  --> src/main.rs:10:5
   |
10 |     foo(&|s| println!("{}",s)); // Error
   |     ^^^ implementation of `FnOnce` is not general enough
   |
   = note: closure with signature `fn(&'2 str)` must implement `FnOnce<(&'1 str,)>`, for any lifetime `'1`...
   = note: ...but it actually implements `FnOnce<(&'2 str,)>`, for some specific lifetime `'2`

error: aborting due to previous error

error: could not compile `playground`

This seems similar to #70263 but wasn't sure so I figured I'd error on the side of opening a new one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceC-bugCategory: This is a bug.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