Skip to content
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 2 commits into from
Jun 5, 2024

Conversation

pacak
Copy link
Contributor

@pacak pacak commented May 22, 2024

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 #125337

@rustbot
Copy link
Collaborator

rustbot commented May 22, 2024

r? @pnkfelix

rustbot has assigned @pnkfelix.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 22, 2024
&& def_id_matches_path(
self.r.tcx,
trait_id,
&["core", "iter", "traits", "iterator", "Iterator"],
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which part specifically?

Copy link
Member

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 (and Iterator::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 to trait <AnyTraitNameHere> { type AnyAssociatedTypeHere; ... // but not GATs } ?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

@pnkfelix
Copy link
Member

pnkfelix commented Jun 5, 2024

sorry for the delay. I agree that the proposed experiment, while potentially interesting, need not block landing this.

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jun 5, 2024

📌 Commit b70fb41 has been approved by pnkfelix

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 5, 2024
@pacak
Copy link
Contributor Author

pacak commented Jun 5, 2024

sorry for the delay. I agree that the proposed experiment, while potentially interesting, need not block landing this.

No worries. I did implemented more general case as well FWIW in addition to custom error for iterators. Are you okay with the wording/terminology?

bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 5, 2024
…iaskrgr

Rollup of 10 pull requests

Successful merges:

 - rust-lang#124746 (`rustc --explain E0582` additional example)
 - rust-lang#125407 (Detect when user is trying to create a lending `Iterator` and give a custom explanation)
 - rust-lang#125505 (Add intra-doc-links to rustc_middle crate-level docs.)
 - rust-lang#125792 (Don't drop `Unsize` candidate in intercrate mode)
 - rust-lang#125819 (Various `HirTyLowerer` cleanups)
 - rust-lang#125861 (rustc_codegen_ssa: fix `get_rpath_relative_to_output` panic when lib only contains file name)
 - rust-lang#125911 (delete bootstrap build before switching to bumped rustc)
 - rust-lang#125921 (coverage: Carve out hole spans in a separate early pass)
 - rust-lang#125940 (std::unix::fs::get_path: using fcntl codepath for netbsd instead.)
 - rust-lang#126022 (set `has_unconstrained_ty_var` when generalizing aliases in bivariant contexts)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 49f9504 into rust-lang:master Jun 5, 2024
6 checks passed
@rustbot rustbot added this to the 1.80.0 milestone Jun 5, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jun 5, 2024
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Detect when user tries to write Iterator that incorrectly borrows from Self and would need GATs
5 participants