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
Assignees
Labels
A-async-await Area: Async & Await A-diagnostics Area: Messages for errors, warnings, and lints AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tmandry
Copy link
Member

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

@JohnTitor JohnTitor added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 14, 2020
@estebank
Copy link
Contributor

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

@estebank estebank added the A-async-await Area: Async & Await label Feb 4, 2020
@nikomatsakis
Copy link
Contributor

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 tmandry added AsyncAwait-OnDeck AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. labels Feb 11, 2020
@tmandry
Copy link
Member Author

tmandry commented Feb 11, 2020

marking as OnDeck, since BoxFuture gets used quite often

@estebank estebank self-assigned this Feb 12, 2020
@estebank
Copy link
Contributor

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
Copy link
Contributor

Filed #69083 for the uncovered cases.

@bors bors closed this as completed in ec5bf15 Feb 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-async-await Area: Async & Await A-diagnostics Area: Messages for errors, warnings, and lints AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants