-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #113174 - chenyukang:yukang-fix-102972-loop-next, r=c…
…ompiler-errors Better messages for next on a iterator inside for loops Fixes #102972
- Loading branch information
Showing
4 changed files
with
129 additions
and
2 deletions.
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
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,16 @@ | ||
fn test1() { | ||
let mut chars = "Hello".chars(); | ||
for _c in chars.by_ref() { | ||
chars.next(); //~ ERROR cannot borrow `chars` as mutable more than once at a time | ||
} | ||
} | ||
|
||
fn test2() { | ||
let v = vec![1, 2, 3]; | ||
let mut iter = v.iter(); | ||
for _i in iter { | ||
iter.next(); //~ ERROR borrow of moved value: `iter` | ||
} | ||
} | ||
|
||
fn main() { } |
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,33 @@ | ||
error[E0499]: cannot borrow `chars` as mutable more than once at a time | ||
--> $DIR/issue-102972.rs:4:9 | ||
| | ||
LL | for _c in chars.by_ref() { | ||
| -------------- | ||
| | | ||
| first mutable borrow occurs here | ||
| first borrow later used here | ||
LL | chars.next(); | ||
| ^^^^^^^^^^^^ second mutable borrow occurs here | ||
| | ||
= note: a for loop advances the iterator for you, the result is stored in `_c`. | ||
= help: if you want to call `next` on a iterator within the loop, consider using `while let`. | ||
|
||
error[E0382]: borrow of moved value: `iter` | ||
--> $DIR/issue-102972.rs:12:9 | ||
| | ||
LL | let mut iter = v.iter(); | ||
| -------- move occurs because `iter` has type `std::slice::Iter<'_, i32>`, which does not implement the `Copy` trait | ||
LL | for _i in iter { | ||
| ---- `iter` moved due to this implicit call to `.into_iter()` | ||
LL | iter.next(); | ||
| ^^^^^^^^^^^ value borrowed here after move | ||
| | ||
= note: a for loop advances the iterator for you, the result is stored in `_i`. | ||
= help: if you want to call `next` on a iterator within the loop, consider using `while let`. | ||
note: `into_iter` takes ownership of the receiver `self`, which moves `iter` | ||
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
Some errors have detailed explanations: E0382, E0499. | ||
For more information about an error, try `rustc --explain E0382`. |