Skip to content

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

@antoyo

Description

@antoyo

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-inferenceArea: Type inference

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions