Skip to content

Commit f780faa

Browse files
committed
reduce to single suggestion for all arguments
1 parent 4d87fb5 commit f780faa

File tree

5 files changed

+66
-89
lines changed

5 files changed

+66
-89
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+24-13
Original file line numberDiff line numberDiff line change
@@ -3429,26 +3429,37 @@ fn hint_missing_borrow<'tcx>(
34293429
(ty, refs)
34303430
}
34313431

3432+
let mut to_borrow = Vec::new();
3433+
let mut remove_borrow = Vec::new();
3434+
34323435
for ((found_arg, expected_arg), arg_span) in found_args.zip(expected_args).zip(arg_spans) {
34333436
let (found_ty, found_refs) = get_deref_type_and_refs(*found_arg);
34343437
let (expected_ty, expected_refs) = get_deref_type_and_refs(*expected_arg);
34353438

34363439
if found_ty == expected_ty {
3437-
let hint = if found_refs < expected_refs {
3438-
"consider borrowing the argument"
3439-
} else if found_refs == expected_refs {
3440-
continue;
3441-
} else {
3442-
"do not borrow the argument"
3443-
};
3444-
err.span_suggestion_verbose(
3445-
arg_span,
3446-
hint,
3447-
expected_arg.to_string(),
3448-
Applicability::MaybeIncorrect,
3449-
);
3440+
if found_refs < expected_refs {
3441+
to_borrow.push((arg_span, expected_arg.to_string()));
3442+
} else if found_refs > expected_refs {
3443+
remove_borrow.push((arg_span, expected_arg.to_string()));
3444+
}
34503445
}
34513446
}
3447+
3448+
if !to_borrow.is_empty() {
3449+
err.multipart_suggestion(
3450+
"consider borrowing the argument",
3451+
to_borrow,
3452+
Applicability::MaybeIncorrect,
3453+
);
3454+
}
3455+
3456+
if !remove_borrow.is_empty() {
3457+
err.multipart_suggestion(
3458+
"do not borrow the argument",
3459+
remove_borrow,
3460+
Applicability::MaybeIncorrect,
3461+
);
3462+
}
34523463
}
34533464

34543465
/// Collect all the returned expressions within the input expression.

src/test/ui/anonymous-higher-ranked-lifetime.stderr

+30-66
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@ LL | fn f1<F>(_: F) where F: Fn(&(), &()) {}
1515
| ^^^^^^^^^^^^ required by this bound in `f1`
1616
help: consider borrowing the argument
1717
|
18-
LL | f1(|_: &(), _: ()| {});
19-
| ~~~
20-
help: consider borrowing the argument
21-
|
22-
LL | f1(|_: (), _: &()| {});
23-
| ~~~
18+
LL | f1(|_: &(), _: &()| {});
19+
| ~~~ ~~~
2420

2521
error[E0631]: type mismatch in closure arguments
2622
--> $DIR/anonymous-higher-ranked-lifetime.rs:3:5
@@ -39,12 +35,8 @@ LL | fn f2<F>(_: F) where F: for<'a> Fn(&'a (), &()) {}
3935
| ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `f2`
4036
help: consider borrowing the argument
4137
|
42-
LL | f2(|_: &'a (), _: ()| {});
43-
| ~~~~~~
44-
help: consider borrowing the argument
45-
|
46-
LL | f2(|_: (), _: &()| {});
47-
| ~~~
38+
LL | f2(|_: &'a (), _: &()| {});
39+
| ~~~~~~ ~~~
4840

4941
error[E0631]: type mismatch in closure arguments
5042
--> $DIR/anonymous-higher-ranked-lifetime.rs:4:5
@@ -63,12 +55,8 @@ LL | fn f3<'a, F>(_: F) where F: Fn(&'a (), &()) {}
6355
| ^^^^^^^^^^^^^^^ required by this bound in `f3`
6456
help: consider borrowing the argument
6557
|
66-
LL | f3(|_: &(), _: ()| {});
67-
| ~~~
68-
help: consider borrowing the argument
69-
|
70-
LL | f3(|_: (), _: &()| {});
71-
| ~~~
58+
LL | f3(|_: &(), _: &()| {});
59+
| ~~~ ~~~
7260

7361
error[E0631]: type mismatch in closure arguments
7462
--> $DIR/anonymous-higher-ranked-lifetime.rs:5:5
@@ -87,12 +75,8 @@ LL | fn f4<F>(_: F) where F: for<'r> Fn(&(), &'r ()) {}
8775
| ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `f4`
8876
help: consider borrowing the argument
8977
|
90-
LL | f4(|_: &(), _: ()| {});
91-
| ~~~
92-
help: consider borrowing the argument
93-
|
94-
LL | f4(|_: (), _: &'r ()| {});
95-
| ~~~~~~
78+
LL | f4(|_: &(), _: &'r ()| {});
79+
| ~~~ ~~~~~~
9680

9781
error[E0631]: type mismatch in closure arguments
9882
--> $DIR/anonymous-higher-ranked-lifetime.rs:6:5
@@ -111,19 +95,17 @@ LL | fn f5<F>(_: F) where F: for<'r> Fn(&'r (), &'r ()) {}
11195
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `f5`
11296
help: consider borrowing the argument
11397
|
114-
LL | f5(|_: &'r (), _: ()| {});
115-
| ~~~~~~
116-
help: consider borrowing the argument
117-
|
118-
LL | f5(|_: (), _: &'r ()| {});
119-
| ~~~~~~
98+
LL | f5(|_: &'r (), _: &'r ()| {});
99+
| ~~~~~~ ~~~~~~
120100

121101
error[E0631]: type mismatch in closure arguments
122102
--> $DIR/anonymous-higher-ranked-lifetime.rs:7:5
123103
|
124104
LL | g1(|_: (), _: ()| {});
125-
| ^^ -------------- found signature defined here
126-
| |
105+
| ^^ --------------
106+
| | | |
107+
| | | help: consider borrowing the argument: `&()`
108+
| | found signature defined here
127109
| expected due to this
128110
|
129111
= note: expected closure signature `for<'a> fn(&'a (), Box<(dyn for<'a> Fn(&'a ()) + 'static)>) -> _`
@@ -133,17 +115,15 @@ note: required by a bound in `g1`
133115
|
134116
LL | fn g1<F>(_: F) where F: Fn(&(), Box<dyn Fn(&())>) {}
135117
| ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `g1`
136-
help: consider borrowing the argument
137-
|
138-
LL | g1(|_: &(), _: ()| {});
139-
| ~~~
140118

141119
error[E0631]: type mismatch in closure arguments
142120
--> $DIR/anonymous-higher-ranked-lifetime.rs:8:5
143121
|
144122
LL | g2(|_: (), _: ()| {});
145-
| ^^ -------------- found signature defined here
146-
| |
123+
| ^^ --------------
124+
| | | |
125+
| | | help: consider borrowing the argument: `&()`
126+
| | found signature defined here
147127
| expected due to this
148128
|
149129
= note: expected closure signature `for<'a> fn(&'a (), for<'a> fn(&'a ())) -> _`
@@ -153,17 +133,15 @@ note: required by a bound in `g2`
153133
|
154134
LL | fn g2<F>(_: F) where F: Fn(&(), fn(&())) {}
155135
| ^^^^^^^^^^^^^^^^ required by this bound in `g2`
156-
help: consider borrowing the argument
157-
|
158-
LL | g2(|_: &(), _: ()| {});
159-
| ~~~
160136

161137
error[E0631]: type mismatch in closure arguments
162138
--> $DIR/anonymous-higher-ranked-lifetime.rs:9:5
163139
|
164140
LL | g3(|_: (), _: ()| {});
165-
| ^^ -------------- found signature defined here
166-
| |
141+
| ^^ --------------
142+
| | | |
143+
| | | help: consider borrowing the argument: `&'s ()`
144+
| | found signature defined here
167145
| expected due to this
168146
|
169147
= note: expected closure signature `for<'s> fn(&'s (), Box<(dyn for<'a> Fn(&'a ()) + 'static)>) -> _`
@@ -173,17 +151,15 @@ note: required by a bound in `g3`
173151
|
174152
LL | fn g3<F>(_: F) where F: for<'s> Fn(&'s (), Box<dyn Fn(&())>) {}
175153
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `g3`
176-
help: consider borrowing the argument
177-
|
178-
LL | g3(|_: &'s (), _: ()| {});
179-
| ~~~~~~
180154

181155
error[E0631]: type mismatch in closure arguments
182156
--> $DIR/anonymous-higher-ranked-lifetime.rs:10:5
183157
|
184158
LL | g4(|_: (), _: ()| {});
185-
| ^^ -------------- found signature defined here
186-
| |
159+
| ^^ --------------
160+
| | | |
161+
| | | help: consider borrowing the argument: `&()`
162+
| | found signature defined here
187163
| expected due to this
188164
|
189165
= note: expected closure signature `for<'a> fn(&'a (), for<'r> fn(&'r ())) -> _`
@@ -193,10 +169,6 @@ note: required by a bound in `g4`
193169
|
194170
LL | fn g4<F>(_: F) where F: Fn(&(), for<'r> fn(&'r ())) {}
195171
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `g4`
196-
help: consider borrowing the argument
197-
|
198-
LL | g4(|_: &(), _: ()| {});
199-
| ~~~
200172

201173
error[E0631]: type mismatch in closure arguments
202174
--> $DIR/anonymous-higher-ranked-lifetime.rs:11:5
@@ -215,12 +187,8 @@ LL | fn h1<F>(_: F) where F: Fn(&(), Box<dyn Fn(&())>, &(), fn(&(), &())) {}
215187
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `h1`
216188
help: consider borrowing the argument
217189
|
218-
LL | h1(|_: &(), _: (), _: (), _: ()| {});
219-
| ~~~
220-
help: consider borrowing the argument
221-
|
222-
LL | h1(|_: (), _: (), _: &(), _: ()| {});
223-
| ~~~
190+
LL | h1(|_: &(), _: (), _: &(), _: ()| {});
191+
| ~~~ ~~~
224192

225193
error[E0631]: type mismatch in closure arguments
226194
--> $DIR/anonymous-higher-ranked-lifetime.rs:12:5
@@ -239,12 +207,8 @@ LL | fn h2<F>(_: F) where F: for<'t0> Fn(&(), Box<dyn Fn(&())>, &'t0 (), fn(&(),
239207
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `h2`
240208
help: consider borrowing the argument
241209
|
242-
LL | h2(|_: &(), _: (), _: (), _: ()| {});
243-
| ~~~
244-
help: consider borrowing the argument
245-
|
246-
LL | h2(|_: (), _: (), _: &'t0 (), _: ()| {});
247-
| ~~~~~~~
210+
LL | h2(|_: &(), _: (), _: &'t0 (), _: ()| {});
211+
| ~~~ ~~~~~~~
248212

249213
error: aborting due to 11 previous errors
250214

src/test/ui/closures/multiple-fn-bounds.stderr

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ error[E0631]: type mismatch in closure arguments
22
--> $DIR/multiple-fn-bounds.rs:10:5
33
|
44
LL | foo(move |x| v);
5-
| ^^^ -------- found signature defined here
6-
| |
5+
| ^^^ --------
6+
| | | |
7+
| | | help: do not borrow the argument: `char`
8+
| | found signature defined here
79
| expected due to this
810
|
911
= note: expected closure signature `fn(char) -> _`
@@ -18,10 +20,6 @@ note: required by a bound in `foo`
1820
|
1921
LL | fn foo<F: Fn(&char) -> bool + Fn(char) -> bool>(f: F) {
2022
| ^^^^^^^^^^^^^^^^ required by this bound in `foo`
21-
help: do not borrow the argument
22-
|
23-
LL | foo(move |char| v);
24-
| ~~~~
2523

2624
error: aborting due to previous error
2725

src/test/ui/mismatched_types/closure-arg-type-mismatch.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ error[E0631]: type mismatch in closure arguments
22
--> $DIR/closure-arg-type-mismatch.rs:3:14
33
|
44
LL | a.iter().map(|_: (u32, u32)| 45);
5-
| ^^^ --------------- found signature defined here
6-
| |
5+
| ^^^ ---------------
6+
| | | |
7+
| | | help: consider borrowing the argument: `&(u32, u32)`
8+
| | found signature defined here
79
| expected due to this
810
|
911
= note: expected closure signature `fn(&(u32, u32)) -> _`

src/test/ui/mismatched_types/issue-36053-2.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ error[E0631]: type mismatch in closure arguments
22
--> $DIR/issue-36053-2.rs:7:32
33
|
44
LL | once::<&str>("str").fuse().filter(|a: &str| true).count();
5-
| ^^^^^^ --------- found signature defined here
6-
| |
5+
| ^^^^^^ ---------
6+
| | | |
7+
| | | help: consider borrowing the argument: `&&str`
8+
| | found signature defined here
79
| expected due to this
810
|
911
= note: expected closure signature `for<'a> fn(&'a &str) -> _`

0 commit comments

Comments
 (0)