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

Confusing error message when expecting BoxFuture #68197

Closed
tmandry opened this issue Jan 14, 2020 · 5 comments · Fixed by #69082
Closed

Confusing error message when expecting BoxFuture #68197

tmandry opened this issue Jan 14, 2020 · 5 comments · Fixed by #69082

Comments

@tmandry
Copy link
Contributor

@tmandry tmandry commented Jan 14, 2020

Playground

fn foo() -> BoxFuture<'static, i32> {
    async {
        42
    }
}

gives

error[E0308]: mismatched types
 --> src/lib.rs:5:5
  |
4 |   fn foo() -> BoxFuture<'static, i32> {
  |               ----------------------- expected `std::pin::Pin<std::boxed::Box<(dyn core::future::future::Future<Output = i32> + std::marker::Send + 'static)>>` because of return type
5 | /     async {
6 | |         42
7 | |     }
  | |_____^ expected struct `std::pin::Pin`, found opaque type
  |
  = note: expected type `std::pin::Pin<std::boxed::Box<(dyn core::future::future::Future<Output = i32> + std::marker::Send + 'static)>>`
             found type `impl core::future::future::Future`

The expected struct std::pin::Pin in particular is confusing. This can be a pretty intimidating error message for someone new to async/await, when all they forgot was .boxed().

I'm not sure if we can fix this without stabilizing BoxFuture so we can sprinkle some magic compiler dust on its error messages. Maybe there's something we can do, though. Maybe some intelligent use of type aliases in our error messages?

cc @JakeEhrlich

@estebank

This comment has been minimized.

Copy link
Contributor

@estebank estebank commented Jan 14, 2020

This can be handled without needing to stabilize the struct. It should act the same way that the primitive type cast suggestions do.

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

@nikomatsakis nikomatsakis commented Feb 11, 2020

Some observations:

  • There is no BoxFuture struct, it's a type-alias
  • We could suggest invoking boxed() but that is a method defined only in the futures crate, which feels a bit awkward to me; I'd rather it were part of libstd
  • We can suggest Box::pin(async { .. }) as well (playground)
@tmandry

This comment has been minimized.

Copy link
Contributor Author

@tmandry tmandry commented Feb 11, 2020

marking as OnDeck, since BoxFuture gets used quite often

@tmandry tmandry added this to High priority in wg-async-foundations triage Feb 11, 2020
@estebank estebank self-assigned this Feb 12, 2020
@estebank

This comment has been minimized.

Copy link
Contributor

@estebank estebank commented Feb 12, 2020

I have a PR that suggests the right code for this:

Screen Shot 2020-02-11 at 5 09 37 PM

But if first you try Box::new:

Screen Shot 2020-02-11 at 5 10 12 PM

or Pin::new:

Screen Shot 2020-02-11 at 5 11 11 PM

we will send you in a wild goose chase:

Screen Shot 2020-02-11 at 5 10 53 PM

@estebank

This comment has been minimized.

Copy link
Contributor

@estebank estebank commented Feb 12, 2020

Filed #69083 for the uncovered cases.

@bors bors closed this in ec5bf15 Feb 13, 2020
wg-async-foundations work automation moved this from To do to Done Feb 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.