# Limited Opt Out of Orphan Rule

**URL:** <https://internals.rust-lang.org/t/limited-opt-out-of-orphan-rule/21709>\
**Category:** language design\
**Created:** [October 15, 2024, 1:08pm UTC](https://internals.rust-lang.org/t/limited-opt-out-of-orphan-rule/21709 "2024-10-15T13:08:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dwarsloeper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dwarsloeper/32/11073_2.png) [@Dwarsloeper](https://internals.rust-lang.org/u/Dwarsloeper)\
**Post date:** [October 15, 2024, 1:08pm UTC](https://internals.rust-lang.org/t/limited-opt-out-of-orphan-rule/21709/1 "2024-10-15T13:08:34Z")

</div>

If I understand the orphan rule, it’s only about protecting users of my crate from chaos? If so, and if I’m willing to cope with any (including any future) collisions, I would want the ability to opt out just for myself!

The following syntax would limit the visibility of the `impl`, just like `pub` can be limited. I suggest that, within this visibility limitation, Rust allow implementation to be free from the orphan rule:

```rust
impl(self) Bar for Foo {}
impl(super) Bar for Foo {}
impl(in path) Bar for Foo {}
impl(crate) Bar for Foo {}

```

Making the `impl` more public than the type would be an error.

The trait methods would only be available within the declared perimeter. So, for special traits like `Drop`, only objects created within the scope of such an `impl` would run that `drop()` method. Can that be compatible with moving the object out of `Drop`’s scope? Maybe that would be a compiler error unless `Drop` is explicitly propagated, e.g. as `pub fn f() -> Foo + Drop`.

**Is this overall a logical model?** Is it something that the compiler can implement? At least `impl(self)` should be easy, right?

The above makes it easy to marry any trait to any type. So integration crates are possibly not needed at all, unless they actually implement specific code. In that case `impl(in path)` – unlike `pub(in path)` – could allow foreign pathes as well.

_Not sure if this should instead or additionally have been an issue on [Experiment with relaxing the Orphan Rule](https://github.com/rust-lang/rust-project-goals/blob/main/src/2024h2/Relaxing-the-Orphan-Rule.md)_

---

<div class="post-metadata">

**Author:** ![dlight](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dlight/32/8462_2.png) [@dlight](https://internals.rust-lang.org/u/dlight)\
**Post date:** [October 16, 2024, 2:45am UTC](https://internals.rust-lang.org/t/limited-opt-out-of-orphan-rule/21709/2 "2024-10-16T02:45:14Z")

</div>

What happens when two different crates impl `Hash` for the same type `MyType` (defined in a third crate) and each one builds a `HashSet<MyType>` with their own impls? Somehow the type system must prevent smuggling each hash table into the other crate, because they would actually break (you can't insert items with one `Hash` impl and then search for them with another impl).

The same may happen with traits like `PartialEq` or `PartialOrd` and many other traits.

That's a kind of bug that coherence tries to prevent.

---

<div class="post-metadata">

**Author:** ![zirconium-n](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/zirconium-n/32/9078_2.png) [@zirconium-n](https://internals.rust-lang.org/u/zirconium-n)\
**Post date:** [October 16, 2024, 7:45am UTC](https://internals.rust-lang.org/t/limited-opt-out-of-orphan-rule/21709/3 "2024-10-16T07:45:31Z")

</div>

We can restrict only `bin` crate can opted-out orphan rule, and can't be used as a dependency. And there's always a valid option called "compile error". If there's overlapping impls the compiler just error out.

It might be a version compat nightmare when upgrading dependencies, but that's my own choice.

---

<div class="post-metadata">

**Author:** ![Dwarsloeper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dwarsloeper/32/11073_2.png) [@Dwarsloeper](https://internals.rust-lang.org/u/Dwarsloeper)\
**Post date:** [October 16, 2024, 8:34am UTC](https://internals.rust-lang.org/t/limited-opt-out-of-orphan-rule/21709/4 "2024-10-16T08:34:50Z")

</div>

Gosh, you made me realize my huge shortcoming. I thought returning `HashSet<MyType + Hash>` would likewise solve it. But no, I can also move into a 3rd party function, which would not be prepared to consider a different `Drop` or `Hash` that it doesn’t have in scope. And your example even extends to references.

This would require some kind of `dyn`, but then every parameter to every function would need to be `dyn`, just in case. 😭

We could move the orphan rule to the point of use: your `HashSet<MyType>` without my `Hash` would **not be the same type** as my `HashSet<MyType>`. What new can of worms would that open?

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [October 16, 2024, 8:58am UTC](https://internals.rust-lang.org/t/limited-opt-out-of-orphan-rule/21709/5 "2024-10-16T08:58:44Z")

</div>

Changing the `Hash` to have a type argument like `Hash<whose implementation is this>` works, and if it was a rust-native feature I think it could have been made syntactically hidden most of the time.

You can actually implement workarounds for the orphan rule like that in stable Rust (see [`UserImplementationType` arg here](https://docs.rs/vpsearch/latest/vpsearch/trait.MetricSpace.html)).

It does mean that a hashmap will be specific to what context it has been created in, and you will get a type error when you mix implementations (except where you manage to make that dyn/generic).

---

<div class="post-metadata">

**Author:** ![dlight](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dlight/32/8462_2.png) [@dlight](https://internals.rust-lang.org/u/dlight)\
**Post date:** [October 16, 2024, 9:14am UTC](https://internals.rust-lang.org/t/limited-opt-out-of-orphan-rule/21709/6 "2024-10-16T09:14:25Z")

</div>

> [@Dwarsloeper](#):
>
> We could move the orphan rule to the point of use: your `HashSet<MyType>` without my `Hash` would **not be the same type** as my `HashSet<MyType>`. What new can of worms would that open?

Then the `MyType` in your crate is just a syntax sugar for `struct MyTypeWithHash(MyType)` (the newtype pattern) plus implementing `Hash` yourself. Newtypes are inconvenient and boilerplate-y, but they at least allow you to convert from one newtype into another (well if you can access the inner value, anyway)

I can't find it, but I remember a proposal here in IRLO that was basically this: a bare `MyType`, a `MyType with a given Hash impl` and a `MyType with another Hash impl` would all be different types. (this sounds like the typestate pattern, and really is just sugar for newtypes too)

---

<div class="post-metadata">

**Author:** ![Dwarsloeper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dwarsloeper/32/11073_2.png) [@Dwarsloeper](https://internals.rust-lang.org/u/Dwarsloeper)\
**Post date:** [October 16, 2024, 9:20am UTC](https://internals.rust-lang.org/t/limited-opt-out-of-orphan-rule/21709/7 "2024-10-16T09:20:10Z")

</div>

Newtype wrappers are convenient, but syntactic cruft. If they can be hidden in syntactic sugar, that would go a long way to transparently solving the problem.

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [October 16, 2024, 10:09am UTC](https://internals.rust-lang.org/t/limited-opt-out-of-orphan-rule/21709/8 "2024-10-16T10:09:51Z")

</div>

Allowing bin crates to opt out and make impls for themselves without regard for the orphan rule is a reasonable experiment that we've talked about a few times. It'd be a worthwhile first step to try.

---

<div class="post-metadata">

**Author:** ![Dwarsloeper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dwarsloeper/32/11073_2.png) [@Dwarsloeper](https://internals.rust-lang.org/u/Dwarsloeper)\
**Post date:** [October 16, 2024, 9:36pm UTC](https://internals.rust-lang.org/t/limited-opt-out-of-orphan-rule/21709/9 "2024-10-16T21:36:13Z")

</div>

I guess that would already solve many end users’ big pain point with Rust. Huge pity the linked todo isn’t getting tackled this semester as scheduled!

Since a bin defines a limit on visiblity anyway, the syntax I proposed can be added later, if and when this is also done for libs.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [October 16, 2024, 11:20pm UTC](https://internals.rust-lang.org/t/limited-opt-out-of-orphan-rule/21709/10 "2024-10-16T23:20:36Z")

</div>

> [@kornel](#):
>
> Changing the `Hash` to have a type argument like `Hash<whose implementation is this>` works, and if it was a rust-native feature I think it could have been made syntactically hidden most of the time.

This just reminds me of [passing an `IEqualityComparer` to a `Dictionary` constructor in C#](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.-ctor?view=net-8.0#system-collections-generic-dictionary-2-ctor(system-collections-generic-iequalitycomparer((-0)))). We probably would use a self-less trait, so implementations can assume that the same type has the same `Eq`, so that things like `HashMap: Eq` makes any sense, rather than an instance, but it's the same idea.

If we just had a defaulted comparer type on `HashMap`, then you could implement your own one of those for other types, and that also allows for things like rather than doing `BTreeSet<Reverse<i32>>`, you do `BTreeSet<i32, ReverseComparer>`.

As you say, I think any loosening of coherence basically ends up needing to do that under the hood anyway, so I think just exposing it as a normal thing might better than making it type system magic.

---

<div class="post-metadata">

**Author:** ![binarycat](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/binarycat/32/12210_2.png) [@binarycat](https://internals.rust-lang.org/u/binarycat)\
**Post date:** [October 17, 2024, 5:16am UTC](https://internals.rust-lang.org/t/limited-opt-out-of-orphan-rule/21709/11 "2024-10-17T05:16:27Z")

</div>

i've head this called [the hashtable problem](https://gist.github.com/nikomatsakis/1421744)

---

<div class="post-metadata">

**Author:** ![Tamschi](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tamschi/32/11541_2.png) [@Tamschi](https://internals.rust-lang.org/u/Tamschi)\
**Post date:** [October 17, 2024, 8:08pm UTC](https://internals.rust-lang.org/t/limited-opt-out-of-orphan-rule/21709/12 "2024-10-17T20:08:11Z")

</div>

This is relevant, I think:

> <https://github.com/rust-lang/rfcs/pull/3634>
>
> This may be a bit long. I thought it would be best to go over the interactions w…ith other language features and tooling as well as potential impacts thoroughly in advance, since while the addition to the language itself can be explained quickly, it does interact with \*a lot\* of other Rust features and aspects of the crate ecosystem.
> 
> In (very) short this is a version of orphan-rule avoidance (i.e. non-unique trait implementations) that does not suffer from either version of "the hashtable problem", does not obscure imports, can be safely ignored unless needed (but is discoverable), is seamlessly compatible with specialisation and tries to strike a good balance between usability and complexity, so that developers are automatically funneled towards correct and unsurprising but also highly compatible code.
> 
> I'm aware that there are often rash proposals in this direction, but I believe I've covered all previously-discussed issues.
> As the overall RFC is a fairly dense read, I've added cross-references and less-formal comments in form of blockquotes to it.
> 
> \---
> 
> Thanks
> 
> \- to @teliosdev for some very early syntax feedback that helped put me on track,
> \- to @cofinite for pointing out how scoped implementations allow syntax traits to be used as extension traits,
> \- to @thefakeplace and to SkiFire13 in the \[draft discussion\](https://internals.rust-lang.org/t/pre-rfc-scoped-impl-trait-for-type/19923) for suggestions on how to make this RFC more approachable and easier to understand.
> 
> \---
> 
> \[Rendered\](https://github.com/Tamschi/rust-rfcs/blob/scoped\_impl\_trait\_for\_type/text/3634-scoped-impl-trait-for-type.md)

I still haven't applied all necessary fixes (see the discussion; There are quite a few cases where code can assume a specific implementation currently, which is one major source of potential unsoundness.), but the RFC and comments taken together go over all problems with orphan rule relaxation that I know of.

There are some parts you could omit by not having import/export, but unfortunately _anything_ that relaxes the orphan rule must either be `unsafe` itself or take into account some not entirely obvious type system proofs that current Rust supports. (For example, there are two distinct hashtable problems, not just one, and type system proofs can be linked through associated types and multi-trait bounds. Blanket implementations must be treated as 'sealing' a trait for types they cover, too.)

---

<div class="post-metadata">

**Author:** ![DanielFath](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/danielfath/32/5372_2.png) [@DanielFath](https://internals.rust-lang.org/u/DanielFath)\
**Post date:** [October 18, 2024, 10:23am UTC](https://internals.rust-lang.org/t/limited-opt-out-of-orphan-rule/21709/13 "2024-10-18T10:23:30Z")

</div>

My honest question is what are design constraints on the orphan implementation? From my understanding, it's the following:

- Must be backwards compatible. Why? First, it preserves the crate ecosystem, and is one of Rust core promises. As a result, it might cause problems for traits being used to keep an invariant. Consider following.

- Should work for the Hash map problem: [Addressing "the hashtable problem" with modular type classes · GitHub](https://gist.github.com/nikomatsakis/1421744)

- In Zulip chat, it's mentioned that having a single trait per type is a great way to speed up compilation times in some cases, so maybe that is a good property to have as well.

Edit: list isn't complete ofc. It's mostly a conversation starter.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [October 18, 2024, 10:41am UTC](https://internals.rust-lang.org/t/limited-opt-out-of-orphan-rule/21709/14 "2024-10-18T10:41:43Z")

</div>

Compatibility/soundness could be solved by requiring the traits to opt-in to allowing 3rd party implementations.

Traits like `ToSql` and `serde::Serialize` could allow being implemented on anything by anyone, and that would be convenient without causing too much chaos.

---

<div class="post-metadata">

**Author:** ![DanielFath](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/danielfath/32/5372_2.png) [@DanielFath](https://internals.rust-lang.org/u/DanielFath)\
**Post date:** [October 18, 2024, 6:13pm UTC](https://internals.rust-lang.org/t/limited-opt-out-of-orphan-rule/21709/15 "2024-10-18T18:13:13Z")

</div>

Would that be backward compatible?

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [October 18, 2024, 7:29pm UTC](https://internals.rust-lang.org/t/limited-opt-out-of-orphan-rule/21709/16 "2024-10-18T19:29:31Z")

</div>

> [@Dwarsloeper](#):
>
> We could move the orphan rule to the point of use: your `HashSet<MyType>` without my `Hash` would **not be the same type** as my `HashSet<MyType>`. What new can of worms would that open?

That's what Scala does. A consequence of this is that even if `T == U`, you don't get that `T::Assoc == U::Assoc`. That can be quite painful in itself...

---

<div class="post-metadata">

**Author:** ![Aloso](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/aloso/32/5039_2.png) [@Aloso](https://internals.rust-lang.org/u/Aloso)\
**Post date:** [October 21, 2024, 11:13pm UTC](https://internals.rust-lang.org/t/limited-opt-out-of-orphan-rule/21709/17 "2024-10-21T23:13:16Z")

</div>

> We can restrict only `bin` crate can opted-out orphan rule

That would be a good start, but I hope we can do better. We could also relax the orphan rules when trait and implementation are in the same cargo workspace, or when the crate containing the implementation has `private = true`.

Of course, rustc isn't aware of cargo workspaces, so this would require a compiler flag like `--relax-orphan-rules=foo,bar`.

---

<div class="post-metadata">

**Author:** ![Tamschi](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tamschi/32/11541_2.png) [@Tamschi](https://internals.rust-lang.org/u/Tamschi)\
**Post date:** [October 22, 2024, 1:25pm UTC](https://internals.rust-lang.org/t/limited-opt-out-of-orphan-rule/21709/18 "2024-10-22T13:25:49Z")

</div>

Having trait definitions permit 3rd party implementations is unfortunately not backwards-compatible for existing traits, no.

If

- crate `a` defines `Trait`,
- crate `b`
  - defines `Type`,

  - implements `Trait for Type` ① and

  - has a function

then code at ② can currently assume to see implementation ① on `T`. ~~Changing an existing trait to allow third party implementations would be unsound, due to that.~~ (edit: I got this mixed up, see below.) Changing an existing _implementation_ to allow additional implementations would be sound, but that's not useful if such an implementation doesn't exist yet (and is exactly what [Specialisation](https://rust-lang.github.io/rfcs/1210-impl-specialization.html) covers, anyway, with its associated `default` items).

Something similar happens with `fn f<T: Sealed + Trait>() { … }` where `b` defines `Sealed`, i.e. `b` could use that to prove the identity of the `impl` is among a well-known set, and additionally rule out certain other types that are `Sealed` but not `Trait`.

This can be mitigated by adjusting how `TypeId::of` behaves in and on generics (must take added implementations into account _sometimes_) and by requiring the implementation of `Sealed` to opt into use alongside 3rd party implementations instead. Then the opt-in on `Trait`'s definition isn't required, either.

The details aren't trivial, but can be intuitive, I think. One problem is that you can get unexpected behaviour of value-key-type-erasing collections if the type differentiation is to broad though, or very high friction at crate boundaries if you don't except certain common generics like `Option`, `Result`, slices, function pointers, … (and adjusting these exceptions would at best always require an edition change, if I'm not entirely mistaken).

---

<div class="post-metadata">

**Author:** ![SichangHe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sichanghe/32/11042_2.png) [@SichangHe](https://internals.rust-lang.org/u/SichangHe)\
**Post date:** [October 24, 2024, 12:06am UTC](https://internals.rust-lang.org/t/limited-opt-out-of-orphan-rule/21709/19 "2024-10-24T00:06:01Z")

</div>

Please share specific examples of when you would like to opt out of the orphan rule. I think it would help much more.

These are what I can think of right now:

| General description | Example | Workaround |
| --- | --- | --- |
| External trait + external struct | Trying to derive serde on an external struct that does not implement it | newtype/ fork upstream/ do not rely on the struct implementing that trait |
| My trait + external struct | Library author wanting to let users implement their trait on others' structs for their use cases | [Add a dummy parameter to your trait and let user implement it using a dummy wrapper struct](https://github.com/SichangHe/work_around_orphan_check.rs) |

Hope this helps, Steven Hé (Sīchàng)

---

<div class="post-metadata">

**Author:** ![zackw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/zackw/32/2071_2.png) [@zackw](https://internals.rust-lang.org/u/zackw)\
**Post date:** [October 24, 2024, 4:01pm UTC](https://internals.rust-lang.org/t/limited-opt-out-of-orphan-rule/21709/20 "2024-10-24T16:01:13Z")

</div>

Concrete example of your first case: I have a project right now that uses [`rustix`](https://docs.rs/rustix/latest/rustix/) and [`unix_path`](https://docs.rs/unix_path/latest/unix_path/) in a no-std context. (It's a fully hosted environment; no-std is only being used to avoid a dependency on the C runtime.) Because `rustix` and `unix_path` are independent crates, neither of them provides an impl of [`rustix::path::Arg`](https://docs.rs/rustix/latest/rustix/path/trait.Arg.html) for [`unix_path::UnixPath`](https://docs.rs/unix_path/latest/unix_path/struct.Path.html), and the orphan rule prevents me from providing one, which means I have to write `path.as_unix_str().as_bytes()` every time I want to pass a path to a rustix function, which is often.

(It's my impression that this sort of thing is _the_ most important reason why people want to relax the orphan rule. I _could_ work around this with a newtype over `UnixPath`, but getting its ergonomics to match `UnixPath`'s would be more work than continuing to type `path.as_unix_str().as_bytes()` all over the place.)

* * *

This is not technically about the orphan rule, but it's a closely related possible language feature that would require us to solve many of the same problems: Sometimes a crate might wish to _override_ the implementation of a trait by one of its dependencies. This same project provides a good example: In a no-std configuration, rustix's `Display` impl for [`rustix::io::Errno`](https://docs.rs/rustix/latest/rustix/io/struct.Errno.html) prints "os error \<number\>" for all errors, which is not exactly user friendly; I'd ideally like to override it with an impl that provides the usual human-readable error strings for at least the errno values that I expect to come up in normal operation. Again, this can be dealt with by using a wrapper struct, but at the expense of at least some ergonomics -- right now what I have is a local conversion trait that lets me write

```rust
writeln!(stderr, "failed to {}: {}", operation, err.msg())

```

when `err` is an Errno, which is not bad but not as nice as

```rust
writeln!(stderr, "failed to {operation}: {err}")

```

[Next page](https://internals.rust-lang.org/t/limited-opt-out-of-orphan-rule/21709.md?page=2)
