about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorJonas Schievink <jonasschievink@gmail.com>2020-01-24 00:49:44 +0100
committerJonas Schievink <jonasschievink@gmail.com>2020-02-02 13:20:57 +0100
commit5b2059b2572cff9974e6820791c8ab57b6c50234 (patch)
treeb2d054733fdfe5c57d64e96333026275424a7516 /src
parent3c22e51e7f6debd96af76f36aa8b090c40b8acb6 (diff)
downloadrust-5b2059b2572cff9974e6820791c8ab57b6c50234.tar.gz
rust-5b2059b2572cff9974e6820791c8ab57b6c50234.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/librustc_typeck/check/closure.rs5
-rw-r--r--src/test/ui/generator/type-mismatch-error.rs22
-rw-r--r--src/test/ui/generator/type-mismatch-error.stderr19
3 files changed, 44 insertions, 2 deletions
diff --git a/src/librustc_typeck/check/closure.rs b/src/librustc_typeck/check/closure.rs
index fd6be852051..26777b3b010 100644
--- a/src/librustc_typeck/check/closure.rs
+++ b/src/librustc_typeck/check/closure.rs
@@ -265,8 +265,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                 _ => return None,
             }
         } else {
-            // Generators cannot have explicit arguments.
-            vec![]
+            // Generators with a `()` resume type may be defined with 0 or 1 explicit arguments,
+            // else they must have exactly 1 argument. For now though, just give up in this case.
+            return None;
         };
 
         let ret_param_ty = projection.skip_binder().ty;
diff --git a/src/test/ui/generator/type-mismatch-error.rs b/src/test/ui/generator/type-mismatch-error.rs
new file mode 100644
index 00000000000..d39c788a84b
--- /dev/null
+++ b/src/test/ui/generator/type-mismatch-error.rs
@@ -0,0 +1,22 @@
+//! Test that we get the expected type mismatch error instead of "closure is expected to take 0
+//! arguments" (which got introduced after implementing resume arguments).
+
+#![feature(generators, generator_trait)]
+
+use std::ops::Generator;
+
+fn f<G: Generator>(_: G, _: G::Return) {}
+
+fn main() {
+    f(
+        |a: u8| {
+            if false {
+                yield ();
+            } else {
+                a
+                //~^ error: `if` and `else` have incompatible types
+            }
+        },
+        0u8,
+    );
+}
diff --git a/src/test/ui/generator/type-mismatch-error.stderr b/src/test/ui/generator/type-mismatch-error.stderr
new file mode 100644
index 00000000000..8f5949533e2
--- /dev/null
+++ b/src/test/ui/generator/type-mismatch-error.stderr
@@ -0,0 +1,19 @@
+error[E0308]: `if` and `else` have incompatible types
+  --> $DIR/type-mismatch-error.rs:16:17
+   |
+LL | /             if false {
+LL | |                 yield ();
+   | |                 ---------
+   | |                 |       |
+   | |                 |       help: consider removing this semicolon
+   | |                 expected because of this
+LL | |             } else {
+LL | |                 a
+   | |                 ^ expected `()`, found `u8`
+LL | |
+LL | |             }
+   | |_____________- `if` and `else` have incompatible types
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0308`.