Skip to content

Commit 084029f

Browse files
authored
Rollup merge of #101431 - compiler-errors:move-place-ty-for-move-place-sugg, r=cjgillot
Look at move place's type when suggesting mutable reborrow Not sure why we are looking at the use site's ty instead of the move site's ty in order to suggest reborrowing the move site, but it was suppressing a perfectly valid reborrow suggestion. r? `@estebank` who i think touched this last in 520461f, though that was quite a while ago so feel free to reassign.
2 parents 033f93f + 7e226e6 commit 084029f

File tree

5 files changed

+56
-6
lines changed

5 files changed

+56
-6
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
198198
move_span,
199199
move_spans,
200200
*moved_place,
201-
Some(used_place),
202201
partially_str,
203202
loop_message,
204203
move_msg,

compiler/rustc_borrowck/src/diagnostics/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
972972
move_span: Span,
973973
move_spans: UseSpans<'tcx>,
974974
moved_place: Place<'tcx>,
975-
used_place: Option<PlaceRef<'tcx>>,
976975
partially_str: &str,
977976
loop_message: &str,
978977
move_msg: &str,
@@ -1060,9 +1059,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10601059
place_name, partially_str, loop_message
10611060
),
10621061
);
1063-
// If we have a `&mut` ref, we need to reborrow.
1064-
if let Some(ty::Ref(_, _, hir::Mutability::Mut)) = used_place
1065-
.map(|used_place| used_place.ty(self.body, self.infcx.tcx).ty.kind())
1062+
// If the moved place was a `&mut` ref, then we can
1063+
// suggest to reborrow it where it was moved, so it
1064+
// will still be valid by the time we get to the usage.
1065+
if let ty::Ref(_, _, hir::Mutability::Mut) =
1066+
moved_place.ty(self.body, self.infcx.tcx).ty.kind()
10661067
{
10671068
// If we are in a loop this will be suggested later.
10681069
if !is_loop_move {

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
401401
};
402402
if let Some(use_spans) = use_spans {
403403
self.explain_captures(
404-
&mut err, span, span, use_spans, move_place, None, "", "", "", false, true,
404+
&mut err, span, span, use_spans, move_place, "", "", "", false, true,
405405
);
406406
}
407407
err
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Tests the suggestion to reborrow the first move site
2+
// when we move then borrow a `&mut` ref.
3+
4+
struct State;
5+
6+
impl IntoIterator for &mut State {
7+
type IntoIter = std::vec::IntoIter<()>;
8+
type Item = ();
9+
10+
fn into_iter(self) -> Self::IntoIter {
11+
vec![].into_iter()
12+
}
13+
}
14+
15+
fn once(f: impl FnOnce()) {}
16+
17+
fn fill_memory_blocks_mt(state: &mut State) {
18+
for _ in state {}
19+
//~^ HELP consider creating a fresh reborrow of `state` here
20+
fill_segment(state);
21+
//~^ ERROR borrow of moved value: `state`
22+
}
23+
24+
fn fill_segment(state: &mut State) {}
25+
26+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0382]: borrow of moved value: `state`
2+
--> $DIR/reborrow-sugg-move-then-borrow.rs:20:18
3+
|
4+
LL | fn fill_memory_blocks_mt(state: &mut State) {
5+
| ----- move occurs because `state` has type `&mut State`, which does not implement the `Copy` trait
6+
LL | for _ in state {}
7+
| ----- `state` moved due to this implicit call to `.into_iter()`
8+
LL |
9+
LL | fill_segment(state);
10+
| ^^^^^ value borrowed here after move
11+
|
12+
note: this function takes ownership of the receiver `self`, which moves `state`
13+
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
14+
|
15+
LL | fn into_iter(self) -> Self::IntoIter;
16+
| ^^^^
17+
help: consider creating a fresh reborrow of `state` here
18+
|
19+
LL | for _ in &mut *state {}
20+
| ++++++
21+
22+
error: aborting due to previous error
23+
24+
For more information about this error, try `rustc --explain E0382`.

0 commit comments

Comments
 (0)