Skip to content

confusing diagnostic for incorrect type parameter on BoxFuture #72117

Closed
@qutesy

Description

@qutesy

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)

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-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.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

Development

No branches or pull requests

Issue actions