Skip to content

Suggest to take and ignore args while closure args count mismatching #52647

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

Merged
merged 7 commits into from
Jul 26, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
23 changes: 23 additions & 0 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,29 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
if let Some(found_span) = found_span {
err.span_label(found_span, format!("takes {}", found_str));

// Suggest to take and ignore the arguments with expected_args_length `_`s if
// found arguments is empty(Suppose the user just wants to ignore args in this case).
// like `|_, _|` for closure with 2 expected args.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

            // Suggest to take and ignore the arguments with expected_args_length `_`s if
            // found arguments is empty (assume the user just wants to ignore args in this case).
            // For example, if `expected_args_length` is 2, suggest `|_, _|`.

if found_args.is_empty() && is_closure {
let mut underscores = "_".repeat(expected_args.len())
.split("")
.filter(|s| !s.is_empty())
.collect::<Vec<_>>()
.join(", ");
err.span_suggestion_with_applicability(
found_span,
&format!("change the closure to take and ignore the argument{}",
if expected_args.len() < 2 {
""
} else {
"s"
}
),
format!("|{}|", underscores),
Applicability::MachineApplicable,
);
}

if let &[ArgKind::Tuple(_, ref fields)] = &found_args[..] {
if fields.len() == expected_args.len() {
let sugg = fields.iter()
Expand Down
12 changes: 8 additions & 4 deletions src/test/ui/mismatched_types/closure-arg-count.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ error[E0593]: closure is expected to take 2 arguments, but it takes 0 arguments
--> $DIR/closure-arg-count.rs:15:15
|
LL | [1, 2, 3].sort_by(|| panic!());
| ^^^^^^^ -- takes 0 arguments
| |
| ^^^^^^^ --
| | |
| | takes 0 arguments
| | help: change the closure to take and ignore the arguments: `|_, _|`
Copy link
Member Author

@csmoe csmoe Jul 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@estebank the position of this help message is a bit weird since I span_suggestion_with_applicability the same way as the tuple-args do. what I want:

    |
 help: change the closure to take multiple arguments instead of a single tuple
    |
 LL |     [1, 2, 3].sort_by(|tuple, tuple2| panic!());
    |                       ^^^^^^^^^^^^^^^

How to fix this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestions that have fewer than 10 words (among other things) are emitted as above, inline. Making the message longer is one of the ways of ensuring the separate suggestion line.

| expected closure that takes 2 arguments

error[E0593]: closure is expected to take 2 arguments, but it takes 1 argument
Expand Down Expand Up @@ -42,8 +44,10 @@ error[E0593]: closure is expected to take 1 argument, but it takes 0 arguments
--> $DIR/closure-arg-count.rs:23:5
|
LL | f(|| panic!());
| ^ -- takes 0 arguments
| |
| ^ --
| | |
| | takes 0 arguments
| | help: change the closure to take and ignore the argument: `|_|`
| expected closure that takes 1 argument
|
note: required by `f`
Expand Down