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 · 1 comment

Comments

Projects
None yet
4 participants
@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 label Oct 2, 2018

@ExpHP

This comment has been minimized.

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?

@oli-obk oli-obk added the A-inference label Oct 4, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment