forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
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 rust-lang#125407 - pacak:no-lending-iterators, r=pnkfelix Detect when user is trying to create a lending `Iterator` and give a custom explanation The scope for this diagnostic is to detect lending iterators specifically and it's main goal is to help beginners to understand that what they are trying to implement might not be possible for `Iterator` trait specifically. I ended up to changing the wording from originally proposed in the ticket because it might be misleading otherwise: `Data` might have a lifetime parameter but it can be unrelated to items user is planning to return. Fixes rust-lang#125337
- Loading branch information
Showing
7 changed files
with
140 additions
and
8 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
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,35 @@ | ||
struct Data(String); | ||
|
||
impl Iterator for Data { | ||
type Item = &str; | ||
//~^ ERROR 4:17: 4:18: associated type `Iterator::Item` is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
Some(&self.0) | ||
} | ||
} | ||
|
||
trait Bar { | ||
type Item; | ||
fn poke(&mut self, item: Self::Item); | ||
} | ||
|
||
impl Bar for usize { | ||
type Item = &usize; | ||
//~^ ERROR 18:17: 18:18: in the trait associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type | ||
|
||
fn poke(&mut self, item: Self::Item) { | ||
self += *item; | ||
} | ||
} | ||
|
||
impl Bar for isize { | ||
type Item<'a> = &'a isize; | ||
//~^ ERROR 27:14: 27:18: lifetime parameters or bounds on type `Item` do not match the trait declaration [E0195] | ||
|
||
fn poke(&mut self, item: Self::Item) { | ||
self += *item; | ||
} | ||
} | ||
|
||
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,30 @@ | ||
error: associated type `Iterator::Item` is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type | ||
--> $DIR/no_lending_iterators.rs:4:17 | ||
| | ||
LL | type Item = &str; | ||
| ^ | ||
| | ||
note: you can't create an `Iterator` that borrows each `Item` from itself, but you can instead create a new type that borrows your existing type and implement `Iterator` for that new type. | ||
--> $DIR/no_lending_iterators.rs:3:19 | ||
| | ||
LL | impl Iterator for Data { | ||
| ^^^^ | ||
|
||
error: in the trait associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type | ||
--> $DIR/no_lending_iterators.rs:18:17 | ||
| | ||
LL | type Item = &usize; | ||
| ^ this lifetime must come from the implemented type | ||
|
||
error[E0195]: lifetime parameters or bounds on type `Item` do not match the trait declaration | ||
--> $DIR/no_lending_iterators.rs:27:14 | ||
| | ||
LL | type Item; | ||
| - lifetimes in impl do not match this type in trait | ||
... | ||
LL | type Item<'a> = &'a isize; | ||
| ^^^^ lifetimes do not match type in trait | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0195`. |