Skip to content

if cond { yield; } in a generator causes a confusing type error (E0631) #88653

Closed
@PatchMixolydic

Description

@PatchMixolydic

Given the following code (playground):

#![feature(generators, generator_trait)]

use std::ops::Generator;

fn foo(bar: bool) -> impl Generator<(bool,)> {
    |bar| {
        if bar {
            yield bar;
        }
    }
}

The current output is:

error[E0631]: type mismatch in function arguments
 --> src/lib.rs:5:22
  |
5 | fn foo(bar: bool) -> impl Generator<(bool,)> {
  |                      ^^^^^^^^^^^^^^^^^^^^^^^
  |                      |
  |                      expected signature of `fn(bool) -> _`
  |                      found signature of `fn(bool) -> _`

For more information about this error, try `rustc --explain E0631`.
error: could not compile `playground` due to previous error

This diagnostic seems bizarre since, as far as I'm aware, the type parameter on Generator represents the arguments to the generator (like how Fn is represented at the time of writing), and the generator's parameters can be trivially seen to consist of one bool. Ideally, this should either compile or emit a less confusing diagnostic. I'm not sure whether this is an issue with E0631 or a bug in generators.

This still occurs when returning a bool from the generator, yielding a different type, giving an explicit type to the generator's bar parameter, and when specifying the Return and Yield types in impl Generator.

@rustbot modify labels: +A-generators +D-confusing +F-generators

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-coroutinesArea: CoroutinesA-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.F-coroutines`#![feature(coroutines)]`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