Skip to content

Type inference can't resolve type of output of dyn Future inside of impl Fn inside of Vec #68064

@olegnn

Description

@olegnn

This code (Playground)

fn get_closure_vec(length: usize) -> Vec<impl Fn() -> Box<dyn std::future::Future<Output = usize>>> {
    vec![|| Box::new(async { 5 }) as Box<_>; length]
}

gives error

error[E0271]: type mismatch resolving `<impl std::future::Future as std::future::Future>::Output == usize`
 --> src/lib.rs:2:13
  |
2 |     vec![|| Box::new(async { 5 }) as Box<_>; width]
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected usize, found i32
  |
  = note: required for the cast to the object type `dyn std::future::Future<Output = usize>`

and this code (Playground)

fn get_closure_vec(length: usize) -> Vec<impl Fn() -> Box<dyn std::future::Future<Output = usize>>> {
    vec![|| Box::new(async { 5 }); length]
}

also gives error

error[E0271]: type mismatch resolving `<[closure@src/lib.rs:2:10: 2:39] as std::ops::FnOnce<()>>::Output == std::boxed::Box<(dyn std::future::Future<Output = usize> + 'static)>`
 --> src/lib.rs:1:41
  |
1 | fn get_closure_vec(width: usize) -> Vec<impl Fn() -> Box<dyn std::future::Future<Output = usize>>> {
  |                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected opaque type, found trait std::future::Future
  |
  = note: expected type `std::boxed::Box<impl std::future::Future>`
             found type `std::boxed::Box<(dyn std::future::Future<Output = usize> + 'static)>`
  = note: the return type of a function must have a statically known size

while this one works fine (Playground)

fn get_closure() -> impl Fn() -> Box<dyn std::future::Future<Output = usize>> {
    || Box::new(async { 5 })
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-inferenceArea: Type inferenceC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions