Skip to content

Commit 681736a

Browse files
committed
generic_arg_contains_target: ignore closures
1 parent 69d575e commit 681736a

File tree

4 files changed

+22
-26
lines changed

4 files changed

+22
-26
lines changed

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -700,8 +700,14 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
700700
match inner.unpack() {
701701
GenericArgKind::Lifetime(_) => {}
702702
GenericArgKind::Type(ty) => {
703-
if matches!(ty.kind(), ty::Opaque(..)) {
704-
// Opaque types can't be named by the user right now
703+
if matches!(ty.kind(), ty::Opaque(..) | ty::Closure(..) | ty::Generator(..)) {
704+
// Opaque types can't be named by the user right now.
705+
//
706+
// Both the generic arguments of closures and generators can
707+
// also not be named. We may want to only look into the closure
708+
// signature in case it has no captures, as that can be represented
709+
// using `fn(T) -> R`.
710+
705711
// FIXME(type_alias_impl_trait): These opaque types
706712
// can actually be named, so it would make sense to
707713
// adjust this case and add a test for it.

src/test/ui/inference/cannot-infer-closure-circular.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ fn main() {
44
// error handles this gracefully, and in particular doesn't generate an extra
55
// note about the `?` operator in the closure body, which isn't relevant to
66
// the inference.
7-
let x = |r| { //~ ERROR type annotations needed for `Result<(), E>`
7+
let x = |r| {
88
let v = r?;
99
Ok(v)
1010
};
1111

12-
let _ = x(x(Ok(())));
12+
let _ = x(x(Ok(()))); //~ ERROR type annotations needed for `Result<(), E>`
1313
}

src/test/ui/inference/cannot-infer-closure-circular.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error[E0282]: type annotations needed for `Result<(), E>`
2-
--> $DIR/cannot-infer-closure-circular.rs:7:14
2+
--> $DIR/cannot-infer-closure-circular.rs:12:9
33
|
4-
LL | let x = |r| {
5-
| ^
4+
LL | let _ = x(x(Ok(())));
5+
| ^
66
|
7-
help: consider giving this closure parameter an explicit type, where the type for type parameter `E` is specified
7+
help: consider giving this pattern a type, where the type for type parameter `E` is specified
88
|
9-
LL | let x = |r: Result<(), E>| {
10-
| +++++++++++++++
9+
LL | let _: Result<(), E> = x(x(Ok(())));
10+
| +++++++++++++++
1111

1212
error: aborting due to previous error
1313

src/test/ui/type/type-check/unknown_type_for_closure.stderr

+6-16
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
error[E0282]: type annotations needed for the closure `fn(Vec<_>)`
2-
--> $DIR/unknown_type_for_closure.rs:2:9
1+
error[E0282]: type annotations needed
2+
--> $DIR/unknown_type_for_closure.rs:2:13
33
|
44
LL | let x = |b: Vec<_>| {};
5-
| ^
6-
|
7-
help: consider giving `x` an explicit type, where the type for struct `Vec<_>` is specified
8-
|
9-
LL | let x: [closure@$DIR/unknown_type_for_closure.rs:2:13: 2:27] = |b: Vec<_>| {};
10-
| +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5+
| ^^^^^^^^^^^^^^ cannot infer type for struct `Vec<_>`
116

127
error[E0282]: type annotations needed
138
--> $DIR/unknown_type_for_closure.rs:6:14
@@ -20,16 +15,11 @@ help: consider giving this closure parameter an explicit type
2015
LL | let x = |_: _| {};
2116
| +++
2217

23-
error[E0282]: type annotations needed for the closure `fn(_)`
24-
--> $DIR/unknown_type_for_closure.rs:10:9
18+
error[E0282]: type annotations needed
19+
--> $DIR/unknown_type_for_closure.rs:10:14
2520
|
2621
LL | let x = |k: _| {};
27-
| ^
28-
|
29-
help: consider giving `x` an explicit type, where the placeholders `_` are specified
30-
|
31-
LL | let x: [closure@$DIR/unknown_type_for_closure.rs:10:13: 10:22] = |k: _| {};
32-
| +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22+
| ^ cannot infer type
3323

3424
error[E0282]: type annotations needed
3525
--> $DIR/unknown_type_for_closure.rs:14:28

0 commit comments

Comments
 (0)