Skip to content

Improve suggestions for closure parameters #97262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,32 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
});

if let Some((decl, body_id)) = closure_decl_and_body_id {
for arg in fn_sig.inputs().skip_binder().iter() {
if let Some((ty, ty_span)) = self.unresolved_type_vars(arg) {
let ty_span = match ty_span {
Some(ty_span) => ty_span,
None => match ty.ty_vid() {
Some(ty_vid) => {
self.inner
.borrow_mut()
.type_variables()
.var_origin(ty_vid)
.span
}
None => continue,
},
};
err.span_label(
ty_span,
"give this closure parameter an explicit type instead of `_`",
);
// We don't want to give the other suggestions when the problem is
// a closure parameter type.
return err;
}
}

// Parameters were all fine, assume there was an issue with the return type
closure_return_type_suggestion(
&mut err,
&decl.output,
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/closures/closure-signature/unspecified-output.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
let a = |a: i32, b: &i32| Vec::new(); //~ ERROR E0282
}
14 changes: 14 additions & 0 deletions src/test/ui/closures/closure-signature/unspecified-output.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0282]: type annotations needed for the closure `fn(i32, &i32) -> Vec<_>`
--> $DIR/unspecified-output.rs:2:31
|
LL | let a = |a: i32, b: &i32| Vec::new();
| ^^^^^^^^ cannot infer type for type parameter `T`
|
help: give this closure an explicit return type without `_` placeholders
|
LL | let a = |a: i32, b: &i32| -> Vec<_> { Vec::new() };
| +++++++++++ +

error: aborting due to previous error

For more information about this error, try `rustc --explain E0282`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
let a = |a: i32, b: fn(i32) -> _| -> Vec<i32> { Vec::new() }; //~ ERROR E0282
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0282]: type annotations needed for the closure `fn(i32, fn(i32) -> _) -> Vec<i32>`
--> $DIR/unspecified-parameter-fn.rs:2:13
|
LL | let a = |a: i32, b: fn(i32) -> _| -> Vec<i32> { Vec::new() };
| ^^^^^^^^^^^^^^^^^^^^^^^-^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| give this closure parameter an explicit type instead of `_`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0282`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
let a = |a: i32, b: &_| -> Vec<i32> { Vec::new() }; //~ ERROR E0282
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0282]: type annotations needed for the closure `fn(i32, &_) -> Vec<i32>`
--> $DIR/unspecified-parameter-ref.rs:2:13
|
LL | let a = |a: i32, b: &_| -> Vec<i32> { Vec::new() };
| ^^^^^^^^^^^^^-^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| give this closure parameter an explicit type instead of `_`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0282`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
let a = |a: i32, b: Vec<_>| -> Vec<i32> { Vec::new() }; //~ ERROR E0282
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0282]: type annotations needed for the closure `fn(i32, Vec<_>) -> Vec<i32>`
--> $DIR/unspecified-parameter-type-param.rs:2:13
|
LL | let a = |a: i32, b: Vec<_>| -> Vec<i32> { Vec::new() };
| ^^^^^^^^^^^^^^^^-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| give this closure parameter an explicit type instead of `_`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0282`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
let a = |a: _, b: &i32| -> Vec<i32> { Vec::new() }; //~ ERROR E0282
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0282]: type annotations needed
--> $DIR/unspecified-parameter.rs:2:14
|
LL | let a = |a: _, b: &i32| -> Vec<i32> { Vec::new() };
| ^ consider giving this closure parameter a type

error: aborting due to previous error

For more information about this error, try `rustc --explain E0282`.