Skip to content

Commit 754f7cd

Browse files
committed
Add label to help with suggestion to turn bound to FnOnce
1 parent ab39b0f commit 754f7cd

12 files changed

+43
-39
lines changed

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![allow(rustc::untranslatable_diagnostic)]
33

44
use rustc_data_structures::fx::FxHashSet;
5-
use rustc_errors::{Applicability, Diag};
5+
use rustc_errors::{Applicability, Diag, MultiSpan};
66
use rustc_hir::intravisit::Visitor;
77
use rustc_hir::{self as hir, CaptureBy, ExprKind, HirId, Node};
88
use rustc_middle::bug;
@@ -508,7 +508,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
508508
);
509509

510510
let closure_span = tcx.def_span(def_id);
511-
let mut clause_span = DUMMY_SP;
511+
let mut clause_span: MultiSpan = DUMMY_SP.into();
512512
let typck_result = self.infcx.tcx.typeck(self.mir_def_id());
513513
if let Some(closure_def_id) = def_id.as_local()
514514
&& let hir::Node::Expr(expr) = tcx.hir_node_by_def_id(closure_def_id)
@@ -538,7 +538,11 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
538538
{
539539
// We point at the `Fn()` or `FnMut()` bound that coerced the closure, which
540540
// could be changed to `FnOnce()` to avoid the move error.
541-
clause_span = *span;
541+
clause_span = (*span).into();
542+
if fn_def_id.is_local() {
543+
clause_span
544+
.push_span_label(*span, "consider changing this bound to be `FnOnce`");
545+
}
542546
}
543547

544548
self.cannot_move_out_of(span, &place_description)

tests/ui/borrowck/borrowck-move-by-capture.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
1616
--> $DIR/borrowck-move-by-capture.rs:3:37
1717
|
1818
LL | fn to_fn_mut<A:std::marker::Tuple,F:FnMut<A>>(f: F) -> F { f }
19-
| ^^^^^^^^
19+
| ^^^^^^^^ consider changing this bound to be `FnOnce`
2020
help: consider cloning the value before moving it into the closure
2121
|
2222
LL ~ let value = bar.clone();

tests/ui/borrowck/issue-103624.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
1717
--> $DIR/issue-103624.rs:7:36
1818
|
1919
LL | async fn spawn_blocking<T>(f: impl (Fn() -> T) + Send + Sync + 'static) -> T {
20-
| ^^^^^^^^^^^
20+
| ^^^^^^^^^^^ consider changing this bound to be `FnOnce`
2121
note: if `StructB` implemented `Clone`, you could clone the value
2222
--> $DIR/issue-103624.rs:23:1
2323
|

tests/ui/borrowck/issue-87456-point-to-closure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Regression test for #87456.
22

3-
fn take_mut(_val: impl FnMut()) {}
3+
fn take_mut(_val: impl FnMut()) {} //~ NOTE: consider changing this bound to be `FnOnce`
44

55
fn main() {
66
let val = String::new();

tests/ui/borrowck/issue-87456-point-to-closure.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
1414
--> $DIR/issue-87456-point-to-closure.rs:3:24
1515
|
1616
LL | fn take_mut(_val: impl FnMut()) {}
17-
| ^^^^^^^
17+
| ^^^^^^^ consider changing this bound to be `FnOnce`
1818
help: consider borrowing here
1919
|
2020
LL | let _foo: String = &val;

tests/ui/borrowck/unboxed-closures-move-upvar-from-non-once-ref-closure.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
1414
--> $DIR/unboxed-closures-move-upvar-from-non-once-ref-closure.rs:5:28
1515
|
1616
LL | fn call<F>(f: F) where F : Fn() {
17-
| ^^^^
17+
| ^^^^ consider changing this bound to be `FnOnce`
1818
note: `into_iter` takes ownership of the receiver `self`, which moves `y`
1919
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
2020
help: you can `clone` the value and consume it, but this might not be your desired behavior

tests/ui/moves/moves-based-on-type-move-out-of-closure-env-issue-1965.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
1414
--> $DIR/moves-based-on-type-move-out-of-closure-env-issue-1965.rs:3:33
1515
|
1616
LL | fn to_fn<A:std::marker::Tuple,F:Fn<A>>(f: F) -> F { f }
17-
| ^^^^^
17+
| ^^^^^ consider changing this bound to be `FnOnce`
1818
help: consider cloning the value if the performance cost is acceptable
1919
|
2020
LL | let _f = to_fn(|| test(i.clone()));

tests/ui/nll/issue-52663-span-decl-captured-variable.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
1414
--> $DIR/issue-52663-span-decl-captured-variable.rs:1:33
1515
|
1616
LL | fn expect_fn<F>(f: F) where F : Fn() {
17-
| ^^^^
17+
| ^^^^ consider changing this bound to be `FnOnce`
1818
help: consider cloning the value if the performance cost is acceptable
1919
|
2020
LL | expect_fn(|| drop(x.0.clone()));

tests/ui/suggestions/dont-suggest-ref/move-into-closure.stderr

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
1616
--> $DIR/move-into-closure.rs:13:18
1717
|
1818
LL | fn consume_fn<F: Fn()>(_f: F) { }
19-
| ^^^^
19+
| ^^^^ consider changing this bound to be `FnOnce`
2020
help: consider borrowing here
2121
|
2222
LL | let X(_t) = &x;
@@ -41,7 +41,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
4141
--> $DIR/move-into-closure.rs:13:18
4242
|
4343
LL | fn consume_fn<F: Fn()>(_f: F) { }
44-
| ^^^^
44+
| ^^^^ consider changing this bound to be `FnOnce`
4545
help: consider borrowing here
4646
|
4747
LL | if let Either::One(_t) = &e { }
@@ -66,7 +66,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
6666
--> $DIR/move-into-closure.rs:13:18
6767
|
6868
LL | fn consume_fn<F: Fn()>(_f: F) { }
69-
| ^^^^
69+
| ^^^^ consider changing this bound to be `FnOnce`
7070
help: consider borrowing here
7171
|
7272
LL | while let Either::One(_t) = &e { }
@@ -94,7 +94,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
9494
--> $DIR/move-into-closure.rs:13:18
9595
|
9696
LL | fn consume_fn<F: Fn()>(_f: F) { }
97-
| ^^^^
97+
| ^^^^ consider changing this bound to be `FnOnce`
9898
help: consider borrowing here
9999
|
100100
LL | match &e {
@@ -122,7 +122,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
122122
--> $DIR/move-into-closure.rs:13:18
123123
|
124124
LL | fn consume_fn<F: Fn()>(_f: F) { }
125-
| ^^^^
125+
| ^^^^ consider changing this bound to be `FnOnce`
126126
help: consider borrowing here
127127
|
128128
LL | match &e {
@@ -147,7 +147,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
147147
--> $DIR/move-into-closure.rs:13:18
148148
|
149149
LL | fn consume_fn<F: Fn()>(_f: F) { }
150-
| ^^^^
150+
| ^^^^ consider changing this bound to be `FnOnce`
151151
help: consider borrowing here
152152
|
153153
LL | let X(mut _t) = &x;
@@ -172,7 +172,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
172172
--> $DIR/move-into-closure.rs:13:18
173173
|
174174
LL | fn consume_fn<F: Fn()>(_f: F) { }
175-
| ^^^^
175+
| ^^^^ consider changing this bound to be `FnOnce`
176176
help: consider borrowing here
177177
|
178178
LL | if let Either::One(mut _t) = &em { }
@@ -197,7 +197,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
197197
--> $DIR/move-into-closure.rs:13:18
198198
|
199199
LL | fn consume_fn<F: Fn()>(_f: F) { }
200-
| ^^^^
200+
| ^^^^ consider changing this bound to be `FnOnce`
201201
help: consider borrowing here
202202
|
203203
LL | while let Either::One(mut _t) = &em { }
@@ -225,7 +225,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
225225
--> $DIR/move-into-closure.rs:13:18
226226
|
227227
LL | fn consume_fn<F: Fn()>(_f: F) { }
228-
| ^^^^
228+
| ^^^^ consider changing this bound to be `FnOnce`
229229
help: consider borrowing here
230230
|
231231
LL | match &em {
@@ -253,7 +253,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
253253
--> $DIR/move-into-closure.rs:13:18
254254
|
255255
LL | fn consume_fn<F: Fn()>(_f: F) { }
256-
| ^^^^
256+
| ^^^^ consider changing this bound to be `FnOnce`
257257
help: consider borrowing here
258258
|
259259
LL | match &em {
@@ -277,7 +277,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
277277
--> $DIR/move-into-closure.rs:25:21
278278
|
279279
LL | fn consume_fnmut<F: FnMut()>(_f: F) { }
280-
| ^^^^^^^
280+
| ^^^^^^^ consider changing this bound to be `FnOnce`
281281
help: consider borrowing here
282282
|
283283
LL | let X(_t) = &x;
@@ -302,7 +302,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
302302
--> $DIR/move-into-closure.rs:25:21
303303
|
304304
LL | fn consume_fnmut<F: FnMut()>(_f: F) { }
305-
| ^^^^^^^
305+
| ^^^^^^^ consider changing this bound to be `FnOnce`
306306
help: consider borrowing here
307307
|
308308
LL | if let Either::One(_t) = &e { }
@@ -327,7 +327,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
327327
--> $DIR/move-into-closure.rs:25:21
328328
|
329329
LL | fn consume_fnmut<F: FnMut()>(_f: F) { }
330-
| ^^^^^^^
330+
| ^^^^^^^ consider changing this bound to be `FnOnce`
331331
help: consider borrowing here
332332
|
333333
LL | while let Either::One(_t) = &e { }
@@ -355,7 +355,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
355355
--> $DIR/move-into-closure.rs:25:21
356356
|
357357
LL | fn consume_fnmut<F: FnMut()>(_f: F) { }
358-
| ^^^^^^^
358+
| ^^^^^^^ consider changing this bound to be `FnOnce`
359359
help: consider borrowing here
360360
|
361361
LL | match &e {
@@ -383,7 +383,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
383383
--> $DIR/move-into-closure.rs:25:21
384384
|
385385
LL | fn consume_fnmut<F: FnMut()>(_f: F) { }
386-
| ^^^^^^^
386+
| ^^^^^^^ consider changing this bound to be `FnOnce`
387387
help: consider borrowing here
388388
|
389389
LL | match &e {
@@ -408,7 +408,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
408408
--> $DIR/move-into-closure.rs:25:21
409409
|
410410
LL | fn consume_fnmut<F: FnMut()>(_f: F) { }
411-
| ^^^^^^^
411+
| ^^^^^^^ consider changing this bound to be `FnOnce`
412412
help: consider borrowing here
413413
|
414414
LL | let X(mut _t) = &x;
@@ -433,7 +433,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
433433
--> $DIR/move-into-closure.rs:25:21
434434
|
435435
LL | fn consume_fnmut<F: FnMut()>(_f: F) { }
436-
| ^^^^^^^
436+
| ^^^^^^^ consider changing this bound to be `FnOnce`
437437
help: consider borrowing here
438438
|
439439
LL | if let Either::One(mut _t) = &em { }
@@ -458,7 +458,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
458458
--> $DIR/move-into-closure.rs:25:21
459459
|
460460
LL | fn consume_fnmut<F: FnMut()>(_f: F) { }
461-
| ^^^^^^^
461+
| ^^^^^^^ consider changing this bound to be `FnOnce`
462462
help: consider borrowing here
463463
|
464464
LL | while let Either::One(mut _t) = &em { }
@@ -486,7 +486,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
486486
--> $DIR/move-into-closure.rs:25:21
487487
|
488488
LL | fn consume_fnmut<F: FnMut()>(_f: F) { }
489-
| ^^^^^^^
489+
| ^^^^^^^ consider changing this bound to be `FnOnce`
490490
help: consider borrowing here
491491
|
492492
LL | match &em {
@@ -514,7 +514,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
514514
--> $DIR/move-into-closure.rs:25:21
515515
|
516516
LL | fn consume_fnmut<F: FnMut()>(_f: F) { }
517-
| ^^^^^^^
517+
| ^^^^^^^ consider changing this bound to be `FnOnce`
518518
help: consider borrowing here
519519
|
520520
LL | match &em {
@@ -542,7 +542,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
542542
--> $DIR/move-into-closure.rs:25:21
543543
|
544544
LL | fn consume_fnmut<F: FnMut()>(_f: F) { }
545-
| ^^^^^^^
545+
| ^^^^^^^ consider changing this bound to be `FnOnce`
546546
help: consider borrowing here
547547
|
548548
LL | match &em {

tests/ui/suggestions/option-content-move2.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
1818
--> $DIR/option-content-move2.rs:5:12
1919
|
2020
LL | fn func<F: FnMut() -> H, H: FnMut()>(_: F) {}
21-
| ^^^^^^^^^^^^
21+
| ^^^^^^^^^^^^ consider changing this bound to be `FnOnce`
2222
note: if `NotCopyable` implemented `Clone`, you could clone the value
2323
--> $DIR/option-content-move2.rs:1:1
2424
|
@@ -48,7 +48,7 @@ help: `Fn` and `FnMut` closures require captured values to be able to be consume
4848
--> $DIR/option-content-move2.rs:5:12
4949
|
5050
LL | fn func<F: FnMut() -> H, H: FnMut()>(_: F) {}
51-
| ^^^^^^^^^^^^
51+
| ^^^^^^^^^^^^ consider changing this bound to be `FnOnce`
5252

5353
error: aborting due to 2 previous errors
5454

0 commit comments

Comments
 (0)