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

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

Open
Matthias247 opened this Issue Mar 5, 2019 · 3 comments

Comments

Projects
None yet
5 participants
@Matthias247
Copy link
Contributor

Matthias247 commented Mar 5, 2019

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

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Mar 5, 2019

Marking as blocking. @gilescope is going to try and do some investigation.

@Zoxc

This comment has been minimized.

Copy link
Contributor

Zoxc commented Mar 5, 2019

I'd look into reusing the code which emits the E0282 error to also emit the error for when a type is required in the generator.

@gilescope

This comment has been minimized.

Copy link
Contributor

gilescope commented Mar 8, 2019

Thanks - think I've got a v1 now. Will have a PR for review once I've got a passing test to go with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.