Skip to content

note_obligation_cause reporting for non-async/await not optimal #70818

Closed
@rkuhn

Description

@rkuhn

Consider the following code:

fn d<T: Sized>(t: T) -> impl std::future::Future<Output = T> + Send {
    async { t }
}

This produces the following error message in cargo build:

error[E0277]: `T` cannot be sent between threads safely
  --> src/main.rs:27:25
   |
27 | fn d<T: Sized>(t: T) -> impl std::future::Future<Output = T> + Send {
   |      --                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `T` cannot be sent between threads safely
   |      |
   |      help: consider further restricting this bound: `T: std::marker::Send +`
28 |     async { t }
   |     ----------- this returned value is of type `impl std::future::Future`
   |
   = help: within `impl std::future::Future`, the trait `std::marker::Send` is not implemented for `T`
   = note: required because it appears within the type `[static generator@src/main.rs:28:11: 28:16 t:T _]`
   = note: required because it appears within the type `std::future::GenFuture<[static generator@src/main.rs:28:11: 28:16 t:T _]>`
   = note: required because it appears within the type `impl std::future::Future`
   = note: the return type of a function must have a statically known size

and the following tooltip in VS Code:

`T` cannot be sent between threads safely

`T` cannot be sent between threads safely

help: within `impl std::future::Future`, the trait `std::marker::Send` is not implemented for `T`
note: required because it appears within the type `[static generator@src/main.rs:28:11: 28:16 t:T _]`
note: required because it appears within the type `std::future::GenFuture<[static generator@src/main.rs:28:11: 28:16 t:T _]>`
note: required because it appears within the type `impl std::future::Future`
note: the return type of a function must have a statically known sizerustc(E0277)
future.rs(21, 58): within this `impl std::future::Future`
main.rs(27, 18): `T` cannot be sent between threads safely
main.rs(28, 5): this returned value is of type `impl std::future::Future`

@eddyb asked me to open this issue because the notes are mostly confusing (at least they won’t help mere mortals) and said that the message should not mention GenFuture nor [static generator ...]. He used the following code for a larger error message (playground):

async fn foo<T>(x: T) -> T {
    x
}

fn assert_send(_: impl Send) {}

fn test<T>(x: T) {
    assert_send(foo(x));
}

/cc @davidtwco @nikomatsakis

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.E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.P-mediumMedium priorityT-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