Skip to content

Commit 5b2059b

Browse files
Fix error message on type mismatch in generator
Instead of "closure is expected to take 0 arguments" we now get the expected type mismatch error.
1 parent 3c22e51 commit 5b2059b

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

src/librustc_typeck/check/closure.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
265265
_ => return None,
266266
}
267267
} else {
268-
// Generators cannot have explicit arguments.
269-
vec![]
268+
// Generators with a `()` resume type may be defined with 0 or 1 explicit arguments,
269+
// else they must have exactly 1 argument. For now though, just give up in this case.
270+
return None;
270271
};
271272

272273
let ret_param_ty = projection.skip_binder().ty;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//! Test that we get the expected type mismatch error instead of "closure is expected to take 0
2+
//! arguments" (which got introduced after implementing resume arguments).
3+
4+
#![feature(generators, generator_trait)]
5+
6+
use std::ops::Generator;
7+
8+
fn f<G: Generator>(_: G, _: G::Return) {}
9+
10+
fn main() {
11+
f(
12+
|a: u8| {
13+
if false {
14+
yield ();
15+
} else {
16+
a
17+
//~^ error: `if` and `else` have incompatible types
18+
}
19+
},
20+
0u8,
21+
);
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0308]: `if` and `else` have incompatible types
2+
--> $DIR/type-mismatch-error.rs:16:17
3+
|
4+
LL | / if false {
5+
LL | | yield ();
6+
| | ---------
7+
| | | |
8+
| | | help: consider removing this semicolon
9+
| | expected because of this
10+
LL | | } else {
11+
LL | | a
12+
| | ^ expected `()`, found `u8`
13+
LL | |
14+
LL | | }
15+
| |_____________- `if` and `else` have incompatible types
16+
17+
error: aborting due to previous error
18+
19+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)