Ixrec / rust-orphan-rules

An unofficial, experimental place for documenting and gathering feedback on the design problems around Rust's orphan rules
Apache License 2.0
200 stars 3 forks source link

Use different orphan rules for different traits #15

Open Ixrec opened 6 years ago

Ixrec commented 6 years ago

The main use case here is binary operator traits. "Little Orphan Impls" discusses this use case a lot but ends up arguing that ordering should matter, and it does matter in today's rules, which is a surprise people usually encounter with the binary operator traits.

https://internals.rust-lang.org/t/coherence-rules-for-generic-binary-operator-traits/4778/10 suggests that we could simply make these traits use different orphan rules where ordering does not matter. https://internals.rust-lang.org/t/orphan-rules/1322/11?u=ixrec suggests manually specifying the ordering for orphan rules separately from the actual ordering of type parameters in the public API.

Since changing which orphan rules apply to a trait would obviously be backwards incompatible (and no, even editions can't change that), we probably can't actually do this for std's operator traits. Are there any use cases with third party traits where this would help?

Ixrec commented 6 years ago

Looks like Scala is considering letting type classes declare whether or not they should be coherent, which is sort of the logical extreme of this idea.