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

Closed
Matthias247 opened this issue Mar 5, 2019 · 3 comments

Comments

@Matthias247
Copy link
Contributor

@Matthias247 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
Copy link
Contributor

@nikomatsakis nikomatsakis commented Mar 5, 2019

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

@Zoxc
Copy link
Contributor

@Zoxc 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
Copy link
Contributor

@gilescope 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.

bors added a commit that referenced this issue Apr 25, 2019
Improved error message when type must be bound due to generator.

Fixes #58930.

Keen to get some feedback - is this as minimal as we can get it or is there an existing visitor I could repurpose?
@bors bors closed this in #59111 Apr 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

5 participants