-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
let place_span = (place_span.0, var_span); | ||
this.report_move_into_closure(context, place_span, &borrow); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} else { | ||
this.report_move_out_while_borrowed(context, place_span, &borrow); | ||
} | ||
|
||
} | ||
} | ||
Control::Break | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While you're at this, could we move this file to |
||
let fancy_num = FancyNum { num: 5 }; | ||
let mut fancy_num = FancyNum { num: 5 }; | ||
let fancy_ref = &fancy_num; | ||
|
||
let x = move || { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add a secondary span label pointing at |
||
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); | ||
} |
There was a problem hiding this comment.
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).