Conflicting implementation with slices and trait objects

Hi. I'm trying to compile this code, but it unexpectedly fails (I tried to reduce it to the smallest example possible).

#![feature(ptr_metadata)]

use std::ptr::{Pointee, DynMetadata};

trait Bar {}

impl<T> Bar for [T] {}

// error[E0119]: conflicting implementations of trait `Bar` for type `[_]`
impl<T> Bar for T
where
    T: Pointee<Metadata = DynMetadata<T>> + ?Sized,
{}

try on playground: Rust Playground

Rust version: 1.95.0-nightly (2026-02-02), playground.

So, is something I'm doing wrong or is it too complex for the compiler to understand that [T] never implements Pointee<Metadata = DynMetadata<T>>?

Rust doesn't have negative reasoning about trait bounds, so it's not possible to say that something will never implement a trait.

Rust doesn't just check whether something implements the trait currently. It checks whether it's theoretically possible at all in the future.

2 Likes

So is there a way to do something similar to my example?
What I needed to do is implement a trait for [T], str and any dyn SomeTrait, which I thought I could do by implementing the trait on T: Pointee<Metadata = DynMetadata<T>>, but it conflicts with [T].

Maybe I could use specialization or negative trait bounds but it would require me to add another nightly feature to my project

In stable Rust there's no generic solution. You can make a bunch of implementations for concrete types (using a macro). You can implement the trait for Wrapper<[T]>, because the compiler knows core won't be able to add a conflicting implementation for your Wrapper type.

1 Like

While I'm sure this won't solve your actual problem, you could add the ?Sized bound to T and implement Bar for it:

trait Bar {}
impl<T: ?Sized> Bar for T {}

This will implement Bar for all types including DSTs (e.g., [T], str, and dyn SomeTrait). Presumably your impls would be different among [T], str, and dyn SomeTrait though.

You're likely going to have to go the newtype route explained above. Alternatively you may be able to use a trait defined in the same crate as your bound in lieu of Pointee:

trait Bar {}
trait Foo {}
impl<T> Bar for [T] {}
impl<T: Foo + ?Sized> Bar for T {}

Yet another possibility is finding a common type that [T], str, and dyn SomeTrait can be represented as. For example, maybe you could have an impl that works on [u8]s. Then you simply need a bound that is likely to be met that would allow you to convert your T into [u8] like:

trait Bar {
    fn bar(&self) {}
}
impl<T: AsRef<[u8]> + ?Sized> Bar for T {
    fn bar(&self) {
        self.as_ref();
    }
}

The problem is that the first example implements Bar for all types, and not only str, slices and trait objects, and the second example still has conflicts.

Also note that in my real example I have a function to implement, so I can't abstract this much the implementation on all types. all slices implement it the same way, and all trait objects do in the same way (but not the same as slices)

The second example does not have any conflict since Foo is defined in the same crate.

But there is a workaround.

3 Likes

Well but I need to implement Foo on all T: Pointee<Metadata = DynMetadata<T>> or else the implementation won't work with any type (as Foo is not implemented on any), and if I do, the it will cause conflicts

You're missing the ?Sized bound as without it there is no conflict, but regardless this is why I claimed my suggestions would likely not work for your situation. Does @quinedot's suggestion work for you? If not, then you don't really have a choice but to use additional unstable features or newtypes as mentioned in the accepted answer.