-
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
Detect when user is trying to create a lending Iterator
and give a custom explanation
#125407
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
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`. |
Oops, something went wrong.
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.
Can this be extended to all non-generic associated types?
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.
Which part specifically?
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 think Jack is saying that this PR has been tailored to just
Iterator
(andIterator::Item
), and is then posing the thought exercise of whether the logic here (and the associated diagnostic output) could be generalized, so that it would cover all associated types lacking a parameter (i.e. "non-generic").I.e., instead of specializing this check to
trait Iterator { type Item; ... }
, can it be extended totrait <AnyTraitNameHere> { type AnyAssociatedTypeHere; ... // but not GATs }
?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.
Possibly, but I think this would be a separate diagnostic. I've seen people trying to implement lending iterators, I even tried to implement it myself years ago before I understood how it works :) So note about iterators is the main part and I want to keep it.
I can try to make the check more generic and add an error message about using non-GAT associated type.
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.
Rebased, added a better error message for general case of non-GAT associated type, left the error for iterators as is.
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.
And I'm open to any suggestions on wording/formatting/etc.