A Rust crate defines `trait Encode` with a blanket `impl<T: Display> Encode for T`; why does adding `impl Encode for SystemTime` fail with E0119 although `SystemTime` has no `Display` impl?
answer
- overlap is checked against the future
- std may add an impl later
- negative reasoning only for local types
- a marker trait you own
basics
~20 sRust's overlap check must survive std adding impl Display for SystemTime in a later release, when both impls would apply. rustc assumes an impl is absent only where no other crate could add it, as for a local type.
solid answer
~50 sA blanket impl claims every type that satisfies its bound, and coherence forbids two impls that could apply to the same type. rustc does not ask whether `SystemTime` implements `Display` today; it asks whether it could without your crate changing. std is allowed to add `impl Display for SystemTime` in a future release, and then the blanket impl and the specific impl would both apply, so rustc rejects the pair with E0119 and the note "upstream crates may add a new impl of trait `std::fmt::Display` for type `std::time::SystemTime` in future versions". For a **local** type without `Display`, the same specific impl compiles, because only your crate could add that `Display` impl. Fixes: bound the blanket on a marker trait you own, write per-type impls instead of the blanket, or wrap `SystemTime` in a local type. Specialization, which would let the specific impl win, is still unstable.
code
plaintext · 10 lineserror[E0119]: conflicting implementations of trait `Encode` for type `SystemTime`
--> src/main.rs:14:1
|
8 | impl<T: Display> Encode for T {
| ----------------------------- first implementation here
...
14 | impl Encode for SystemTime {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `SystemTime`
|
= note: upstream crates may add a new impl of trait `std::fmt::Display` for type `std::time::SystemTime` in future versionsgo deeper
Know that a blanket impl like impl<T: Display> Encode for T covers every Display type, and that writing a second impl for a type it covers is a conflict, E0119.
Explain why impl ToString for MyType conflicts once MyType implements Display, and why std's docs tell you to implement Display instead.
Explain the future-compatibility rule: rustc assumes upstream may add Display for a foreign type, reasons negatively only about local types, and offers the marker-trait, per-type or newtype fixes.
Treat a blanket impl over a foreign bound as an API commitment: it claims a large set of types for good, so weigh it against an opt-in marker trait before publishing.
## What a blanket impl claims A **blanket impl** implements a trait for every type that meets a bound, for example `impl<T: Display> Encode for T`. It is how std gives every `Display` type a `to_string` method: alloc contains `impl<T: fmt::Display + ?Sized> ToString for T`. Coherence says at most one impl of a trait may apply to any type, so once the blanket exists, any other impl of `Encode` must be provably disjoint from it, now and in the future. ## Why `SystemTime` overlaps although it has no `Display` Today `SystemTime` implements `Debug` but not `Display`, so the two impls do not actually collide. rustc still reports E0119, "conflicting implementations of trait `Encode` for type `SystemTime`", with the note "upstream crates may add a new impl of trait `std::fmt::Display` for type `std::time::SystemTime` in future versions". The overlap check is built to survive dependency upgrades: 1. The standard library owns both `Display` and `SystemTime`, so it may add `impl Display for SystemTime` in any release without asking you. 2. If your crate relied on the absence of that impl, the upgrade would create two applicable impls and break your build. 3. To keep that from happening, rustc reasons "this type does not implement this trait" only when no upstream crate could add the impl without a breaking change, which in practice means the type or the trait is local. For a foreign trait and a foreign type it assumes the impl might appear; a few fundamental traits, such as `Sized` and the `Fn` traits, are the exception. The same specific impl for a **local** struct compiles: with `pub struct Reading(u32);` and no `Display` for it, `impl Encode for Reading` is accepted beside the blanket, because only your crate could ever implement `Display` for `Reading`. The orphan rule is what makes that negative reasoning sound. ## The same error with std's own blanket impl The `ToString` blanket produces the textbook case. If `Celsius` implements `Display` and you also write `impl ToString for Celsius`, rustc reports E0119 and points at alloc's `impl<T> ToString for T where T: std::fmt::Display, T: ?Sized`. The `ToString` docs say it outright: implement `Display` instead, and you get `ToString` for free. Core's `impl<T> From<T> for T` causes the same failure for `impl<T> From<T> for Celsius`. | specific impl beside `impl<T: Display> Encode for T` | outcome | |---|---| | `impl Encode for u32` | E0119: `u32` implements `Display` already | | `impl Encode for SystemTime` | E0119: std may add `Display` later | | `impl Encode for Vec<u8>` | E0119: same upstream-may-add note | | `impl Encode for Reading`, local, no `Display` | accepted | ## Fixes on stable Rust - **Bound the blanket on a trait you own.** Declare `trait EncodeAsText: Display {}`, implement it for the types that should use the text encoding, and write `impl<T: EncodeAsText> Encode for T`. Only your crate can implement `EncodeAsText`, so rustc can see that `SystemTime` is not covered and accepts `impl Encode for SystemTime`. The cost is one opt-in line per type. - **Drop the blanket.** Write explicit impls for the types you support, often generated by a `macro_rules!` macro over a list of types. - **Wrap the foreign type.** A local newtype such as `struct UnixMillis(SystemTime);` without `Display` is disjoint from the blanket for the same reason `Reading` is. ## Specialization is not the answer yet The feature that would let a more specific impl override a blanket one is **specialization**: the blanket marks its method `default fn` and the specific impl wins. It is unstable. On stable rustc 1.99.1, `default fn` inside an impl fails with E0658, "specialization is experimental", and `#![feature(specialization)]` is refused with E0554 because feature gates are nightly-only. std uses a restricted form, `min_specialization`, internally (alloc's `ToString` dispatches through an internal `SpecToString` trait with special-cased impls for types such as `char`, `bool` and the integers), but that is not available to stable code. ## The library-author angle The reasoning that rejected your impl also binds the crates you depend on: for std, adding a new `Display` impl is the kind of change the overlap check anticipates. For your own library, a blanket impl over a foreign bound claims a large set of types up front; downstream crates can then no longer write their own impl for any of their types that implement the bound's trait, which is worth deciding deliberately before publishing.
- Why does `impl Encode for Reading` compile beside the blanket when `Reading` is a local struct without `Display`?Only your crate can implement `Display` for `Reading`, because the orphan rule bars every other crate from it. rustc can therefore rely on the impl being absent, so the blanket and the specific impl are disjoint. If you later add `Display` for `Reading`, the same pair becomes E0119.
- Can specialization resolve the overlap on stable Rust?No. Specialization, a blanket impl with `default fn` overridden by a specific impl, is unstable: stable rustc rejects `default fn` with E0658, and `#![feature(specialization)]` is refused with E0554. std uses `min_specialization` internally, which stable code cannot use.
- How does the marker-trait fix make `impl Encode for SystemTime` legal?The blanket becomes `impl<T: EncodeAsText> Encode for T`, where `EncodeAsText` is a trait your crate defines. No other crate can implement it for `SystemTime`, so rustc knows the blanket does not cover `SystemTime` and accepts the specific impl. Each text-encoded type opts in with one empty impl.
- rustc only checks overlap against the impls that exist today.
- Since SystemTime has no Display, the two impls cannot overlap.
- Writing the specific impl after the blanket makes it take priority.
- Specialization with default fn is stable since the 2024 edition.
- You should implement ToString directly for your type instead of Display.