Skip to content

RFC: Allow generic impls using local trait bounds #3821

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

victorGhiglione
Copy link

@victorGhiglione victorGhiglione commented May 27, 2025

This proposal was initially shared in a Reddit post I authored to introduce the idea and gather community feedback:

“Proposal to reconcile generics and Rust’s orphan rule”

While the post is still recent and awaiting responses, it serves as an early exposition of the problem and the motivation behind this RFC.

Rendered


```rust
#[compiler_built_in]
pub trait LocalToThisCrate {}
Copy link
Member

Choose a reason for hiding this comment

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

🤔 I was expecting this trait was added to every crate with pub(crate) visibility, so foreign crates naturally can't use it, and that the diagnostics can refer to dependency_package::LocalToThisCrate in situation that we are the foreign crate that violated the local-to-their-crate restriction

@comex
Copy link

comex commented May 27, 2025

The example is confusing. What exactly is Calculable? The name makes it sound like a trait but it appears to be a type. It seems like the LocalToThisCrate version would result in not only Vec2 + Point and Point + Vec2 producing Calculable, but also Vec2 + Vec2… is that really what you want?

The error message example also doesn’t make sense. Why would the compiler complain that “T may be a type defined outside of the current crate” when T is an unknown type that’s specifically required to be LocalToThisCrate?

Was this written with the help of AI? The writing style kind of feels like it.

Anyway, this feature doesn’t seem logically unsound, and it might be helpful for some use cases. But I don’t think it would address the most common pain points caused by the orphan rule. Those usually involve wanting to actually impl foreign traits for foreign types, as opposed to just wanting shorthand for writing a bunch of impls for local types.

@victorGhiglione
Copy link
Author

victorGhiglione commented May 27, 2025 via email

@ehuss ehuss added T-lang Relevant to the language team, which will review and decide on the RFC. T-types Relevant to the types team, which will review and decide on the RFC. labels May 27, 2025
@bluebear94
Copy link

I’ll second @comex; the example provided makes the feature look like one with situational utility.

One interesting thing that could be done with LocalToThisCrate is to use it as a supertrait to make the subtrait sealed.

@Jules-Bertholet
Copy link
Contributor

Jules-Bertholet commented May 29, 2025

I recently made a small post on the Internals forum proposing something similar to this, though I had a different use-case in mind. I am glad to see a related idea as a proper RFC.

My internals post doesn’t propose any sort of LocalToThisTrait trait; instead, you define your own trait, and when checking the orphan rules, the compiler verifies when necessary that there are no impls of the trait for upstream types. A major advantage of this is that downstream crates might be able to implement the trait as well.

@Jules-Bertholet
Copy link
Contributor

Jules-Bertholet commented May 29, 2025

I thought using AI would allow for a better translation, but I'm not sure it was the best choice.

A big sign of this is that there is a lot of “fluff” and vague/generic language in the RFC currently, especially in later sections (“Prior Art” and following). An RFC is not a box-checking exercise or something with a minimum/maximum word count. The goal is to provide all the info that the lang team will need when they discuss and decide on the proposal. They are always very busy—too many proposals, too little time—so you want to make it easy for them. If a sentence or paragraph doesn’t contribute to that, cut it out. And if it is an important detail, or something they might have questions about, then make sure it’s explained with concrete examples that demonstrate the full implications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC. T-types Relevant to the types team, which will review and decide on the RFC.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants