Skip to content

Improve move related error messages #47093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/librustc_mir/borrow_check/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,30 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
err.emit();
}

pub(super) fn report_move_into_closure(
&mut self,
context: Context,
(place, span): (&Place<'tcx>, Span),
borrow: &BorrowData<'tcx>,
) {
let borrow_msg = match self.describe_place(&borrow.borrowed_place) {
Some(name) => format!("`{}`", name),
None => "value".to_owned(),
};
let mut err = self.tcx.cannot_move_into_closure(
span,
&self.describe_place(place).unwrap_or("_".to_owned()),
Origin::Mir,
);
err.span_label(
self.retrieve_borrow_span(borrow),
format!("borrow of {} occurs here", borrow_msg),
);
err.span_label(span, format!("move into closure occurs here"));
self.explain_why_borrow_contains_point(context, borrow, &mut err);
err.emit();
}

pub(super) fn report_use_while_mutably_borrowed(
&mut self,
context: Context,
Expand All @@ -164,7 +188,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
/// the local assigned at `location`.
/// This is done by searching in statements succeeding `location`
/// and originating from `maybe_closure_span`.
fn find_closure_span(
pub(super) fn find_closure_span(
&self,
maybe_closure_span: Span,
location: Location,
Expand All @@ -177,6 +201,9 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
_ => return None,
};

// When a closure upvar is assigned, it generates the necessary borrows and moves just
// above it and they all have the same span (including the upvar assignment). So, we are
// searching for closure upvar assignment.
for stmt in &self.mir[location.block].statements[location.statement_index + 1..] {
if maybe_closure_span != stmt.source_info.span {
break;
Expand Down
12 changes: 11 additions & 1 deletion src/librustc_mir/borrow_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,17 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
}
WriteKind::Move => {
error_reported = true;
this.report_move_out_while_borrowed(context, place_span, &borrow)

// If the move occurs due to a closure, then we want to show the span
// of the place where it's used in the closure.
let closure_span = this.find_closure_span(place_span.1, context.loc);
if let Some((_, var_span)) = closure_span {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first span is the args_span, you could use it to add a secondary span label pointing at move || saying "moved into this closure". That way there's no ambiguity (think of the case of nested closures).

let place_span = (place_span.0, var_span);
this.report_move_into_closure(context, place_span, &borrow);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

report_move_into_closure could be called from report_move_out_while_borrowed as it already has the context and place_span, the only things needed to verify wether it happened to be a closure move.

} else {
this.report_move_out_while_borrowed(context, place_span, &borrow);
}

}
}
Control::Break
Expand Down
10 changes: 7 additions & 3 deletions src/test/compile-fail/E0504.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// revisions: ast mir
//[mir]compile-flags: -Z borrowck=mir

struct FancyNum {
num: u8,
}

fn main() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you're at this, could we move this file to test/ui?

let fancy_num = FancyNum { num: 5 };
let mut fancy_num = FancyNum { num: 5 };
let fancy_ref = &fancy_num;

let x = move || {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a secondary span label pointing at move || saying "moved into this closure"?

println!("child function: {}", fancy_num.num); //~ ERROR E0504
println!("child function: {}", fancy_num.num);
//[ast]~^ ERROR cannot move `fancy_num` into closure because it is borrowed
//[mir]~^^ ERROR cannot move `fancy_num` into closure because it is borrowed
};

x();
println!("main function: {}", fancy_ref.num);
}