-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
NLL: Improve move error loop detection (was "First shot at #54015") #54343
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
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.
Updates look good. It'd be nice, if you're up for it, to separate the "rustfmt run" into its own commit (I usually do this by rebasing and "inserting" a rustfmt commit before the "meat").
The main concern though is that the output looks wrong; but then I see there's a travis failure, maybe it just needs to be updated?
|
||
/// True if we traversed a back edge while walking from the point | ||
/// of error to the move site. | ||
traversed_back_edge: bool, |
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.
Nice.
| ^ value used here after move | ||
LL | while true { while true { while true { x = y; x.clone(); } } } | ||
| - value moved here | ||
| ^ value moved here in previous iteration of loop |
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.
Huh, interesting. This doesn't quite look right, since this location is not a move.
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.
Is this the current output?
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.
I expect to see "value used here after move", and the "value moved here..." annotation somewhere else.
Ah, no, the travis failure is that the
|
joydeep$ rustc +stage2 src/test/ui/liveness/liveness-move-in-while.rs error: aborting due to previous error For more information about this error, try |
That is the current error when we compile the file. Expected:
But we are getting:
It completely ate up this line
|
☔ The latest upstream changes (presumably #54229) made this pull request unmergeable. Please resolve the merge conflicts. |
let move_site_vec = self.get_moved_indexes(context, mpi);
debug!(
- "report_use_of_moved_or_uninitialized: mois={:?}",
+ "report_use_of_moved_or_uninitialized: vec<mois,back_edge>={:?}",
move_site_vec
);
- let mois = move_site_vec.clone().into_iter().map(|x| x.moi).collect();
+ let mois = move_site_vec
+ .clone()
+ .into_iter()
+ .map(|x| {
+ let location = self.move_data.moves[x.moi].source;
+ let span = self.mir.source_info(location).span;
+ debug!("moi {:?}: {:?}", x.moi, span);
+ x.moi
+ })
+ .collect();
if move_site_vec.is_empty() {
let root_place = self.prefixes(&place, PrefixSet::All).last().unwrap();
@@ -137,12 +146,19 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
""
};
- if move_site.traversed_back_edge {
+ // if move_site.traversed_back_edge {
+ // is_loop_move = true;
+ // err.span_label(
+ // span,
+ // format!("value moved{} here in previous iteration of loop", move_msg),
+ // );
+ // }
+ if span == move_span {
+ is_loop_move = true;
err.span_label(
span,
format!("value moved{} here in previous iteration of loop", move_msg),
);
- is_loop_move = true;
} else {
err.span_label(move_span, format!("value moved{} here", move_msg));
move_spans.var_span_label(&mut err, "variable moved due to use in closure"); |
This change is basically the same error reporting with the back-edge calculation done (but not used). >$ rustc +stage2 ./src/test/ui/liveness/liveness-move-in-while.rs
error[E0382]: borrow of moved value: `y`
--> ./src/test/ui/liveness/liveness-move-in-while.rs:18:24
|
18 | println!("{}", y); //~ ERROR borrow of moved value: `y`
| ^ value moved here in previous iteration of loop
|
= note: move occurs because `y` has type `std::boxed::Box<isize>`, which does not implement the `Copy` trait
error: aborting due to previous error
For more information about this error, try `rustc --explain E0382`. Compiled with stage 1, this is the output: >$ rustc +stage1 ./src/test/ui/liveness/liveness-move-in-while.rs
error[E0382]: borrow of moved value: `y`
--> ./src/test/ui/liveness/liveness-move-in-while.rs:18:24
|
18 | println!("{}", y); //~ ERROR borrow of moved value: `y`
| ^ value borrowed here after move
19 | while true { while true { while true { x = y; x.clone(); } } }
| - value moved here
|
= note: move occurs because `y` has type `std::boxed::Box<isize>`, which does not implement the `Copy` trait
error: aborting due to previous error
For more information about this error, try `rustc --explain E0382`. |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
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.
This looks really great! Just a couple small things I noticed when taking a look at this.
debug!( | ||
"report_use_of_moved_or_uninitialized: current_location={:?}", | ||
l | ||
location |
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.
nit: could you add is_back_edge
to this debug!
too? It's useful if someone else ever needs to make a change here and work out what is happening from the logging.
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.
Thank you. That is a good point.
@@ -134,8 +152,16 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { | |||
format!("value moved{} here in previous iteration of loop", move_msg), |
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.
nit: perhaps add a comma in this message too.
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.
sure
I couldn't this to the above review below since you didn't change these lines, but could you change the error message in the below snippet to have a comma so all of the "iteration of loop" messages are consistent? (I'm pretty sure this still makes sense gramatically) ie. rust/src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs Lines 48 to 52 in c222479
|
Before this patch running the following command would generate the given output: $ rustc +stage1 src/test/ui/liveness/liveness-move-in-while.rs -Zborrowck=mir -Ztwo-phase-borrows error[E0382]: borrow of moved value: `y` --> src/main.rs:8:24 | 8 | println!("{}", y); //~ ERROR use of moved value: `y` | ^ value borrowed here after move 9 | while true { while true { while true { x = y; x.clone(); } } } | - value moved here | = note: move occurs because `y` has type `std::boxed::Box<isize>`, which does not implement the `Copy` trait We want to give the user more hint by telling them that the value was moved in the previous iteration of the loop. After this patch, the error message adds the phrase "in previous iteration of loop" and in totality looks like this: $ rustc +stage1 src/test/ui/liveness/liveness-move-in-while.rs -Zborrowck=mir -Ztwo-phase-borrows error[E0382]: borrow of moved value: `y` --> src/test/ui/liveness/liveness-move-in-while.rs:17:24 | 17 | println!("{}", y); //~ ERROR use of moved value: `y` | ^ value borrowed here after move 18 | while true { while true { while true { x = y; x.clone(); } } } | - value moved here, in previous iteration of loop | = note: move occurs because `y` has type `std::boxed::Box<isize>`, which does not implement the `Copy` trait
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
@bors r |
@bors r+ |
📌 Commit 671e77d has been approved by |
☀️ Test successful - status-appveyor, status-travis |
Closes #54015