-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
When moving out of a for loop head, suggest borrowing it #59195
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ use std::fmt; | |
use std::rc::Rc; | ||
use rustc_data_structures::sync::Lrc; | ||
use std::hash::{Hash, Hasher}; | ||
use syntax::source_map::CompilerDesugaringKind; | ||
use syntax_pos::{MultiSpan, Span}; | ||
use errors::{Applicability, DiagnosticBuilder, DiagnosticId}; | ||
use log::debug; | ||
|
@@ -744,6 +745,19 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> { | |
}, | ||
moved_lp.ty)); | ||
} | ||
if let (Some(CompilerDesugaringKind::ForLoop), Ok(snippet)) = ( | ||
move_span.compiler_desugaring_kind(), | ||
self.tcx.sess.source_map().span_to_snippet(move_span), | ||
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. Nit: not sure whether |
||
) { | ||
if !snippet.starts_with("&") { | ||
err.span_suggestion( | ||
move_span, | ||
"consider borrowing this to avoid moving it into the for loop", | ||
format!("&{}", snippet), | ||
Applicability::MaybeIncorrect, | ||
); | ||
} | ||
} | ||
|
||
// Note: we used to suggest adding a `ref binding` or calling | ||
// `clone` but those suggestions have been removed because | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
error[E0505]: cannot move out of `a` because it is borrowed | ||
--> $DIR/borrow-for-loop-head.rs:4:18 | ||
| | ||
LL | for i in &a { | ||
| -- | ||
| | | ||
| borrow of `a` occurs here | ||
| borrow later used here | ||
LL | for j in a { | ||
| ^ move out of `a` occurs here | ||
|
||
error[E0382]: use of moved value: `a` | ||
--> $DIR/borrow-for-loop-head.rs:4:18 | ||
| | ||
LL | let a = vec![1, 2, 3]; | ||
| - move occurs because `a` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait | ||
LL | for i in &a { | ||
LL | for j in a { | ||
| ^ value moved here, in previous iteration of loop | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
Some errors occurred: E0382, E0505. | ||
For more information about an error, try `rustc --explain E0382`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
fn main() { | ||
let a = vec![1, 2, 3]; | ||
for i in &a { | ||
for j in a { | ||
//~^ ERROR cannot move out of `a` because it is borrowed | ||
//~| ERROR use of moved value: `a` | ||
println!("{} * {} = {}", i, j, i * j); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
error[E0505]: cannot move out of `a` because it is borrowed | ||
--> $DIR/borrow-for-loop-head.rs:4:18 | ||
| | ||
LL | for i in &a { | ||
| - borrow of `a` occurs here | ||
LL | for j in a { | ||
| ^ move out of `a` occurs here | ||
|
||
error[E0382]: use of moved value: `a` | ||
--> $DIR/borrow-for-loop-head.rs:4:18 | ||
| | ||
LL | for j in a { | ||
| ^ value moved here in previous iteration of loop | ||
| | ||
= note: move occurs because `a` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait | ||
help: consider borrowing this to avoid moving it into the for loop | ||
| | ||
LL | for j in &a { | ||
| ^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
Some errors occurred: E0382, E0505. | ||
For more information about an error, try `rustc --explain E0382`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We might want to use a separate
CompilerDesugaringKind
variant to differentiate in errors, something along the lines ofForLoopHead
, or something, or modifyForLoop
to carry information on wether the source is completely synthetic or just moving around code that the user has written.