Skip to content

Commit d2fdb5d

Browse files
authored
Rollup merge of rust-lang#101429 - compiler-errors:issue-101119, r=lcnr
Don't suggest reborrow if usage is inside a closure I can't think of why we would ever be able to *successfully* suggest a mutable reborrow `&mut *` due to a move happening due to a closure, so just suppress it. Fixes rust-lang#101119
2 parents 75e7bb8 + 41d4ea2 commit d2fdb5d

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
258258
let ty = place.ty(self.body, self.infcx.tcx).ty;
259259

260260
// If we're in pattern, we do nothing in favor of the previous suggestion (#80913).
261-
if is_loop_move & !in_pattern {
261+
// Same for if we're in a loop, see #101119.
262+
if is_loop_move & !in_pattern && !matches!(use_spans, UseSpans::ClosureUse { .. }) {
262263
if let ty::Ref(_, _, hir::Mutability::Mut) = ty.kind() {
263264
// We have a `&mut` ref, we need to reborrow on each iteration (#62112).
264265
err.span_suggestion_verbose(

src/test/ui/borrowck/issue-101119.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
struct State;
2+
3+
fn once(_: impl FnOnce()) {}
4+
5+
fn fill_memory_blocks_mt(state: &mut State) {
6+
loop {
7+
once(move || {
8+
//~^ ERROR use of moved value: `state`
9+
fill_segment(state);
10+
});
11+
}
12+
}
13+
14+
fn fill_segment(_: &mut State) {}
15+
16+
fn main() {}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0382]: use of moved value: `state`
2+
--> $DIR/issue-101119.rs:7:14
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 | loop {
7+
LL | once(move || {
8+
| ^^^^^^^ value moved into closure here, in previous iteration of loop
9+
LL |
10+
LL | fill_segment(state);
11+
| ----- use occurs due to use in closure
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0382`.

0 commit comments

Comments
 (0)