Rollup merge of #80941 - JohnTitor:ref-mut-pat-in-loops, r=varkor
Do not suggest invalid code in pattern with loop Fixes #80913
This commit is contained in:
commit
79a8499f77
3 changed files with 29 additions and 1 deletions
|
@ -141,6 +141,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
||||||
self.add_moved_or_invoked_closure_note(location, used_place, &mut err);
|
self.add_moved_or_invoked_closure_note(location, used_place, &mut err);
|
||||||
|
|
||||||
let mut is_loop_move = false;
|
let mut is_loop_move = false;
|
||||||
|
let mut in_pattern = false;
|
||||||
|
|
||||||
for move_site in &move_site_vec {
|
for move_site in &move_site_vec {
|
||||||
let move_out = self.move_data.moves[(*move_site).moi];
|
let move_out = self.move_data.moves[(*move_site).moi];
|
||||||
|
@ -256,6 +257,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
||||||
"ref ".to_string(),
|
"ref ".to_string(),
|
||||||
Applicability::MachineApplicable,
|
Applicability::MachineApplicable,
|
||||||
);
|
);
|
||||||
|
in_pattern = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(DesugaringKind::ForLoop(_)) = move_span.desugaring_kind() {
|
if let Some(DesugaringKind::ForLoop(_)) = move_span.desugaring_kind() {
|
||||||
|
@ -302,7 +304,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
||||||
let place = &self.move_data.move_paths[mpi].place;
|
let place = &self.move_data.move_paths[mpi].place;
|
||||||
let ty = place.ty(self.body, self.infcx.tcx).ty;
|
let ty = place.ty(self.body, self.infcx.tcx).ty;
|
||||||
|
|
||||||
if is_loop_move {
|
// If we're in pattern, we do nothing in favor of the previous suggestion (#80913).
|
||||||
|
if is_loop_move & !in_pattern {
|
||||||
if let ty::Ref(_, _, hir::Mutability::Mut) = ty.kind() {
|
if let ty::Ref(_, _, hir::Mutability::Mut) = ty.kind() {
|
||||||
// We have a `&mut` ref, we need to reborrow on each iteration (#62112).
|
// We have a `&mut` ref, we need to reborrow on each iteration (#62112).
|
||||||
err.span_suggestion_verbose(
|
err.span_suggestion_verbose(
|
||||||
|
|
10
src/test/ui/borrowck/move-in-pattern-mut-in-loop.rs
Normal file
10
src/test/ui/borrowck/move-in-pattern-mut-in-loop.rs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
// Regression test for #80913.
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut x = 42_i32;
|
||||||
|
let mut opt = Some(&mut x);
|
||||||
|
for _ in 0..5 {
|
||||||
|
if let Some(mut _x) = opt {}
|
||||||
|
//~^ ERROR: use of moved value
|
||||||
|
}
|
||||||
|
}
|
15
src/test/ui/borrowck/move-in-pattern-mut-in-loop.stderr
Normal file
15
src/test/ui/borrowck/move-in-pattern-mut-in-loop.stderr
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
error[E0382]: use of moved value
|
||||||
|
--> $DIR/move-in-pattern-mut-in-loop.rs:7:21
|
||||||
|
|
|
||||||
|
LL | if let Some(mut _x) = opt {}
|
||||||
|
| ^^^^^^ value moved here, in previous iteration of loop
|
||||||
|
|
|
||||||
|
= note: move occurs because value has type `&mut i32`, which does not implement the `Copy` trait
|
||||||
|
help: borrow this field in the pattern to avoid moving `opt.0`
|
||||||
|
|
|
||||||
|
LL | if let Some(ref mut _x) = opt {}
|
||||||
|
| ^^^
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0382`.
|
Loading…
Add table
Add a link
Reference in a new issue