From 137860d4c066216ccbeda745b7da82d72a0816ba Mon Sep 17 00:00:00 2001 From: TmSalviano Date: Tue, 11 Mar 2025 05:24:20 -0300 Subject: [PATCH 1/3] Add: orphan rule rationale. --- src/items/implementations.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/items/implementations.md b/src/items/implementations.md index dff5c03eb..1d3da5efd 100644 --- a/src/items/implementations.md +++ b/src/items/implementations.md @@ -174,6 +174,15 @@ be instantiable with the same set of types for the input type parameters. --> r[items.impl.trait.orphan-rule] #### Orphan rules +r[items.impl.trait.orphan-rule.rationale] +The orphan rules ensure that other peoples code can't break your code, and vice versa. +If an external crate implements an external trait for an external type, and your crate also +implements the same trait for the same type, the compiler wouldn't know which implementation +to use.\ +The orphan rule prevents this by requiring that either the trait or some type in the +implementation is local to your crate, ensuring only one crate defines the implementation and +thereby maintaining coherence. + r[items.impl.trait.orphan-rule.general] Given `impl Trait for T0`, an `impl` is valid only if at least one of the following is true: From 009ebdde96196b1a701e470d32c53e7b91e19e6f Mon Sep 17 00:00:00 2001 From: Tiago <144632256+TmSalviano@users.noreply.github.com> Date: Thu, 13 Mar 2025 17:21:11 -0300 Subject: [PATCH 2/3] Fix: grammar error . Fix: improved semantics Co-authored-by: Josh Triplett --- src/items/implementations.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/items/implementations.md b/src/items/implementations.md index 1d3da5efd..8720ff1a5 100644 --- a/src/items/implementations.md +++ b/src/items/implementations.md @@ -175,7 +175,7 @@ r[items.impl.trait.orphan-rule] #### Orphan rules r[items.impl.trait.orphan-rule.rationale] -The orphan rules ensure that other peoples code can't break your code, and vice versa. +The orphan rule helps ensure that other people's code can't break your code, and vice versa. If an external crate implements an external trait for an external type, and your crate also implements the same trait for the same type, the compiler wouldn't know which implementation to use.\ From e49621528ff96ca7a8d6d0e51f75549c7c08ed23 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sat, 3 May 2025 11:02:35 -0700 Subject: [PATCH 3/3] Update orphan rule introduction This updates the orphan rule introduction based on some conversations to try to emphasize a few points: - Say what it is before explaining why it exists. - Frame the rationale as saving space for crate authors. --- src/items/implementations.md | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/items/implementations.md b/src/items/implementations.md index 8720ff1a5..796d61c61 100644 --- a/src/items/implementations.md +++ b/src/items/implementations.md @@ -174,14 +174,12 @@ be instantiable with the same set of types for the input type parameters. --> r[items.impl.trait.orphan-rule] #### Orphan rules -r[items.impl.trait.orphan-rule.rationale] -The orphan rule helps ensure that other people's code can't break your code, and vice versa. -If an external crate implements an external trait for an external type, and your crate also -implements the same trait for the same type, the compiler wouldn't know which implementation -to use.\ -The orphan rule prevents this by requiring that either the trait or some type in the -implementation is local to your crate, ensuring only one crate defines the implementation and -thereby maintaining coherence. +r[items.impl.trait.orphan-rule.intro] +The *orphan rule* states that a trait implementation is only allowed if either the trait or at least one of the types in the implementation is defined in the current crate. It prevents conflicting trait implementations across different crates and is key to ensuring coherence. + +An orphan implementation is one that implements a foreign trait for a foreign type. If these were freely allowed, two crates could implement the same trait for the same type in incompatible ways, creating a situation where adding or updating a dependency could break compilation due to conflicting implementations. + +The orphan rule enables library authors to add new implementations to their traits without fear that they'll break downstream code. Without these restrictions, a library couldn't add an implementation like `impl MyTrait for T` without potentially conflicting with downstream implementations. r[items.impl.trait.orphan-rule.general] Given `impl Trait for T0`, an `impl` is valid only if at