Skip to content

async/await produces unspecific "type inside generator must be known in this context" error messages #58930

Closed
@Matthias247

Description

@Matthias247

While trying to write async/await code I encountered lots of error messages with the description

type inside generator must be known in this context

The error is typically displayed at the callsite of the async function, and it's often very unclear what the actual error is. It often seems like the message might be a compiler bug, but I could either find a bug in my code later on or solve it by moving/refactoring things. However it's a bit frustrating to work with, since the message is very unspecific and often does not point to the actual issue. In one case it took me several hours to figure out that a generic bound in a different module (which was neither the caller nor the called async function) was missing in order to enable compilation.

Example:

The following code (playground)

#![feature(futures_api, async_await, await_macro)]
struct Xyz{}

impl Xyz {
    async fn do_sth<F>(self) -> bool
    {
        true
    }
}

async fn foo() {
    let x = Xyz{};
    await!(x.do_sth());
}

leads to the following error message:

error[E0698]: type inside generator must be known in this context
  --> src/lib.rs:14:5
   |
14 |     await!(x.do_sth());
   |     ^^^^^^^^^^^^^^^^^^^
   |
note: the type is part of the generator because of this `yield`
  --> src/lib.rs:14:5

The issue here is an unused type parameter.

Workaround that I discovered much later: Remove the async annotation and call the function as a normal function. That will typically break something else, but often would also show a hint about the real issue.

E.g. if we remove async/await from the example, this error message is displayed:

error[E0282]: type annotations needed
  --> src/lib.rs:14:7
   |
14 |     x.do_sth();
   |       ^^^^^^ cannot infer type for `F`

error: aborting due to previous error

Metadata

Metadata

Assignees

Labels

A-async-awaitArea: Async & AwaitA-diagnosticsArea: Messages for errors, warnings, and lintsAsyncAwait-PolishAsync-await issues that are part of the "polish" areaC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions