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

Wrong type inference for closure with impl Trait inside another type #54729

Open
antoyo opened this issue Oct 1, 2018 · 2 comments
Open

Wrong type inference for closure with impl Trait inside another type #54729

antoyo opened this issue Oct 1, 2018 · 2 comments
Labels
A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-inference Area: Type inference

Comments

@antoyo
Copy link
Contributor

antoyo commented Oct 1, 2018

Hi.
It compiles with a type annotation as in this code.

fn work() -> impl FnMut(&u32, Option<u32>) -> bool {
    |current, msg| false
}

fn main() {}

This code doesn't compile.

fn doesnt_work() -> Result<impl FnMut(&u32, Option<u32>) -> bool, ()> {
    Ok(|current, msg| false)
}

fn main() {}

error:

error[E0631]: type mismatch in closure arguments
 --> src/main.rs:1:28
  |
1 | fn doesnt_work() -> Result<impl FnMut(&u32, Option<u32>) -> bool, ()> {
  |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected signature of `for<'r> fn(&'r u32, std::option::Option<u32>) -> _`
2 |     Ok(|current, msg| false)
  |        -------------------- found signature of `fn(_, _) -> _`
  |
  = note: the return type of a function must have a statically known size

error[E0271]: type mismatch resolving `for<'r> <[closure@src/main.rs:2:8: 2:28] as std::ops::FnOnce<(&'r u32, std::option::Option<u32>)>>::Output == bool`
 --> src/main.rs:1:28
  |
1 | fn doesnt_work() -> Result<impl FnMut(&u32, Option<u32>) -> bool, ()> {
  |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected bound lifetime parameter, found concrete lifetime
  |
  = note: the return type of a function must have a statically known size

error: aborting due to 2 previous errors

Thanks to fix this issue.

@csmoe csmoe added the A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. label Oct 2, 2018
@ExpHP
Copy link
Contributor

ExpHP commented Oct 4, 2018

Notably, Ok(|current: &_, msg| false) resolves the error. I guess closures in output position for -> impl Fn receive the same special-case treatment from the type checker that those in argument position do?

@jyn514
Copy link
Member

jyn514 commented Jul 7, 2021

Triage: I think this is a duplicate of #41078.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-inference Area: Type inference
Projects
None yet
Development

No branches or pull requests

5 participants