From 0da4c44190d657e7c28f077f99b2fa03f06e1649 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 15 Nov 2022 19:43:33 -0800 Subject: [PATCH] Account for closures --- .../rustc_borrowck/src/diagnostics/conflict_errors.rs | 6 ++++-- compiler/rustc_borrowck/src/diagnostics/mod.rs | 8 +++++--- src/test/ui/closure_context/issue-42065.stderr | 4 ---- src/test/ui/moves/borrow-closures-instead-of-move.stderr | 7 ------- .../unboxed-closures-infer-fnonce-call-twice.stderr | 4 ---- .../unboxed-closures-infer-fnonce-move-call-twice.stderr | 4 ---- 6 files changed, 9 insertions(+), 24 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index 70ad3b62805f..04bf4e04736f 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -167,7 +167,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { ); } - self.add_moved_or_invoked_closure_note(location, used_place, &mut err); + let closure = self.add_moved_or_invoked_closure_note(location, used_place, &mut err); let mut is_loop_move = false; let mut in_pattern = false; @@ -193,7 +193,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { } if !seen_spans.contains(&move_span) { - self.suggest_ref_or_clone(mpi, move_span, &mut err, &mut in_pattern); + if !closure { + self.suggest_ref_or_clone(mpi, move_span, &mut err, &mut in_pattern); + } self.explain_captures( &mut err, diff --git a/compiler/rustc_borrowck/src/diagnostics/mod.rs b/compiler/rustc_borrowck/src/diagnostics/mod.rs index 4c3216d98783..c500cbc49e4a 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mod.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mod.rs @@ -70,7 +70,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { location: Location, place: PlaceRef<'tcx>, diag: &mut Diagnostic, - ) { + ) -> bool { debug!("add_moved_or_invoked_closure_note: location={:?} place={:?}", location, place); let mut target = place.local_or_deref_local(); for stmt in &self.body[location.block].statements[location.statement_index..] { @@ -106,7 +106,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { { place.local_or_deref_local().unwrap() } - _ => return, + _ => return false, }; debug!("add_moved_or_invoked_closure_note: closure={:?}", closure); @@ -125,7 +125,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { ty::place_to_string_for_capture(self.infcx.tcx, hir_place) ), ); - return; + return true; } } } @@ -149,9 +149,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { ty::place_to_string_for_capture(self.infcx.tcx, hir_place) ), ); + return true; } } } + false } /// End-user visible description of `place` if one can be found. diff --git a/src/test/ui/closure_context/issue-42065.stderr b/src/test/ui/closure_context/issue-42065.stderr index 4e436ca7c03f..896bb6dc6bee 100644 --- a/src/test/ui/closure_context/issue-42065.stderr +++ b/src/test/ui/closure_context/issue-42065.stderr @@ -16,10 +16,6 @@ note: this value implements `FnOnce`, which causes it to be moved when called | LL | debug_dump_dict(); | ^^^^^^^^^^^^^^^ -help: consider cloning the value if the performance cost is acceptable - | -LL | debug_dump_dict.clone()(); - | ++++++++ error: aborting due to previous error diff --git a/src/test/ui/moves/borrow-closures-instead-of-move.stderr b/src/test/ui/moves/borrow-closures-instead-of-move.stderr index fbeeaf646e1e..9a84ddef7e64 100644 --- a/src/test/ui/moves/borrow-closures-instead-of-move.stderr +++ b/src/test/ui/moves/borrow-closures-instead-of-move.stderr @@ -58,13 +58,6 @@ note: closure cannot be moved more than once as it is not `Copy` due to moving t | LL | x += 1; | ^ -note: consider changing this parameter type in function `takes_fnonce` to borrow instead if owning the value isn't necessary - --> $DIR/borrow-closures-instead-of-move.rs:34:20 - | -LL | fn takes_fnonce(_: impl FnOnce()) {} - | ------------ ^^^^^^^^^^^^^ this parameter takes ownership of the value - | | - | in this function help: consider mutably borrowing `closure` | LL | takes_fnonce(&mut closure); diff --git a/src/test/ui/unboxed-closures/unboxed-closures-infer-fnonce-call-twice.stderr b/src/test/ui/unboxed-closures/unboxed-closures-infer-fnonce-call-twice.stderr index 3175d2942d5a..ab6f06518467 100644 --- a/src/test/ui/unboxed-closures/unboxed-closures-infer-fnonce-call-twice.stderr +++ b/src/test/ui/unboxed-closures/unboxed-closures-infer-fnonce-call-twice.stderr @@ -16,10 +16,6 @@ note: this value implements `FnOnce`, which causes it to be moved when called | LL | tick(); | ^^^^ -help: consider cloning the value if the performance cost is acceptable - | -LL | tick.clone()(); - | ++++++++ error: aborting due to previous error diff --git a/src/test/ui/unboxed-closures/unboxed-closures-infer-fnonce-move-call-twice.stderr b/src/test/ui/unboxed-closures/unboxed-closures-infer-fnonce-move-call-twice.stderr index ffeafb10fda2..8d70a2b17602 100644 --- a/src/test/ui/unboxed-closures/unboxed-closures-infer-fnonce-move-call-twice.stderr +++ b/src/test/ui/unboxed-closures/unboxed-closures-infer-fnonce-move-call-twice.stderr @@ -16,10 +16,6 @@ note: this value implements `FnOnce`, which causes it to be moved when called | LL | tick(); | ^^^^ -help: consider cloning the value if the performance cost is acceptable - | -LL | tick.clone()(); - | ++++++++ error: aborting due to previous error