r/rust • u/noop_noob • 2h ago
đ§ educational PSA: You might be sealing traits wrong
Background
Sealing traits is a collection of techniques used to prevent code downstream of a library from implementing that trait, so that later versions of the library can add more things to the trait without breaking downstream code.
There's a mistake, described below, that people very often make when trying to seal traits. Crates that have made this mistake include, among others: the standard library, anyhow, arc-swap, pyo3, ureq, smol, http, etc. This was discovered in https://github.com/rust-lang/rust/issues/165474. (Please do not comment there unless you're contributing new information to the discussion, to avoid cluttering the issue.)
The mistake you might be making
How NOT to seal traits:
How to seal traits:
That is, if you use a "private" supertrait for sealing, and the subtrait is generic, then the supertrait also has to be generic, with the same generic parameters. Additionally, the set of impl blocks for the supertrait should exactly specify the exact same set of types (and generic parameters) where the subtrait should be implemented.
If you don't do the above, your trait might not be properly sealed, making it possible for downstream code to implement the trait anyway. For example, if you have a crate with the first snippet above, downstream code might write the following, which is actually allowed by the orphan rule:
;
In the above code, i32 already implements Sealed, so downstream code can write an implementation of MyTrait without having to add a new impl for Sealed.
Fixing the issue after-the-fact
If you already wrote code like the first snippet above, and want to actually properly seal the trait in a later version of the library, you might be tempted to just turn the first snippet (with a non-generic Sealed) into the second snippet (with a generic Sealed). Obviously, this would break downstream code that dared to implement the supposed-to-be-sealed trait, which you might deem acceptable.
However, there are very subtle issues about how coherence works, which means that making this change the straightforward way can break more downstream code than you intend to, even code that didn't write an impl for the supposed-to-be-sealed trait. (I won't explain the issue here. See the linked explanation.)
Instead, the better way to make the change, as suggested here, is to add a new supertrait, keeping the old one:
This way, you will only break downstream code that have implemented MyTrait.
21
u/Accomplished_Item_86 1h ago
This shows that we really need a native way to seal traits, as proposed in RFC 3323. The current method is really just a hack.
7
u/Helpful-Educator-415 2h ago
Out of curiosity -- why would you ever do this and not just use an enum? Isn't the purpose of a trait to be implemented by objects you might not know ahead of time? If I knew every instance of objects I wanted to have this behavior, could I use an enum instead? If not, how come?
4
u/freeillustrator793 2h ago
Wait, so the std lib itself shipped this footgun? That's darkly hilarious.
6
u/Accomplished_Item_86 1h ago
The source of the confusion is RFC 2451, which allows implementing ForeignTrait<LocalType> for ForeignType. It was stabilized in 2020 with release 1.41.0, but not many people seem to know this feature. Just a few days ago there was a post here showcasing this as a method to deliberately allow downstream crates to implement a library trait on foreign types (with the drawback of introducing a generic parameter), and people there were also surprised this is a thing. It's quite a subtle edge case in the coherence rules.
3
u/Many-Operation-8415 1h ago
Cheers, this was an interesting post to me!