Skip to content

Commit 731ce84

Browse files
authored
Rollup merge of rust-lang#138462 - ShE3py:mut-borrow-in-loop, r=oli-obk
Dedup `&mut *` reborrow suggestion in loops rust-lang#73534 added a reborrow suggestion in loops; rust-lang#127579 generalized this to generic parameters, making the suggestion triggers twice: ```rs use std::io::Read; fn decode_scalar(_reader: impl Read) {} fn decode_array(reader: &mut impl Read) { for _ in 0.. { decode_scalar(reader); } } ``` ``` error[E0382]: use of moved value: `reader` --> src/lib.rs:6:23 | 4 | fn decode_array(reader: &mut impl Read) { | ------ move occurs because `reader` has type `&mut impl Read`, which does not implement the `Copy` trait 5 | for _ in 0.. { | ------------ inside of this loop 6 | decode_scalar(reader); | ^^^^^^ value moved here, in previous iteration of loop | help: consider creating a fresh reborrow of `reader` here | 6 | decode_scalar(&mut *reader); | ++++++ help: consider creating a fresh reborrow of `reader` here | 6 | decode_scalar(&mut *reader); | ++++++ ``` This PR removes the suggestion in loops, as it requires generic parameters anyway (i.e., the reborrow is automatic if there is no generic params). `@rustbot` label +A-borrow-checker +A-diagnostics +A-suggestion-diagnostics +D-papercut
2 parents e5c7451 + 2d2307e commit 731ce84

File tree

2 files changed

+0
-17
lines changed

2 files changed

+0
-17
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

-13
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
181181
let closure = self.add_moved_or_invoked_closure_note(location, used_place, &mut err);
182182

183183
let mut is_loop_move = false;
184-
let mut in_pattern = false;
185184
let mut seen_spans = FxIndexSet::default();
186185

187186
for move_site in &move_site_vec {
@@ -204,7 +203,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
204203
self.suggest_ref_or_clone(
205204
mpi,
206205
&mut err,
207-
&mut in_pattern,
208206
move_spans,
209207
moved_place.as_ref(),
210208
&mut has_suggest_reborrow,
@@ -256,15 +254,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
256254
let place = &self.move_data.move_paths[mpi].place;
257255
let ty = place.ty(self.body, self.infcx.tcx).ty;
258256

259-
// If we're in pattern, we do nothing in favor of the previous suggestion (#80913).
260-
// Same for if we're in a loop, see #101119.
261-
if is_loop_move & !in_pattern && !matches!(use_spans, UseSpans::ClosureUse { .. }) {
262-
if let ty::Ref(_, _, hir::Mutability::Mut) = ty.kind() {
263-
// We have a `&mut` ref, we need to reborrow on each iteration (#62112).
264-
self.suggest_reborrow(&mut err, span, moved_place);
265-
}
266-
}
267-
268257
if self.infcx.param_env.caller_bounds().iter().any(|c| {
269258
c.as_trait_clause().is_some_and(|pred| {
270259
pred.skip_binder().self_ty() == ty && self.infcx.tcx.is_fn_trait(pred.def_id())
@@ -330,7 +319,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
330319
&self,
331320
mpi: MovePathIndex,
332321
err: &mut Diag<'infcx>,
333-
in_pattern: &mut bool,
334322
move_spans: UseSpans<'tcx>,
335323
moved_place: PlaceRef<'tcx>,
336324
has_suggest_reborrow: &mut bool,
@@ -545,7 +533,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
545533
&& !move_span.is_dummy()
546534
&& !self.infcx.tcx.sess.source_map().is_imported(move_span)
547535
{
548-
*in_pattern = true;
549536
let mut sugg = vec![(pat.span.shrink_to_lo(), "ref ".to_string())];
550537
if let Some(pat) = finder.parent_pat {
551538
sugg.insert(0, (pat.span.shrink_to_lo(), "ref ".to_string()));

tests/ui/borrowck/mut-borrow-in-loop-2.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ help: consider creating a fresh reborrow of `value` here
1212
|
1313
LL | Other::handle(&mut *value);
1414
| ++++++
15-
help: consider creating a fresh reborrow of `value` here
16-
|
17-
LL | Other::handle(&mut *value);
18-
| ++++++
1915

2016
error: aborting due to 1 previous error
2117

0 commit comments

Comments
 (0)