Rust: confusing diagnostic for incorrect type parameter on BoxFuture

Created on 11 May 2020  路  4Comments  路  Source: rust-lang/rust

I tried this code:

use futures::{future::{BoxFuture, Future, FutureExt}};

async fn f() -> bool {
    true
}

#[allow(dead_code)]
struct Foo<'a, F>
where F: Future<Output=bool> + Send {
    f: Option<BoxFuture<'a, F>>,
}

impl<'a, F> Foo<'a, F>
where F: Future<Output=bool> + Send + 'a {
    fn new(f: F) -> Self {
        Self {
            f: Some(f.boxed())
        }
    }
}

fn main() {
    Foo::new(f());
}

I expected to see this happen:
I wish the compiler had suggested that I should change f: Option<BoxFuture<'a, F>> to f: Option<BoxFuture<'a, F::Output>>,

Instead, this happened:

27 | impl<'a, F> Foo<'a, F>
   |          - this type parameter
...
33 |             f: Some(f.boxed())
   |                     ^^^^^^^^^
   |                     |
   |                     expected type parameter `F`, found `bool`
   |                     help: you need to pin and box this expression: `Box::pin(f.boxed())`
*/

Meta

Rust compiler version (stable version on play.rust-lang.org).

1.43.1

Playground link

Backtrace

N/A (compile error)

A-async-await A-diagnostics AsyncAwait-Triaged C-enhancement D-invalid-suggestion E-mentor T-compiler

Most helpful comment

Simple reproducer:

let f: BoxFuture<'static, bool> = async { }.boxed();

playground

All 4 comments

cc @estebank, I think the suggestion shouldn't be applied here. Is there a way we can target it more precisely?

Triage: wg-async-foundations will track this until the incorrect suggestion is removed, other improvements to the suggestion are out of scope for us.

Some extra filtering in the following will be needed, but without further checking I can't tell for sure what the check should be:

https://github.com/rust-lang/rust/pull/69082/files#diff-1d1b0d29a2e8da97c6bfb6e364d920c7R5070-R5089

Simple reproducer:

let f: BoxFuture<'static, bool> = async { }.boxed();

playground

Was this page helpful?
0 / 5 - 0 ratings