Great question! Let’s explore what would happen if we tried to enforce coherence in the presence of specialization of blanket impls by requiring negative trait bounds — that is, requiring that a more specific impl only applies when some other impl does not.
This idea is inspired by how some languages (like Haskell with OverlappingInstances, or logic programming) handle overlapping instances. The core idea is:
"Impl A applies only if no more-specific impl B applies."
But implementing this in Rust brings up both theoretical and practical challenges. Let's break it down.
🧠 Core Idea: Using Negative Bounds for Overlap Control
Suppose you have a general blanket impl:
impl<T> MyTrait for T { /* general impl */ }
And then a more specific one:
impl<T: Copy> MyTrait for T { /* specialized impl */ }
Today, these two overlap, which is not allowed. But what if we could write something like:
// Hypothetical syntax
impl<T: Copy + !MyTrait> MyTrait for T { /* specialized impl */ }
Or even better, using implicit logic:
“Apply this impl only if no more specific impl exists.”
That would mean the compiler tries to pick the most specific impl possible, falling back to less specific ones only if needed.
This kind of logic is known as closed-world assumption, or coinductive reasoning in logic programming.
✅ Pros of This Approach
1. Coherence Is Preserved
By ensuring that impls are mutually exclusive via negative bounds (or implicit exclusion), you avoid overlapping impls. Each type gets exactly one implementation of a trait.
2. More Flexible Abstractions
You could write generic fallback behavior while allowing crates downstream to override it conditionally without breaking anything else.
3. Better Ergonomics Than Sealed Traits
Instead of manually sealing traits with private marker types to simulate specialization, you'd get a built-in way to do conditional dispatch.
❌ Challenges & Trade-offs
1. Negative Reasoning Is Hard
Rust currently has no first-class support for negative trait bounds (T: !Trait) in trait resolution, especially across crates.
- You can't say "
T: !Copy" on an impl unless you're inside the same crate.
- Even within a crate, the trait solver doesn’t track negations well—it doesn’t know whether such a bound might become true later (e.g., after adding another impl).
- Adding this globally requires a full redesign of the trait system, including the trait solver, type inference, and dependency resolution.
2. Undecidability & Performance Risks
With negative bounds and overlapping impls, the trait solver becomes much more powerful—but also much less predictable.
Example:
impl<T: Send> Foo for T { … } // impl A
impl<T: !Send> Foo for T { … } // impl B
Now, if someone writes code like:
fn bar<T>(x: T) where T: Foo {}
The compiler must prove that either T: Send or T: !Send. But in Rust, due to future compatibility and orphan rules, you can't assume this dichotomy. So this function would be ill-typed under many proposed systems unless you restrict yourself to certain kinds of bounds.
This leads to:
3. Statically Enforcing Disjointness Is Difficult
How do you prove that two impls don't overlap? It’s easy in simple cases like T: Copy vs T: !Copy, but what about:
impl<T: A + B> Foo for T {}
impl<T: A + C> Foo for T {}
Are those disjoint? Not necessarily—what if some type implements all three?
You end up needing a full subsumption/subset analysis between trait bounds — a very hard problem computationally.
This leads to complexity explosion in the trait system.
4. Breaking Stable Abstraction Boundaries
If Crate A defines a trait MyTrait with a default impl:
impl<T> MyTrait for T { ... }
And Crate B adds:
impl<T: Clone> MyTrait for T { ... }
Then Crate C may behave differently depending on whether Crate B is present or not — even though Crate C never imports Crate B directly.
This breaks predictability and hermeticity of builds — a major concern for stable Rust.
5. Lack of First-Class Support in Trait System
Even if you solve the above, you still need:
- A formalized notion of impl priority
- A new kind of where clause (
where T: !SomeTrait)
- Better trait solver semantics (e.g., Prolog-style backtracking)
- Possibly a switch from the current Chalk-based logic to something more expressive
🚀 Alternatives That Avoid These Problems
There are safer ways to get similar effects without full-blown specialization:
✅ Sealed Traits
Manually simulate specialization by defining a sealed trait that controls which impls are allowed.
✅ Marker Types / Associated Constants
Use associated items or marker types to route logic through a single impl block.
✅ Newtype Pattern
Wrap types you want to customize behavior for in a newtype and implement the trait specifically for that wrapper.
✅ Feature Flags + Conditional Compilation
Sometimes, conditional impls are better handled at compile time rather than runtime.
🔚 Conclusion
Requiring negative trait bounds to enforce coherence in the face of blanket impl specialization is an appealing idea, and it would indeed solve the coherence problem in theory.
However, the cost includes:
- A massive rewrite of Rust's trait system
- Much more complex trait solving and inference
- Subtle changes in program behavior based on dependency graphs
- Increased risk of undecidable or ambiguous programs
So while this approach is theoretically viable, it comes with enormous complexity costs and risks to Rust’s stability guarantees.
Until there's a breakthrough in trait system design (or a shift in language priorities), Rust will likely continue avoiding unrestricted specialization of blanket impls — even with negative bounds.
Would you like a sketch of what a potential RFC for this feature might look like?