Skip to content

Commit ac95e80

Browse files
Suggest function borrow ignoring needs_note
`needs_note` is false if we've already suggested why the type is Copy... but that has nothing to do with the diagnostic.
1 parent a9b02e1 commit ac95e80

9 files changed

+53
-4
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,14 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
284284
None => "value".to_owned(),
285285
};
286286
if self.suggest_borrow_fn_like(&mut err, ty, &move_site_vec, &note_msg) {
287-
// Suppress the next note, since we don't want to put more `Fn`-like bounds onto something that already has them
288-
} else if needs_note {
287+
// Suppress the next suggestion since we don't want to put more bounds onto
288+
// something that already has `Fn`-like bounds (or is a closure), so we can't
289+
// restrict anyways.
290+
} else {
289291
self.suggest_adding_copy_bounds(&mut err, ty, span);
292+
}
290293

294+
if needs_note {
291295
let span = if let Some(local) = place.as_local() {
292296
Some(self.body.local_decls[local].source_info.span)
293297
} else {

src/test/ui/chalkify/closure.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ note: closure cannot be moved more than once as it is not `Copy` due to moving t
1212
|
1313
LL | a = 1;
1414
| ^
15+
help: consider mutably borrowing `b`
16+
|
17+
LL | let mut c = &mut b;
18+
| ++++
1519

1620
error: aborting due to previous error
1721

src/test/ui/closures/2229_closure_analysis/diagnostics/closure-origin-multi-variant-diagnostics.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ note: closure cannot be moved more than once as it is not `Copy` due to moving t
1111
|
1212
LL | if let MultiVariant::Point(ref mut x, _) = point {
1313
| ^^^^^
14+
help: consider mutably borrowing `c`
15+
|
16+
LL | let a = &mut c;
17+
| ++++
1418

1519
error: aborting due to previous error
1620

src/test/ui/closures/2229_closure_analysis/diagnostics/closure-origin-single-variant-diagnostics.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ note: closure cannot be moved more than once as it is not `Copy` due to moving t
1111
|
1212
LL | let SingleVariant::Point(ref mut x, _) = point;
1313
| ^^^^^
14+
help: consider mutably borrowing `c`
15+
|
16+
LL | let b = &mut c;
17+
| ++++
1418

1519
error: aborting due to previous error
1620

src/test/ui/closures/2229_closure_analysis/diagnostics/closure-origin-struct-diagnostics.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ note: closure cannot be moved more than once as it is not `Copy` due to moving t
1111
|
1212
LL | x.y.a += 1;
1313
| ^^^^^
14+
help: consider mutably borrowing `hello`
15+
|
16+
LL | let b = &mut hello;
17+
| ++++
1418

1519
error: aborting due to previous error
1620

src/test/ui/closures/2229_closure_analysis/diagnostics/closure-origin-tuple-diagnostics-1.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ note: closure cannot be moved more than once as it is not `Copy` due to moving t
1111
|
1212
LL | x.0 += 1;
1313
| ^^^
14+
help: consider mutably borrowing `hello`
15+
|
16+
LL | let b = &mut hello;
17+
| ++++
1418

1519
error: aborting due to previous error
1620

src/test/ui/moves/borrow-closures-instead-of-move.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ fn takes_fn_mut(m: impl FnMut()) {
1717

1818
fn has_closure() {
1919
let mut x = 0;
20-
let closure = || {
20+
let mut closure = || {
2121
x += 1;
2222
};
2323
takes_fnonce(closure);
24+
//~^ HELP consider mutably borrowing
2425
closure();
26+
//~^ ERROR borrow of moved value
2527
}
2628

2729
fn maybe() -> bool {

src/test/ui/moves/borrow-closures-instead-of-move.stderr

+20-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ help: consider mutably borrowing `m`
2929
LL | takes_fnonce(&mut m);
3030
| ++++
3131

32-
error: aborting due to 2 previous errors
32+
error[E0382]: borrow of moved value: `closure`
33+
--> $DIR/borrow-closures-instead-of-move.rs:25:5
34+
|
35+
LL | takes_fnonce(closure);
36+
| ------- value moved here
37+
LL |
38+
LL | closure();
39+
| ^^^^^^^ value borrowed here after move
40+
|
41+
note: closure cannot be moved more than once as it is not `Copy` due to moving the variable `x` out of its environment
42+
--> $DIR/borrow-closures-instead-of-move.rs:21:9
43+
|
44+
LL | x += 1;
45+
| ^
46+
help: consider mutably borrowing `closure`
47+
|
48+
LL | takes_fnonce(&mut closure);
49+
| ++++
50+
51+
error: aborting due to 3 previous errors
3352

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

src/test/ui/not-copy-closure.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ note: closure cannot be moved more than once as it is not `Copy` due to moving t
1111
|
1212
LL | a += 1;
1313
| ^
14+
help: consider mutably borrowing `hello`
15+
|
16+
LL | let b = &mut hello;
17+
| ++++
1418

1519
error: aborting due to previous error
1620

0 commit comments

Comments
 (0)