Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
DevinR528 opened this issue Jul 12, 2021 · 1 comment
Open

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

DevinR528 opened this issue Jul 12, 2021 · 1 comment
Labels
A-inference Area: Type inference C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@DevinR528
Copy link
Contributor

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.

@Enselic
Copy link
Member

Enselic commented Jun 7, 2024

Both cases work if you add a type annotation (: &str) to the closure parameter, so this seems like an inference issue. (In was not an obvious duplicate of the other issue so I will not close a duplicate.)

    foo(|s: &str| println!("{}",s)); // Ok
    foo(&|s: &str| println!("{}",s)); // Also works now

@Enselic Enselic added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-inference Area: Type inference and removed needs-triage-legacy labels Jun 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inference Area: Type inference C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants