Skip to content

Confusing error message when expecting BoxFuture #68197

@tmandry

Description

@tmandry
Member

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

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
D-confusingDiagnostics: Confusing error or lint that should be reworked.
D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Jan 14, 2020
estebank

estebank commented on Jan 14, 2020

@estebank
Contributor

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

nikomatsakis

nikomatsakis commented on Feb 11, 2020

@nikomatsakis
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 commented on Feb 11, 2020

@tmandry
MemberAuthor

marking as OnDeck, since BoxFuture gets used quite often

self-assigned this
on Feb 12, 2020
estebank

estebank commented on Feb 12, 2020

@estebank
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

estebank commented on Feb 12, 2020

@estebank
Contributor

Filed #69083 for the uncovered cases.

added a commit that references this issue on Feb 13, 2020
ec5bf15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-async-awaitArea: Async & AwaitA-diagnosticsArea: Messages for errors, warnings, and lintsAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

    Participants

    @nikomatsakis@estebank@tmandry@JohnTitor

    Issue actions

      Confusing error message when expecting BoxFuture · Issue #68197 · rust-lang/rust