1
Fork 0

generic_arg_contains_target: ignore closures

This commit is contained in:
lcnr 2022-05-26 12:26:30 +02:00
parent 69d575e58d
commit 681736a6b2
4 changed files with 22 additions and 26 deletions

View file

@ -700,8 +700,14 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
match inner.unpack() { match inner.unpack() {
GenericArgKind::Lifetime(_) => {} GenericArgKind::Lifetime(_) => {}
GenericArgKind::Type(ty) => { GenericArgKind::Type(ty) => {
if matches!(ty.kind(), ty::Opaque(..)) { if matches!(ty.kind(), ty::Opaque(..) | ty::Closure(..) | ty::Generator(..)) {
// Opaque types can't be named by the user right now // 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 // FIXME(type_alias_impl_trait): These opaque types
// can actually be named, so it would make sense to // can actually be named, so it would make sense to
// adjust this case and add a test for it. // adjust this case and add a test for it.

View file

@ -4,10 +4,10 @@ fn main() {
// error handles this gracefully, and in particular doesn't generate an extra // 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 // note about the `?` operator in the closure body, which isn't relevant to
// the inference. // the inference.
let x = |r| { //~ ERROR type annotations needed for `Result<(), E>` let x = |r| {
let v = r?; let v = r?;
Ok(v) Ok(v)
}; };
let _ = x(x(Ok(()))); let _ = x(x(Ok(()))); //~ ERROR type annotations needed for `Result<(), E>`
} }

View file

@ -1,12 +1,12 @@
error[E0282]: type annotations needed for `Result<(), E>` 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 error: aborting due to previous error

View file

@ -1,13 +1,8 @@
error[E0282]: type annotations needed for the closure `fn(Vec<_>)` error[E0282]: type annotations needed
--> $DIR/unknown_type_for_closure.rs:2:9 --> $DIR/unknown_type_for_closure.rs:2:13
| |
LL | let x = |b: Vec<_>| {}; LL | let x = |b: Vec<_>| {};
| ^ | ^^^^^^^^^^^^^^ cannot infer type for struct `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<_>| {};
| +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
error[E0282]: type annotations needed error[E0282]: type annotations needed
--> $DIR/unknown_type_for_closure.rs:6:14 --> $DIR/unknown_type_for_closure.rs:6:14
@ -20,16 +15,11 @@ help: consider giving this closure parameter an explicit type
LL | let x = |_: _| {}; LL | let x = |_: _| {};
| +++ | +++
error[E0282]: type annotations needed for the closure `fn(_)` error[E0282]: type annotations needed
--> $DIR/unknown_type_for_closure.rs:10:9 --> $DIR/unknown_type_for_closure.rs:10:14
| |
LL | let x = |k: _| {}; LL | let x = |k: _| {};
| ^ | ^ cannot infer type
|
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: _| {};
| +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
error[E0282]: type annotations needed error[E0282]: type annotations needed
--> $DIR/unknown_type_for_closure.rs:14:28 --> $DIR/unknown_type_for_closure.rs:14:28