Skip to content

Commit 7bfd45a

Browse files
authored
Rollup merge of rust-lang#67247 - JohnTitor:fix-sugg, r=estebank
Don't suggest wrong snippet in closure Fixes rust-lang#67190 r? @estebank
2 parents 246397f + fa199c5 commit 7bfd45a

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

src/librustc_mir/borrow_check/diagnostics/move_errors.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -223,18 +223,24 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
223223

224224
fn report(&mut self, error: GroupedMoveError<'tcx>) {
225225
let (mut err, err_span) = {
226-
let (span, original_path, kind): (Span, &Place<'tcx>, &IllegalMoveOriginKind<'_>) =
226+
let (span, use_spans, original_path, kind,):
227+
(
228+
Span,
229+
Option<UseSpans>,
230+
&Place<'tcx>,
231+
&IllegalMoveOriginKind<'_>,
232+
) =
227233
match error {
228234
GroupedMoveError::MovesFromPlace { span, ref original_path, ref kind, .. } |
229235
GroupedMoveError::MovesFromValue { span, ref original_path, ref kind, .. } => {
230-
(span, original_path, kind)
236+
(span, None, original_path, kind)
231237
}
232238
GroupedMoveError::OtherIllegalMove {
233239
use_spans,
234240
ref original_path,
235241
ref kind
236242
} => {
237-
(use_spans.args_or_use(), original_path, kind)
243+
(use_spans.args_or_use(), Some(use_spans), original_path, kind)
238244
},
239245
};
240246
debug!("report: original_path={:?} span={:?}, kind={:?} \
@@ -250,6 +256,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
250256
original_path,
251257
target_place,
252258
span,
259+
use_spans,
253260
)
254261
}
255262
IllegalMoveOriginKind::InteriorOfTypeWithDestructor { container_ty: ty } => {
@@ -296,6 +303,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
296303
move_place: &Place<'tcx>,
297304
deref_target_place: &Place<'tcx>,
298305
span: Span,
306+
use_spans: Option<UseSpans>,
299307
) -> DiagnosticBuilder<'a> {
300308
// Inspect the type of the content behind the
301309
// borrow to provide feedback about why this
@@ -416,7 +424,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
416424
if let Ok(snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(span) {
417425
let is_option = move_ty.starts_with("std::option::Option");
418426
let is_result = move_ty.starts_with("std::result::Result");
419-
if is_option || is_result {
427+
if (is_option || is_result) && use_spans.map_or(true, |v| !v.for_closure()) {
420428
err.span_suggestion(
421429
span,
422430
&format!("consider borrowing the `{}`'s content", if is_option {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
struct NotCopyable;
2+
3+
fn func<F: FnMut() -> H, H: FnMut()>(_: F) {}
4+
5+
fn parse() {
6+
let mut var = None;
7+
func(|| {
8+
// Shouldn't suggest `move ||.as_ref()` here
9+
move || {
10+
//~^ ERROR: cannot move out of `var`
11+
var = Some(NotCopyable);
12+
}
13+
});
14+
}
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0507]: cannot move out of `var`, a captured variable in an `FnMut` closure
2+
--> $DIR/option-content-move2.rs:9:9
3+
|
4+
LL | let mut var = None;
5+
| ------- captured outer variable
6+
...
7+
LL | move || {
8+
| ^^^^^^^ move out of `var` occurs here
9+
LL |
10+
LL | var = Some(NotCopyable);
11+
| ---
12+
| |
13+
| move occurs because `var` has type `std::option::Option<NotCopyable>`, which does not implement the `Copy` trait
14+
| move occurs due to use in closure
15+
16+
error: aborting due to previous error
17+
18+
For more information about this error, try `rustc --explain E0507`.

0 commit comments

Comments
 (0)