Skip to content

Commit 636f0a9

Browse files
Rollup merge of rust-lang#55980 - csmoe:issue-55891, r=estebank
Suggest on closure args count mismatching with pipe span Closes rust-lang#55891 r? @estebank
2 parents fa3941c + d93e5b0 commit 636f0a9

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

src/librustc/traits/error_reporting.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1092,13 +1092,27 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
10921092
if let Some(found_span) = found_span {
10931093
err.span_label(found_span, format!("takes {}", found_str));
10941094

1095+
// move |_| { ... }
1096+
// ^^^^^^^^-- def_span
1097+
//
1098+
// move |_| { ... }
1099+
// ^^^^^-- prefix
1100+
let prefix_span = self.tcx.sess.source_map().span_until_non_whitespace(found_span);
1101+
// move |_| { ... }
1102+
// ^^^-- pipe_span
1103+
let pipe_span = if let Some(span) = found_span.trim_start(prefix_span) {
1104+
span
1105+
} else {
1106+
found_span
1107+
};
1108+
10951109
// Suggest to take and ignore the arguments with expected_args_length `_`s if
10961110
// found arguments is empty (assume the user just wants to ignore args in this case).
10971111
// For example, if `expected_args_length` is 2, suggest `|_, _|`.
10981112
if found_args.is_empty() && is_closure {
10991113
let underscores = vec!["_"; expected_args.len()].join(", ");
11001114
err.span_suggestion_with_applicability(
1101-
found_span,
1115+
pipe_span,
11021116
&format!(
11031117
"consider changing the closure to take and ignore the expected argument{}",
11041118
if expected_args.len() < 2 {

src/test/ui/mismatched_types/closure-arg-count.rs

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ fn main() {
2222
//~^ ERROR closure is expected to take
2323
f(|| panic!());
2424
//~^ ERROR closure is expected to take
25+
f( move || panic!());
26+
//~^ ERROR closure is expected to take
2527

2628
let _it = vec![1, 2, 3].into_iter().enumerate().map(|i, x| i);
2729
//~^ ERROR closure is expected to take

src/test/ui/mismatched_types/closure-arg-count.stderr

+28-10
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,26 @@ help: consider changing the closure to take and ignore the expected argument
6060
LL | f(|_| panic!());
6161
| ^^^
6262

63+
error[E0593]: closure is expected to take 1 argument, but it takes 0 arguments
64+
--> $DIR/closure-arg-count.rs:25:5
65+
|
66+
LL | f( move || panic!());
67+
| ^ ---------- takes 0 arguments
68+
| |
69+
| expected closure that takes 1 argument
70+
|
71+
note: required by `f`
72+
--> $DIR/closure-arg-count.rs:13:1
73+
|
74+
LL | fn f<F: Fn<usize>>(_: F) {}
75+
| ^^^^^^^^^^^^^^^^^^^^^^^^
76+
help: consider changing the closure to take and ignore the expected argument
77+
|
78+
LL | f( move |_| panic!());
79+
| ^^^
80+
6381
error[E0593]: closure is expected to take a single 2-tuple as argument, but it takes 2 distinct arguments
64-
--> $DIR/closure-arg-count.rs:26:53
82+
--> $DIR/closure-arg-count.rs:28:53
6583
|
6684
LL | let _it = vec![1, 2, 3].into_iter().enumerate().map(|i, x| i);
6785
| ^^^ ------ takes 2 distinct arguments
@@ -73,7 +91,7 @@ LL | let _it = vec![1, 2, 3].into_iter().enumerate().map(|(i, x)| i);
7391
| ^^^^^^^^
7492

7593
error[E0593]: closure is expected to take a single 2-tuple as argument, but it takes 2 distinct arguments
76-
--> $DIR/closure-arg-count.rs:28:53
94+
--> $DIR/closure-arg-count.rs:30:53
7795
|
7896
LL | let _it = vec![1, 2, 3].into_iter().enumerate().map(|i: usize, x| i);
7997
| ^^^ ------------- takes 2 distinct arguments
@@ -85,15 +103,15 @@ LL | let _it = vec![1, 2, 3].into_iter().enumerate().map(|(i, x)| i);
85103
| ^^^^^^^^
86104

87105
error[E0593]: closure is expected to take a single 2-tuple as argument, but it takes 3 distinct arguments
88-
--> $DIR/closure-arg-count.rs:30:53
106+
--> $DIR/closure-arg-count.rs:32:53
89107
|
90108
LL | let _it = vec![1, 2, 3].into_iter().enumerate().map(|i, x, y| i);
91109
| ^^^ --------- takes 3 distinct arguments
92110
| |
93111
| expected closure that takes a single 2-tuple as argument
94112

95113
error[E0593]: function is expected to take a single 2-tuple as argument, but it takes 0 arguments
96-
--> $DIR/closure-arg-count.rs:32:53
114+
--> $DIR/closure-arg-count.rs:34:53
97115
|
98116
LL | let _it = vec![1, 2, 3].into_iter().enumerate().map(foo);
99117
| ^^^ expected function that takes a single 2-tuple as argument
@@ -102,15 +120,15 @@ LL | fn foo() {}
102120
| -------- takes 0 arguments
103121

104122
error[E0593]: closure is expected to take a single 2-tuple as argument, but it takes 3 distinct arguments
105-
--> $DIR/closure-arg-count.rs:35:53
123+
--> $DIR/closure-arg-count.rs:37:53
106124
|
107125
LL | let bar = |i, x, y| i;
108126
| --------- takes 3 distinct arguments
109127
LL | let _it = vec![1, 2, 3].into_iter().enumerate().map(bar);
110128
| ^^^ expected closure that takes a single 2-tuple as argument
111129

112130
error[E0593]: function is expected to take a single 2-tuple as argument, but it takes 2 distinct arguments
113-
--> $DIR/closure-arg-count.rs:37:53
131+
--> $DIR/closure-arg-count.rs:39:53
114132
|
115133
LL | let _it = vec![1, 2, 3].into_iter().enumerate().map(qux);
116134
| ^^^ expected function that takes a single 2-tuple as argument
@@ -119,13 +137,13 @@ LL | fn qux(x: usize, y: usize) {}
119137
| -------------------------- takes 2 distinct arguments
120138

121139
error[E0593]: function is expected to take 1 argument, but it takes 2 arguments
122-
--> $DIR/closure-arg-count.rs:40:41
140+
--> $DIR/closure-arg-count.rs:42:41
123141
|
124142
LL | let _it = vec![1, 2, 3].into_iter().map(usize::checked_add);
125143
| ^^^ expected function that takes 1 argument
126144

127145
error[E0593]: function is expected to take 0 arguments, but it takes 1 argument
128-
--> $DIR/closure-arg-count.rs:43:5
146+
--> $DIR/closure-arg-count.rs:45:5
129147
|
130148
LL | call(Foo);
131149
| ^^^^ expected function that takes 0 arguments
@@ -134,11 +152,11 @@ LL | struct Foo(u8);
134152
| --------------- takes 1 argument
135153
|
136154
note: required by `call`
137-
--> $DIR/closure-arg-count.rs:50:1
155+
--> $DIR/closure-arg-count.rs:52:1
138156
|
139157
LL | fn call<F, R>(_: F) where F: FnOnce() -> R {}
140158
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
141159

142-
error: aborting due to 13 previous errors
160+
error: aborting due to 14 previous errors
143161

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

0 commit comments

Comments
 (0)