about summary refs log tree commit diff
diff options
context:
space:
mode:
authorlcnr <rust@lcnr.de>2022-05-26 12:26:30 +0200
committerlcnr <rust@lcnr.de>2022-06-02 10:19:15 +0200
commit681736a6b2297016bcf6964be3e14a77add276d7 (patch)
treecdd1099519acffed3e6b1d0dc2ebbebc281387b9
parent69d575e58dc21175a7365577c84c435a243029eb (diff)
downloadrust-681736a6b2297016bcf6964be3e14a77add276d7.tar.gz
rust-681736a6b2297016bcf6964be3e14a77add276d7.zip
`generic_arg_contains_target`: ignore closures
-rw-r--r--compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs10
-rw-r--r--src/test/ui/inference/cannot-infer-closure-circular.rs4
-rw-r--r--src/test/ui/inference/cannot-infer-closure-circular.stderr12
-rw-r--r--src/test/ui/type/type-check/unknown_type_for_closure.stderr22
4 files changed, 22 insertions, 26 deletions
diff --git a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs
index 1c06c45a923..7b5c377f7b4 100644
--- a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs
+++ b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs
@@ -700,8 +700,14 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
             match inner.unpack() {
                 GenericArgKind::Lifetime(_) => {}
                 GenericArgKind::Type(ty) => {
-                    if matches!(ty.kind(), ty::Opaque(..)) {
-                        // Opaque types can't be named by the user right now
+                    if matches!(ty.kind(), ty::Opaque(..) | ty::Closure(..) | ty::Generator(..)) {
+                        // Opaque types can't be named by the user right now.
+                        //
+                        // Both the generic arguments of closures and generators can
+                        // also not be named. We may want to only look into the closure
+                        // signature in case it has no captures, as that can be represented
+                        // using `fn(T) -> R`.
+
                         // FIXME(type_alias_impl_trait): These opaque types
                         // can actually be named, so it would make sense to
                         // adjust this case and add a test for it.
diff --git a/src/test/ui/inference/cannot-infer-closure-circular.rs b/src/test/ui/inference/cannot-infer-closure-circular.rs
index affb481496d..ae879db68ec 100644
--- a/src/test/ui/inference/cannot-infer-closure-circular.rs
+++ b/src/test/ui/inference/cannot-infer-closure-circular.rs
@@ -4,10 +4,10 @@ fn main() {
     // error handles this gracefully, and in particular doesn't generate an extra
     // note about the `?` operator in the closure body, which isn't relevant to
     // the inference.
-    let x = |r| { //~ ERROR type annotations needed for `Result<(), E>`
+    let x = |r| {
         let v = r?;
         Ok(v)
     };
 
-    let _ = x(x(Ok(())));
+    let _ = x(x(Ok(())));  //~ ERROR type annotations needed for `Result<(), E>`
 }
diff --git a/src/test/ui/inference/cannot-infer-closure-circular.stderr b/src/test/ui/inference/cannot-infer-closure-circular.stderr
index b706cd2bc36..3ad8e3cda16 100644
--- a/src/test/ui/inference/cannot-infer-closure-circular.stderr
+++ b/src/test/ui/inference/cannot-infer-closure-circular.stderr
@@ -1,13 +1,13 @@
 error[E0282]: type annotations needed for `Result<(), E>`
-  --> $DIR/cannot-infer-closure-circular.rs:7:14
+  --> $DIR/cannot-infer-closure-circular.rs:12:9
    |
-LL |     let x = |r| {
-   |              ^
+LL |     let _ = x(x(Ok(())));
+   |         ^
    |
-help: consider giving this closure parameter an explicit type, where the type for type parameter `E` is specified
+help: consider giving this pattern a type, where the type for type parameter `E` is specified
    |
-LL |     let x = |r: Result<(), E>| {
-   |               +++++++++++++++
+LL |     let _: Result<(), E> = x(x(Ok(())));
+   |          +++++++++++++++
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/type/type-check/unknown_type_for_closure.stderr b/src/test/ui/type/type-check/unknown_type_for_closure.stderr
index a9d0dc6e7d8..9ae97f390d3 100644
--- a/src/test/ui/type/type-check/unknown_type_for_closure.stderr
+++ b/src/test/ui/type/type-check/unknown_type_for_closure.stderr
@@ -1,13 +1,8 @@
-error[E0282]: type annotations needed for the closure `fn(Vec<_>)`
-  --> $DIR/unknown_type_for_closure.rs:2:9
+error[E0282]: type annotations needed
+  --> $DIR/unknown_type_for_closure.rs:2:13
    |
 LL |     let x = |b: Vec<_>| {};
-   |         ^
-   |
-help: consider giving `x` an explicit type, where the type for struct `Vec<_>` is specified
-   |
-LL |     let x: [closure@$DIR/unknown_type_for_closure.rs:2:13: 2:27] = |b: Vec<_>| {};
-   |          +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+   |             ^^^^^^^^^^^^^^ cannot infer type for struct `Vec<_>`
 
 error[E0282]: type annotations needed
   --> $DIR/unknown_type_for_closure.rs:6:14
@@ -20,16 +15,11 @@ help: consider giving this closure parameter an explicit type
 LL |     let x = |_: _| {};
    |               +++
 
-error[E0282]: type annotations needed for the closure `fn(_)`
-  --> $DIR/unknown_type_for_closure.rs:10:9
+error[E0282]: type annotations needed
+  --> $DIR/unknown_type_for_closure.rs:10:14
    |
 LL |     let x = |k: _| {};
-   |         ^
-   |
-help: consider giving `x` an explicit type, where the placeholders `_` are specified
-   |
-LL |     let x: [closure@$DIR/unknown_type_for_closure.rs:10:13: 10:22] = |k: _| {};
-   |          +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+   |              ^ cannot infer type
 
 error[E0282]: type annotations needed
   --> $DIR/unknown_type_for_closure.rs:14:28