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

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

Closed
PatchMixolydic opened this issue Sep 4, 2021 · 0 comments · Fixed by #88911
Closed

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

PatchMixolydic opened this issue Sep 4, 2021 · 0 comments · Fixed by #88911
Labels
A-coroutines Area: Coroutines A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. F-coroutines `#![feature(coroutines)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@PatchMixolydic
Copy link
Contributor

PatchMixolydic commented Sep 4, 2021

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

@PatchMixolydic PatchMixolydic added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 4, 2021
@rustbot rustbot added A-coroutines Area: Coroutines D-confusing Diagnostics: Confusing error or lint that should be reworked. F-coroutines `#![feature(coroutines)]` labels Sep 4, 2021
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Sep 16, 2021
…nkov

Improve error message for type mismatch in generator arguments

Fixes rust-lang#88653. The code example given there is invalid because the `Generator` trait (unlike the `Fn` traits) does not take the generator arguments in tupled-up form (because there can only be one argument, from my understanding). Hence, the type error in the example in rust-lang#88653 is correct, because the given generator takes a `bool` argument, whereas the function's return type talks about a generator with a `(bool,)` argument.

The error message is both confusing and wrong, though: It is wrong because it displays the wrong "expected signature", and it is confusing because both the "expected" and "found" notes point at the same span. With my changes, I get the following, more helpful output:
```
error[E0631]: type mismatch in generator arguments
 --> test.rs:5:22
  |
5 | fn foo(bar: bool) -> impl Generator<(bool,)> {
  |                      ^^^^^^^^^^^^^^^^^^^^^^^ expected signature of `fn((bool,)) -> _`
6 |     |bar| {
  |     ----- found signature of `fn(bool) -> _`
```
@bors bors closed this as completed in c97ff09 Sep 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-coroutines Area: Coroutines A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. F-coroutines `#![feature(coroutines)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants