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

Reject generic self types. #130098

Merged
merged 2 commits into from
Oct 30, 2024

Conversation

adetaylor
Copy link
Contributor

The RFC for arbitrary self types v2 declares that we should reject "generic" self types. This commit does so.

The definition of "generic" was unclear in the RFC, but has been explored in
#129147
and the conclusion is that "generic" means any self type which is a type parameter defined on the method itself, or references to such a type.

This approach was chosen because other definitions of "generic" don't work. Specifically,

  • we can't filter out generic type arguments, because that would filter out Rc and all the other types of smart pointer we want to support;
  • we can't filter out all type params, because Self itself is a type param, and because existing Rust code depends on other type params declared on the type (as opposed to the method).

This PR decides to make a new error code for this case, instead of reusing the existing E0307 error. This makes the code a bit more complex, but it seems we have an opportunity to provide specific diagnostics for this case so we should do so.

This PR filters out generic self types whether or not the 'arbitrary self types' feature is enabled. However, it's believed that it can't have any effect on code which uses stable Rust, since there are no stable traits which can be used to indicate a valid generic receiver type, and thus it would have been impossible to write code which could trigger this new error case. It is however possible that this could break existing code which uses either of the unstable arbitrary_self_types or receiver_trait features. This breakage is intentional; as we move arbitrary self types towards stabilization we don't want to continue to support generic such types.

This PR adds lots of extra tests to arbitrary-self-from-method-substs. Most of these are ways to trigger a "type mismatch" error which

// FIXME(arbitrary_self_types): We probably should limit the
hopes can be minimized by filtering out generics in this way. We remove a FIXME from confirm.rs suggesting that we make this change. It's still possible to cause type mismatch errors, and a subsequent PR may be able to improve diagnostics in this area, but it's harder to cause these errors without contrived uses of the turbofish.

This is a part of the arbitrary self types v2 project, rust-lang/rfcs#3519
#44874

r? @wesleywiser

@rustbot rustbot added A-testsuite Area: The testsuite used to check the correctness of rustc S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 8, 2024
@adetaylor
Copy link
Contributor Author

@rustbot label +S-waiting-on-author -S-waiting-on-review

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 8, 2024
@adetaylor
Copy link
Contributor Author

@wesleywiser Over to you for opinions here. See this comment for why I think this is the least bad approach for filtering out generics - to be explicit, we know it's still possible to have generic self types, but this probably reduces the risk of them being common and also resolves a FIXME.

I'd appreciate your help and adjudication about whether this is the right overall approach. We should probably get to the bottom of that before you review the PR in detail, but it's up to you!

@rustbot label -S-waiting-on-author +S-waiting-on-review

@adetaylor adetaylor marked this pull request as ready for review September 9, 2024 09:45
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 9, 2024
@rustbot
Copy link
Collaborator

rustbot commented Sep 9, 2024

Some changes occurred in diagnostic error codes

cc @GuillaumeGomez

@bors
Copy link
Contributor

bors commented Sep 16, 2024

☔ The latest upstream changes (presumably #130414) made this pull request unmergeable. Please resolve the merge conflicts.

@wesleywiser
Copy link
Member

See #129147 (comment) for why I think this is the least bad approach for filtering out generics - to be explicit, we know it's still possible to have generic self types, but this probably reduces the risk of them being common and also resolves a FIXME.

@adetaylor Thanks for that link! After reading that thread and a few others I stumbled across along the way, I think the approach makes sense and your implementation looks fine to me (I had one question I was unsure of and pinged compiler-errors for their opinion). The additions you made to the test suite were also really helpful in my initial understanding and I'm happy to see more test coverage there!

@adetaylor adetaylor force-pushed the arbitrary-self-types-block-generics branch 2 times, most recently from 84ca4cb to c2cbf1d Compare October 24, 2024 17:05
@bors
Copy link
Contributor

bors commented Oct 29, 2024

☔ The latest upstream changes (presumably #132317) made this pull request unmergeable. Please resolve the merge conflicts.

@wesleywiser
Copy link
Member

This looks great to me, just needs a rebase.

@bors delegate+

@adetaylor You can @bors r=wesleywiser after the merge conflict is fixed 🙂

@bors
Copy link
Contributor

bors commented Oct 29, 2024

✌️ @adetaylor, you can now approve this pull request!

If @wesleywiser told you to "r=me" after making some further change, please make that change, then do @bors r=@wesleywiser

@bors

This comment was marked as resolved.

@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 Oct 29, 2024
@wesleywiser

This comment was marked as resolved.

@bors bors removed the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Oct 29, 2024
@bors bors added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Oct 29, 2024
The RFC for arbitrary self types v2 declares that we should reject
"generic" self types. This commit does so.

The definition of "generic" was unclear in the RFC, but has been
explored in
rust-lang#129147
and the conclusion is that "generic" means any `self` type which
is a type parameter defined on the method itself, or references
to such a type.

This approach was chosen because other definitions of "generic"
don't work. Specifically,
* we can't filter out generic type _arguments_, because that would
  filter out Rc<Self> and all the other types of smart pointer
  we want to support;
* we can't filter out all type params, because Self itself is a
  type param, and because existing Rust code depends on other
  type params declared on the type (as opposed to the method).

This PR decides to make a new error code for this case, instead of
reusing the existing E0307 error. This makes the code a
bit more complex, but it seems we have an opportunity to provide
specific diagnostics for this case so we should do so.

This PR filters out generic self types whether or not the
'arbitrary self types' feature is enabled. However, it's believed
that it can't have any effect on code which uses stable Rust, since
there are no stable traits which can be used to indicate a valid
generic receiver type, and thus it would have been impossible to
write code which could trigger this new error case.
It is however possible that this could break existing code which
uses either of the unstable `arbitrary_self_types` or
`receiver_trait` features. This breakage is intentional; as
we move arbitrary self types towards stabilization we don't want
to continue to support generic such types.

This PR adds lots of extra tests to arbitrary-self-from-method-substs.
Most of these are ways to trigger a "type mismatch" error which
https://github.com/rust-lang/rust/blob/9b82580c7347f800c2550e6719e4218a60a80b28/compiler/rustc_hir_typeck/src/method/confirm.rs#L519
hopes can be minimized by filtering out generics in this way.
We remove a FIXME from confirm.rs suggesting that we make this change.
It's still possible to cause type mismatch errors, and a subsequent
PR may be able to improve diagnostics in this area, but it's harder
to cause these errors without contrived uses of the turbofish.

This is a part of the arbitrary self types v2 project,
rust-lang/rfcs#3519
rust-lang#44874

r? @wesleywiser
@adetaylor adetaylor force-pushed the arbitrary-self-types-block-generics branch from c2cbf1d to 86af0f9 Compare October 30, 2024 11:03
@adetaylor
Copy link
Contributor Author

@bors r=wesleywiser

@bors
Copy link
Contributor

bors commented Oct 30, 2024

📌 Commit 86af0f9 has been approved by wesleywiser

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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 30, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 30, 2024
Rollup of 6 pull requests

Successful merges:

 - rust-lang#130098 (Reject generic self types.)
 - rust-lang#131096 (rustdoc: Remove usage of `allow(unused)` attribute on `no_run` merged doctests)
 - rust-lang#132315 (compiletest: improve robustness of LLVM version handling)
 - rust-lang#132346 (Some graphviz tweaks)
 - rust-lang#132359 (Fix AIX libc call char type from i8 to u8)
 - rust-lang#132360 (Un-vacation myself)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 8d9686e into rust-lang:master Oct 30, 2024
6 checks passed
@rustbot rustbot added this to the 1.84.0 milestone Oct 30, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Oct 30, 2024
Rollup merge of rust-lang#130098 - adetaylor:arbitrary-self-types-block-generics, r=wesleywiser

Reject generic self types.

The RFC for arbitrary self types v2 declares that we should reject "generic" self types. This commit does so.

The definition of "generic" was unclear in the RFC, but has been explored in
rust-lang#129147
and the conclusion is that "generic" means any `self` type which is a type parameter defined on the method itself, or references to such a type.

This approach was chosen because other definitions of "generic" don't work. Specifically,
* we can't filter out generic type _arguments_, because that would filter out Rc<Self> and all the other types of smart pointer we want to support;
* we can't filter out all type params, because Self itself is a type param, and because existing Rust code depends on other type params declared on the type (as opposed to the method).

This PR decides to make a new error code for this case, instead of reusing the existing E0307 error. This makes the code a bit more complex, but it seems we have an opportunity to provide specific diagnostics for this case so we should do so.

This PR filters out generic self types whether or not the 'arbitrary self types' feature is enabled. However, it's believed that it can't have any effect on code which uses stable Rust, since there are no stable traits which can be used to indicate a valid generic receiver type, and thus it would have been impossible to write code which could trigger this new error case. It is however possible that this could break existing code which uses either of the unstable `arbitrary_self_types` or `receiver_trait` features. This breakage is intentional; as we move arbitrary self types towards stabilization we don't want to continue to support generic such types.

This PR adds lots of extra tests to arbitrary-self-from-method-substs. Most of these are ways to trigger a "type mismatch" error which https://github.com/rust-lang/rust/blob/9b82580c7347f800c2550e6719e4218a60a80b28/compiler/rustc_hir_typeck/src/method/confirm.rs#L519 hopes can be minimized by filtering out generics in this way. We remove a FIXME from confirm.rs suggesting that we make this change. It's still possible to cause type mismatch errors, and a subsequent PR may be able to improve diagnostics in this area, but it's harder to cause these errors without contrived uses of the turbofish.

This is a part of the arbitrary self types v2 project, rust-lang/rfcs#3519
rust-lang#44874

r? `@wesleywiser`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-testsuite Area: The testsuite used to check the correctness of rustc S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) 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.

5 participants