-
Notifications
You must be signed in to change notification settings - Fork 534
Add: orphan rule rationale. #1755
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
Conversation
TmSalviano
commented
Mar 11, 2025
•
edited
Loading
edited
- I thought it would be helpful to introduce the reader to the problem the orphan rule solves before being exposed to the rule itself. Understanding the orphan rule for the first time can be challenging, and knowing why it exists beforehand can make it easier to grasp.
- Also, including an explanation of the rationale behind the orphan rule in the orphan rule section of the page just makes sense.
Co-authored-by: Josh Triplett <[email protected]>
src/items/implementations.md
Outdated
implementation is local to your crate, ensuring only one crate defines the implementation and | ||
thereby maintaining coherence. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would personally put a “that” here:
implementation is local to your crate, ensuring only one crate defines the implementation and | |
thereby maintaining coherence. | |
implementation is local to your crate, ensuring that only one crate defines the implementation | |
and thereby maintaining coherence. |
src/items/implementations.md
Outdated
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably we'll want to move this into a note section, but it's generally going in the right direction. The one caveat I'd add is that the problem being prevented here isn't really about the compiler not knowing what to pick. The compiler knows what to do about that. It'd throw an ambiguity error. But what that means is that adding an impl for your own types or of your own traits would be breaking (in many more situations). So it's mostly about saving space for such authors, and that's the real rationale. Of course, whenever you talk about removing the rule, you do find out all the ways that we are leaning on the rule more technically, and those reasons are a bit more subtle. E.g.:
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
We had some conversations about reframing this a little, and so I pushed up some edits to try to capture some of the nuances that we discussed.
Update books ## rust-lang/reference 9 commits in 3bf3402aea982b876eb56c87da17b0685c6461d5..387392674d74656f7cb437c05a96f0c52ea8e601 2025-05-03 21:29:09 UTC to 2025-04-22 15:25:03 UTC - Document `let_chains` again (rust-lang/reference#1740) - Add: orphan rule rationale. (rust-lang/reference#1755) - Remove apologies about the Reference (rust-lang/reference#1792) - Clean up some inline assembly examples (rust-lang/reference#1804) - Remove StructExprTuple and StructExprUnit (rust-lang/reference#1803) - Improve documentation of struct expressions (rust-lang/reference#1799) - Clarify interaction of asm-goto with IBT (rust-lang/reference#1790) - naked functions (rust-lang/reference#1689) - Relabel grammarRailroad-Button (rust-lang/reference#1798) ## rust-lang/rust-by-example 3 commits in 0d7964d5b22cf920237ef1282d869564b4883b88..8a8918c698534547fa8a1a693cb3e7277f0bfb2f 2025-04-30 12:20:49 UTC to 2025-04-22 17:42:30 UTC - The example is not meant to be compiled. Changed the code block ann… (rust-lang/rust-by-example#1926) - Non-tail recursive call note in testcase_linked_list.md (rust-lang/rust-by-example#1924) - docs: mark illustrative 'static lifetime example with `ignore` (rust-lang/rust-by-example#1923)
Rollup merge of rust-lang#140676 - rustbot:docs-update, r=ehuss Update books ## rust-lang/reference 9 commits in 3bf3402aea982b876eb56c87da17b0685c6461d5..387392674d74656f7cb437c05a96f0c52ea8e601 2025-05-03 21:29:09 UTC to 2025-04-22 15:25:03 UTC - Document `let_chains` again (rust-lang/reference#1740) - Add: orphan rule rationale. (rust-lang/reference#1755) - Remove apologies about the Reference (rust-lang/reference#1792) - Clean up some inline assembly examples (rust-lang/reference#1804) - Remove StructExprTuple and StructExprUnit (rust-lang/reference#1803) - Improve documentation of struct expressions (rust-lang/reference#1799) - Clarify interaction of asm-goto with IBT (rust-lang/reference#1790) - naked functions (rust-lang/reference#1689) - Relabel grammarRailroad-Button (rust-lang/reference#1798) ## rust-lang/rust-by-example 3 commits in 0d7964d5b22cf920237ef1282d869564b4883b88..8a8918c698534547fa8a1a693cb3e7277f0bfb2f 2025-04-30 12:20:49 UTC to 2025-04-22 17:42:30 UTC - The example is not meant to be compiled. Changed the code block ann… (rust-lang/rust-by-example#1926) - Non-tail recursive call note in testcase_linked_list.md (rust-lang/rust-by-example#1924) - docs: mark illustrative 'static lifetime example with `ignore` (rust-lang/rust-by-example#1923)